
json.pl -- Reading and writing JSON serializationThis module supports reading and writing JSON objects. This library supports two Prolog representations (the new representation is only supported in SWI-Prolog version 7 and later):
json(NameValueList),
a JSON string as an atom and the JSON constants null, true and
false as @(null), @(true) and @false.null, true and false.
This module provides the json Quasi Quotation syntax that allows for
embedding JSON documents in Prolog.
atom_json_term(?Atom, ?JSONTerm, +Options) is detatom (default),
string, codes or chars.
json_read(+Stream, -Term) is det
json_read(+Stream, -Term, +Options) is detjson(NameValueList), where
NameValueList is a list of Name=Value. Name is an atom
created from the JSON string.true and false are mapped -like JPL-
to @(true) and @(false).null is mapped to the Prolog term
@(null)Here is a complete example in JSON and its corresponding Prolog term.
{ "name":"Demo term",
"created": {
"day":null,
"month":"December",
"year":2007
},
"confirmed":true,
"members":[1,2,3]
}
json([ name='Demo term',
created=json([day= @null, month='December', year=2007]),
confirmed= @true,
members=[1, 2, 3]
])
The following options are processed:
null. Default @(null)true. Default @(true)false. Default @(false)error):
error, throw an unexpected
end of file syntax error
Returning an status term is required to process
Concatenated
JSON.
Suggested values are @(eof) or end_of_file.
atom.
The alternative is string, producing a packed string object.
Please note that codes or chars would produce ambiguous
output and are therefore not supported.
ws(+Stream, -Next) is det[private]
ws(+C0, +Stream, -Next)[private]// ... comments.
json_print_length(+Value, +Options, +Max, +Len0, +Len) is semidet[private]
is_json_term(@Term) is semidet
is_json_term(@Term, +Options) is semidettrue, false and null constants.
json_read_dict(+Stream, -Dict) is det
json_read_dict(+Stream, -Dict, +Options) is dettrue, false and null are represented using these
Prolog atoms.type field in an object assigns a tag for
the dict.
The predicate json_read_dict/3 processes the same options as
json_read/3, but with different defaults. In addition, it
processes the tag option. See json_read/3 for details about
the shared options.
tag option does not
apply.null.true.falsestring. The alternative is atom, producing a
packed string object.
json_write_dict(+Stream, +Dict) is det
json_write_dict(+Stream, +Dict, +Options) is det
atom_json_dict(+Atom, -JSONDict, +Options) is detatom,
string or codes.
json(+Content, +Vars, +VarDict, -JSON) is detjson
indicator. Notably, you can't use a Prolog variable in place of an
object key. Here is an example.
{|json(Name)||
{ "name": Name,
"created": {
"day":null,
"month":"December",
"year":2007
},
"confirmed":true,
"members":[1,2,3]
}
|}.
The following predicates are exported, but not or incorrectly documented.
json_write_dict(Arg1, Arg2, Arg3)
json_read_dict(Arg1, Arg2, Arg3)
is_json_term(Arg1, Arg2)
json_write(Arg1, Arg2, Arg3)
json_write(Arg1, Arg2)
json_read(Arg1, Arg2, Arg3)