
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.
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.
json_write(+Stream, +Term) is det
json_write(+Stream, +Term, +Options) is detValues can be of the form #(Term), which causes Term to be stringified if it is not an atom or string. Stringification is based on term_string/2.
The version 7 dict type is supported as well. Optionally, if
the dict has a tag, a property "type":"tag" can be added to
the object. This behaviour can be controlled using the tag
option (see below). For example:
?- json_write(current_output, point{x:1,y:2}).
{
"x":1,
"y":2
}
?- json_write(current_output, point{x:1,y:2}, [tag(type)]).
{
"type":"point",
"x":1,
"y":2
}
In addition to the options recognised by json_read/3, we process the following options are recognised:
true (default false), serialize unknown terms and
print them as a JSON string. The default raises a type
error. Note that this option only makes sense if you can
guarantee that the passed value is not an otherwise valid
Prolog reporesentation of a Prolog term.
If a string is emitted, the sequence </ is emitted as
<\/. This is valid JSON syntax which ensures that JSON
objects can be safely embedded into an HTML <script>
element.
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_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.
json_write(+Stream, +Term) is det
json_write(+Stream, +Term, +Options) is detValues can be of the form #(Term), which causes Term to be stringified if it is not an atom or string. Stringification is based on term_string/2.
The version 7 dict type is supported as well. Optionally, if
the dict has a tag, a property "type":"tag" can be added to
the object. This behaviour can be controlled using the tag
option (see below). For example:
?- json_write(current_output, point{x:1,y:2}).
{
"x":1,
"y":2
}
?- json_write(current_output, point{x:1,y:2}, [tag(type)]).
{
"type":"point",
"x":1,
"y":2
}
In addition to the options recognised by json_read/3, we process the following options are recognised:
true (default false), serialize unknown terms and
print them as a JSON string. The default raises a type
error. Note that this option only makes sense if you can
guarantee that the passed value is not an otherwise valid
Prolog reporesentation of a Prolog term.
If a string is emitted, the sequence </ is emitted as
<\/. This is valid JSON syntax which ensures that JSON
objects can be safely embedded into an HTML <script>
element.
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