forked from SWI-Prolog/packages-pengines
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterm_to_json.pl
127 lines (113 loc) · 4.15 KB
/
term_to_json.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/* Part of SWI-Prolog
Author: Torbjörn Lager and Jan Wielemaker
E-mail: [email protected]
WWW: http://www.swi-prolog.org
Copyright (c) 2014-2015, Torbjörn Lager,
VU University Amsterdam
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
:- module(term_to_json,
[ term_to_json/3, % +Term, +Bindings, -Json
term_to_json/2 % +Term, -Json
]).
:- use_module(library(apply)).
:- use_module(library(error)).
%% term_to_json(+Term, +Bindings, -JsonTerm) is det.
%% term_to_json(+Term, -JsonTerm) is det.
%
% Convert any general Prolog term into a JSON term. Prolog lists
% are treated in a special way. Also, JSON terms are not
% converted. Mapping:
%
% * Variable: =|{"type":"var", "name":<string>}|=
% * Atom: =|{"type":"atom", "value":<string>}|=
% * Integer: =|{"type":"integer", "value":<integer>}|=
% * Float: =|{"type":"float", "value":<float>}|=
% * List: JSON array
% * Dict: a JSON object. Values are processed recursively.
% (the tag is ignored)
% * json([Key=Value, ...]): a JSON object Values are processed
% recursively.
% * compound: =|{"type":"compound", "functor":<string>, "args":<array>}|=
%
% @param Bindings is a list of Name=Var terms for variables that
% get their name from the environment.
term_to_json(Term, JSON) :-
term_to_json(Term, [], JSON).
term_to_json(Term, Bindings, JSON) :-
findall(X,
( maplist(bind_var, Bindings),
numbervars(Term, 0, _, [singletons(true)]),
to_json(Term, X)
),
[JSON]).
bind_var(Name=Var) :-
( var(Var)
-> Var = '$VAR'(Name)
; true
).
to_json(Term, '_') :-
var(Term), !.
to_json(@(Symbol), Symbol) :- % compatibility
atom(Symbol),
json_symbol(Symbol), !.
to_json(Term, Term) :-
atom(Term), !. % interpreted as a string
to_json(Term, Term) :-
string(Term), !.
to_json(Term, Value) :-
integer(Term), !,
( Term >= -(2**31), Term < 2**31
-> Value = Term
; atom_number(Value, Term)
).
to_json(Term, Term) :-
float(Term), !.
to_json(Term, JsonList) :-
is_list(Term), !,
maplist(to_json, Term, JsonList).
to_json(json(Pairs0), Term) :-
must_be(list, Pairs0),
maplist(pair_value_to_json_ex, Pairs0, Pairs),
dict_pairs(Term, json, Pairs).
to_json(Term0, Term) :-
is_dict(Term0), !,
dict_pairs(Term0, Tag, Pairs0),
maplist(pair_value_to_json, Pairs0, Pairs),
dict_pairs(Term, Tag, Pairs).
to_json('$VAR'(Name), VarName) :- !,
format(string(VarName), '~W', ['$VAR'(Name), [numbervars(true)]]).
to_json(Term, json{functor:F, args:JsonArgs}) :-
Term =.. [F|Args],
maplist(to_json, Args, JsonArgs).
json_symbol(null).
json_symbol(true).
json_symbol(false).
pair_value_to_json(Key-Value0, Key-Value) :-
to_json(Value0, Value).
pair_value_to_json_ex(Key=Value0, Key-Value) :-
(atom(Key) ; integer(Key)), !,
to_json(Value0, Value).
pair_value_to_json_ex(Elem, _) :-
domain_error(json_key_value, Elem).