json_lines
ï
The json_lines
library provides predicates for parsing and
generating data in the JSON Lines format based on the proposal found at:
It includes parametric objects whose parameters allow selecting the
representation for parsed JSON objects (curly
or list
), JSON
text strings (atom
, chars
, or codes
) and JSON pairs
(dash
, equal
, or colon
).
API documentationï
Open the ../../apis/library_index.html#json_lines link in a web browser.
Loadingï
To load all entities in this library, load the loader.lgt
file:
| ?- logtalk_load(json_lines(loader)).
Testingï
To test this library predicates, load the tester.lgt
file:
| ?- logtalk_load(json_lines(tester)).
Some of the sample JSON test files are based on examples published at:
Representationï
The following choices of syntax have been made to represent JSON elements as terms:
By default, JSON objects are represented using curly-bracketed terms,
{Pairs}
, where each pair uses the representationKey-Value
(see below for alternative representations).Arrays are represented using lists.
Text strings can be represented as atoms,
chars(List)
, orcodes(List)
. The default when decoding is to use atoms when using thejson_lines
object. To decode text strings into lists of chars or codes, use thejson_lines/1
object with the parameter bound tochars
orcodes
. For example:| ?- json_lines::parse(codes([34,104,101,108,108,111,34]), Terms). Terms = [hello] yes | ?- json_lines(atom)::parse(codes([34,104,101,108,108,111,34]), Terms). Terms = [hello] yes | ?- json_lines(chars)::parse(codes([34,104,101,108,108,111,34]), Terms). Terms = [chars([h,e,l,l,o])] yes | ?- json_lines(codes)::parse(codes([34,104,101,108,108,111,34]), Terms). Terms = [codes([104,101,108,108,111])] yes
The JSON values
false
,true
andnull
are represented by, respectively, the@false
,@true
and@null
compound terms.
The following table exemplifies the term equivalents of JSON elements using default representations for objects, pairs, and strings:
JSON |
term |
---|---|
[1,2] |
[1,2] |
true |
@true |
false |
@false |
null |
@null |
-1 |
-1 |
[1.2345] |
[1.2345] |
[] |
[] |
[2147483647] |
[2147483647] |
[0] |
[0] |
[1234567890123456789] |
[1234567890123456789] |
[false] |
[@false] |
[-2147483648] |
[-2147483648] |
{âaâ:null,âfooâ:âbarâ} |
{a-@null, foo-bar} |
[2.225073858507201e-308] |
[2.225073858507201e-308] |
[0,1] |
[0,1] |
[2.2250738585072014e-308] |
[2.2250738585072014e-308] |
[1.7976931348623157e+308] |
[1.7976931348623157e+308] |
[0.0] |
[0.0] |
[4294967295] |
[4294967295] |
[-1234567890123456789] |
[-1234567890123456789] |
[âfooâ] |
[foo] |
[1] |
[1] |
[null] |
[@null] |
[-1.2345] |
[-1.2345] |
[5.0e-324] |
[5.0e-324] |
[-1] |
[-1] |
[true] |
[@true] |
[9223372036854775807] |
[9223372036854775807] |
For JSON objects that are two possible term representations:
JSON object |
term (curly) |
---|---|
{âaâ:1, âbâ:2, âcâ:3} |
{a-1, b-2, c-3} |
{} |
{} |
and:
JSON object |
term (list) |
---|---|
{âaâ:1, âbâ:2, âcâ:3} |
json([a-1, b-2, c-3]) |
{} |
json([]) |
For JSON pairs that are three possible representations:
JSON object |
term (dash) |
---|---|
{âaâ:1, âbâ:2, âcâ:3} |
{a-1, b-2, c-3} |
and:
JSON object |
term (equal) |
---|---|
{âaâ:1, âbâ:2, âcâ:3} |
{a=1, b=2, c=3} |
and:
JSON object |
term (colon) |
---|---|
{âaâ:1, âbâ:2, âcâ:3} |
{a:1, b:2, c:3} |
By default, the curly-term representation and the dash pair
representation are used. The json/3
parametric object allows
selecting the desired representation choices. For example:
| ?- json_lines(curly,dash,atom)::parse(atom('{"a":1, "b":2, "c":3}'), JSONL).
JSONL = [{a-1, b-2, c-3}]
yes
| ?- json_lines(list,equal,atom)::parse(atom('{"a":1, "b":2, "c":3}'), JSONL).
JSONL = [json([a=1, b=2, c=3])]
yes
| ?- json_lines(curly,colon,atom)::parse(atom('{"a":1, "b":2, "c":3}'), JSONL).
JSONL = [{a:1, b:2, c:3}]
yes
Encodingï
Encoding is accomplished using the generate/2
predicate. For
example:
| ?- json_lines::generate(codes(Encoding), [a,{b-c}]).
Encoding = [34,97,34,10,123,34,98,34,58,34,99,34,125,10]
yes
Alternatively:
| ?- json_lines::generate(chars(Encoding), [a,{b-c}]).
Encoding = ['"',a,'"','\n','{','"',b,'"',:,'"',c,'"','}','\n']
Yes
| ?- json_lines::generate(atom(Encoding), [a,{b-c}]).
Encoding = '"a"\n{"b":"c"}\n'
Yes
Notice that generate/2
takes, as second argument, a Prolog term that
corresponds to the JSON syntax in Prolog and produces the corresponding
JSON output in the format specified in the first argument:
(codes(Variable)
, stream(Stream)
, file(File)
,
chars(Variable)
or atom(Variable)
).
Decodingï
Decoding is accomplished using the parse/2
predicate. For example,
to decode a given json file:
| ?- json_lines::parse(file('simple/data.jsonl'), Terms).
Term = [{a-[b]}]
yes
The parse/2
predicate first argument must indicate the input source
(codes(Codes)
, stream(Stream)
, line(Stream)
, file(Path)
,
chars(Chars)
or atom(Atom)
) containing a JSON payload to be
decoded into the Prolog term in the second argument.
Known issuesï
Some tests may fail on backends such as ECLiPSe and GNU Prolog that donât support Unicode.