| 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(library(lists)). |
| 30 | | |
| 31 | | get_json_key_object(Key,JSON,Object) :- Key = [_|_],!, |
| 32 | | add_internal_error('Illegal Key: ',get_json_key_object(Key,JSON,Object)),fail. |
| 33 | | get_json_key_object(Key,JSON,Object) :- |
| 34 | ? | get_json_attribute(Key,JSON,JObject), |
| 35 | | JObject \= @(null), |
| 36 | | (JObject = json(Object) -> true |
| 37 | | ; add_internal_error('Illegal JSON object for key:',Key:JObject), |
| 38 | | fail |
| 39 | | ). |
| 40 | | |
| 41 | | get_json_key_list(Key,JSON,Object) :- Key = [_|_],!, |
| 42 | | add_internal_error('Illegal Key: ',get_json_key_list(Key,JSON,Object)),fail. |
| 43 | | get_json_key_list(Key,JSON,List) :- |
| 44 | ? | get_json_attribute(Key,JSON,JList), |
| 45 | | JList \= @(null), |
| 46 | | (JList = array(List) -> true |
| 47 | | ; add_internal_error('Illegal JSON list for key:',Key:JList), |
| 48 | | fail |
| 49 | | ). |
| 50 | | |
| 51 | | |
| 52 | ? | get_json_attribute(Attr,ObjList,Value) :- member(Equality,ObjList), |
| 53 | | is_json_equality_attr(Equality,Attr,Value). |
| 54 | | |
| 55 | | get_json_attribute_with_pos(Attr,ObjList,File,Value,Pos) :- |
| 56 | ? | member(Equality,ObjList), |
| 57 | | is_json_equality_attr_with_pos(Equality,File,Attr,Value,Pos). |
| 58 | | |
| 59 | | get_optional_json_attribute_with_pos(Attr,File,Value,Pos,ObjList,RemainingObjList) :- |
| 60 | ? | select(Equality,ObjList,RemainingObjList), |
| 61 | | is_json_equality_attr_with_pos(Equality,File,Attr,V,P),!, |
| 62 | | Value=V, Pos=P. |
| 63 | | get_optional_json_attribute_with_pos(_,_,@(null),unknown,AttrList,AttrList). |
| 64 | | |
| 65 | | |
| 66 | | get_optional_json_string_attribute_with_pos(Attr,File,StringValue,Pos,ObjList,RemObjList) :- |
| 67 | | get_optional_json_attribute_with_pos(Attr,File,Value,Pos,ObjList,RemObjList), |
| 68 | | extract_json_string(Value,Attr,Pos,StringValue). |
| 69 | | |
| 70 | | extract_json_string(string(S),_,_,Res) :- !, Res=S. |
| 71 | | extract_json_string(@(null),_,_,Res) :- !, Res = @(null). |
| 72 | | extract_json_string(Obj,Attr,Pos,_) :- |
| 73 | | add_error(simb,'Illegal JSON string value for attribute: ',Attr,Pos), |
| 74 | | write(Obj),nl, |
| 75 | | fail. |
| 76 | | |
| 77 | | % an array of strings or a single string returned as list |
| 78 | | get_optional_json_strings_attribute_with_pos(Attr,File,StringValue,Pos,ObjList,RemObjList) :- |
| 79 | | get_optional_json_attribute_with_pos(Attr,File,Value,Pos,ObjList,RemObjList), |
| 80 | | extract_json_strings(Value,Attr,Pos,StringValue). |
| 81 | | |
| 82 | | extract_json_strings(string(S),_,_,Res) :- !, Res=[S]. |
| 83 | | extract_json_strings(@(null),_,_,Res) :- !, Res = []. |
| 84 | | extract_json_strings(array(A),Attr,Pos,Res) :- !, |
| 85 | | findall(V,(member(AA,A), extract_json_string(AA,Attr,Pos,V)),Res). |
| 86 | | extract_json_strings(Obj,Attr,Pos,_) :- |
| 87 | | add_error(simb,'Illegal JSON string or string array value for attribute: ',Attr,Pos), |
| 88 | | write(Obj),nl, |
| 89 | | fail. |
| 90 | | |
| 91 | | |
| 92 | | get_optional_json_array_attribute_with_pos(Attr,File,ArrayList,Pos,ObjList,RemObjList) :- |
| 93 | | get_optional_json_attribute_with_pos(Attr,File,Value,Pos,ObjList,RemObjList), |
| 94 | | extract_json_array(Value,ArrayList). |
| 95 | | |
| 96 | | extract_json_array(@(null),Res) :- !, Res = []. |
| 97 | | extract_json_array(array(A),Res) :- !, Res = A. |
| 98 | | extract_json_array(Obj,Res) :- !, Res=[Obj]. |
| 99 | | |
| 100 | | |
| 101 | | get_optional_json_number_attribute_with_pos(Attr,File,NrValue,Pos,ObjList,RemObjList) :- |
| 102 | | get_optional_json_attribute_with_pos(Attr,File,Value,Pos,ObjList,RemObjList), |
| 103 | | extract_json_number(Value,Attr,Pos,NrValue). |
| 104 | | |
| 105 | | extract_json_number(number(S),_,_,Res) :- !, Res=S. |
| 106 | | extract_json_number(@(null),_,_,Res) :- !, Res = @(null). |
| 107 | | extract_json_number(Obj,Attr,Pos,_) :- |
| 108 | | add_error(simb,'Illegal JSON number value for attribute: ',Attr,Pos), |
| 109 | | write(Obj),nl, |
| 110 | | fail. |
| 111 | | |
| 112 | | |
| 113 | | is_json_equality_attr('='(Attr,Val),Attr,Val). |
| 114 | | is_json_equality_attr('='(Attr,Val,_Pos),Attr,Val). % we have position infos |
| 115 | | |
| 116 | | is_json_equality_attr_with_pos('='(Attr,Val),_File,Attr,Val,unknown). |
| 117 | | is_json_equality_attr_with_pos('='(Attr,Val,JPos),File,Attr,Val,ProBPos) :- create_position(JPos,File,ProBPos). |
| 118 | | |
| 119 | | create_position(From-To,File,ProBPos) :- |
| 120 | | ProBPos=src_position_with_filename_and_ec(From,1,To,1,File). |