[det]json_write(+Stream,
+Term)Values 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.
Rational numbers are emitted as floating point numbers. The hook json_write_hook/4 can be used to realize domain specific alternatives.
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:
- width(+Width)
- Width in which we try to format the result. Too long lines switch from horizontal to vertical layout for better readability. If performance is critical and human readability is not an issue use Width = 0, which causes a single-line output.
- step(+Step)
- Indentation increment for next level. Default is 2.
- tab(+TabDistance)
- Distance between tab-stops. If equal to Step, layout is generated with one tab per level.
- non_finite(+How)
- JSON has no representation for the floating point values infinity and
NaN. How is one of
error(default), raising a domain error,null, writing these values asnullorjson5, writing them as the JSON5 tokens Infinity,-Infinityand NaN. - serialize_unknown(+Boolean)
- If
true(defaultfalse), 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 representation of a Prolog term.
If a string is emitted, the sequence </ is emitted as
<\/, <! as <\u0021 and
the characters U+2028 and U+2029 are escaped. This is valid JSON syntax
which ensures that JSON objects can be safely embedded into an HTML <script>
element.
Integer keys, as may appear in dicts, are written as strings.
- Errors
domain_error(acyclic_term, Term)if Term is cyclic.