1 % (c) 2021-2026 Lehrstuhl fuer Softwaretechnik und Programmiersprachen,
2 % Heinrich Heine Universitaet Duesseldorf
3 % This software is licenced under EPL 1.0 (http://www.eclipse.org/org/documents/epl-v10.html)
4
5 :- module(tools_json,
6 [get_json_key_object/3,
7 get_json_key_list/3,
8 get_json_attribute/3,
9 get_json_attribute_with_pos/5,
10 get_optional_json_attribute_with_pos/6,
11 get_optional_json_string_attribute_with_pos/6, extract_json_string/4,
12 get_optional_json_strings_attribute_with_pos/6,
13 get_optional_json_number_attribute_with_pos/6,
14 get_optional_json_array_attribute_with_pos/6,
15 is_json_equality_attr/3,
16 is_json_equality_attr_with_pos/5
17 ]).
18
19 :- use_module(module_information).
20
21 :- module_info(group,infrastructure).
22 :- module_info(description,'A few utilities for JSON terms.').
23 % terms created e.g. by
24 % :- use_module(extrasrc(json_parser),[json_parse_file/3]).
25 % json_parse_file(FileName,Term,[rest(_),position_infos(true),strings_as_atoms(false)]),
26
27 % TODO: include/merge with VisB utils like get_attr_with_pos, del_attr_with_pos
28 :- use_module(error_manager).
29 :- use_module(tools_strings,[ajoin/2]).
30 :- use_module(library(lists)).
31
32 get_json_key_object(Key,JSON,Object) :- Key = [_|_],!,
33 add_internal_error('Illegal Key: ',get_json_key_object(Key,JSON,Object)),fail.
34 get_json_key_object(Key,JSON,Object) :-
35 get_json_attribute(Key,JSON,JObject),
36 JObject \= @(null),
37 (JObject = json(Object) -> true
38 ; add_internal_error('Illegal JSON object for key:',Key:JObject),
39 fail
40 ).
41
42 get_json_key_list(Key,JSON,Object) :- Key = [_|_],!,
43 add_internal_error('Illegal Key: ',get_json_key_list(Key,JSON,Object)),fail.
44 get_json_key_list(Key,JSON,List) :-
45 get_json_attribute(Key,JSON,JList),
46 JList \= @(null),
47 (JList = array(List) -> true
48 ; add_internal_error('Illegal JSON list for key:',Key:JList),
49 fail
50 ).
51
52
53 get_json_attribute(Attr,ObjList,Value) :- member(Equality,ObjList),
54 is_json_equality_attr(Equality,Attr,Value).
55
56 get_json_attribute_with_pos(Attr,ObjList,File,Value,Pos) :-
57 member(Equality,ObjList),
58 is_json_equality_attr_with_pos(Equality,File,Attr,Value,Pos).
59
60 get_optional_json_attribute_with_pos(Attr,File,Value,Pos,ObjList,RemainingObjList) :-
61 ? select(Equality,ObjList,RemainingObjList),
62 is_json_equality_attr_with_pos(Equality,File,Attr,V,P),!,
63 Value=V, Pos=P.
64 get_optional_json_attribute_with_pos(_,_,@(null),unknown,AttrList,AttrList).
65
66
67 get_optional_json_string_attribute_with_pos(Attr,File,StringValue,Pos,ObjList,RemObjList) :-
68 get_optional_json_attribute_with_pos(Attr,File,Value,Pos,ObjList,RemObjList),
69 extract_json_string(Value,Attr,Pos,StringValue).
70
71 extract_json_string(string(S),_,_,Res) :- !, Res=S.
72 extract_json_string(@(null),_,_,Res) :- !, Res = @(null).
73 extract_json_string(Obj,Attr,Pos,_) :-
74 ajoin(['Illegal JSON string value for attribute ',Attr,': '],Msg),
75 add_error(json,Msg,Obj,Pos),
76 fail.
77
78 % an array of strings or a single string returned as list
79 get_optional_json_strings_attribute_with_pos(Attr,File,StringValue,Pos,ObjList,RemObjList) :-
80 get_optional_json_attribute_with_pos(Attr,File,Value,Pos,ObjList,RemObjList),
81 extract_json_strings(Value,Attr,Pos,StringValue).
82
83 extract_json_strings(string(S),_,_,Res) :- !, Res=[S].
84 extract_json_strings(@(null),_,_,Res) :- !, Res = [].
85 extract_json_strings(array(A),Attr,Pos,Res) :- !,
86 findall(V,(member(AA,A), extract_json_string(AA,Attr,Pos,V)),Res).
87 extract_json_strings(Obj,Attr,Pos,_) :-
88 ajoin(['Illegal JSON string or string array value for attribute ',Attr,': '],Msg),
89 add_error(json,Msg,Obj,Pos),
90 fail.
91
92
93 get_optional_json_array_attribute_with_pos(Attr,File,ArrayList,Pos,ObjList,RemObjList) :-
94 get_optional_json_attribute_with_pos(Attr,File,Value,Pos,ObjList,RemObjList),
95 extract_json_array(Value,ArrayList).
96
97 extract_json_array(@(null),Res) :- !, Res = [].
98 extract_json_array(array(A),Res) :- !, Res = A.
99 extract_json_array(Obj,Res) :- !, Res=[Obj].
100
101
102 get_optional_json_number_attribute_with_pos(Attr,File,NrValue,Pos,ObjList,RemObjList) :-
103 get_optional_json_attribute_with_pos(Attr,File,Value,Pos,ObjList,RemObjList),
104 extract_json_number(Value,Attr,Pos,NrValue).
105
106 extract_json_number(number(S),_,_,Res) :- !, Res=S.
107 extract_json_number(@(null),_,_,Res) :- !, Res = @(null).
108 extract_json_number(Obj,Attr,Pos,_) :-
109 ajoin(['Illegal JSON number value for attribute ',Attr,': '],Msg),
110 add_error(json,Msg,Obj,Pos),
111 fail.
112
113
114 is_json_equality_attr('='(Attr,Val),Attr,Val).
115 is_json_equality_attr('='(Attr,Val,_Pos),Attr,Val). % we have position infos
116
117 is_json_equality_attr_with_pos('='(Attr,Val),_File,Attr,Val,unknown).
118 is_json_equality_attr_with_pos('='(Attr,Val,JPos),File,Attr,Val,ProBPos) :- create_position(JPos,File,ProBPos).
119
120 create_position(From-To,File,ProBPos) :-
121 ProBPos=src_position_with_filename_and_ec(From,1,To,1,File).