[ library(json) | Reference Manual | Alphabetic Index ]
json_write(+Stream, ++JsonTerm, +Options)
Write a JSON element to a stream
- Stream
- Output stream
- JsonTerm
- Term describing JSON object to write
- Options
- List of options
Description
Write a JSON element. JSON elements are described by an ECLiPSe term
whose structure mimics the JSON element, as follows:
- object
- Structure with functor {}/1 whose single argument is either
a comma-sequence of Name:Value terms, a list of Name:Value terms,
or a single Name:Value term. Name is a string or atom, and Value
is another JSON element. E.g.
{["a":123,"b":[4,5]]}
or {"a":123,"b":[4,5]}
.
The empty object {}
is also allowed.
- array
- A list or array whose elements are JSON elements, e.g.
[1,{"a":true}]
or [](1,{"a":true})
- string
- A string or atom
- number
- An integer or float
- true
- the atom
true
- false
- the atom
false
- null
- the atom
null
Options is a list that can contain
- indent(N) - default 1
- Indent each structure level by N spaces.
indent(0) suppresses all extra spaces and newlines.
- float_format(String) - default "%q"
- use the given printf-format to print floating point numbers,
e.g. "%.3f"
Examples
?- json_write(output, [1, 2.3, foo, "bar"], []).
[
1,
2.3,
"foo",
"bar"
]
?- json_write(output, {foo:123, "bar":4.5, baz:true}, []).
{
"foo":123,
"bar":4.5,
"baz":true
}
?- json_write(output, {[foo:123, "bar":4.5, baz:true]}, []).
{
"foo":123,
"bar":4.5,
"baz":true
}
?- json_write(output, hello(world), []),
type error: expected json_term, found hello(world) in json_write / 3
Abort
See Also
json_read / 3