JSON encoder and decoder
Author(s): Jose F. Morales.This module defines a term representation for JSON (JavaScript Object Notation), as well as encoders and decoders.
This is an alpha version of the module. Use with care. Both the interface and implementation may change in the future.
Usage and interface
- Library usage:
:- use_module(library(json)). - Exports:
- Predicates:
json_to_string/2, string_to_json/2. - Regular Types:
json/1, json_attrs/1, json_attr/1, json_val/1, json_list/1.
- Predicates:
- Imports:
- System library modules:
strings. - Packages:
prelude, nonpure, assertions, regtypes, basicmodes, dcg.
- System library modules:
Documentation on exports
REGTYPE
Usage:
Attributes (pairs of key/value) of a JSON object.
json_attrs([]). json_attrs([X|Xs]) :- json_attr(X), json_attrs(Xs).
REGTYPE
A regular type, defined as follows:
json_val(string(X)) :- string(X). json_val(X) :- number(X). json_val(X) :- json(X). json_val(X) :- json_list(X). json_val(true). json_val(false). json_val(null).
PREDICATE
Usage:json_to_string(Term,String)
Encode a JSON Term as a character list.
- Call and exit should be compatible with:
(json:json/1)A JSON object.json(json(Attrs)) :- json_attrs(Attrs).
(basic_props:string/1)String is a string (a list of character codes). - The following properties should hold at call time:
(term_typing:nonvar/1)Term is currently a term which is not a free variable.
PREDICATE
Usage:string_to_json(String,Term)
Decode a character list as a JSON Term.
- Call and exit should be compatible with:
(basic_props:string/1)String is a string (a list of character codes).
(json:json/1)A JSON object.json(json(Attrs)) :- json_attrs(Attrs).
- The following properties should hold at call time:
(term_typing:nonvar/1)String is currently a term which is not a free variable.
Known bugs and planned improvements
- The grammar probably incomplete. See http://json.org for a complete reference.
- Missing tests. See http://json.org/example.html for many examples.