Forbid retrieval of non-fully-instantiated json arrays
This commit is contained in:
parent
514bdf6d8f
commit
c79e3096fd
1 changed files with 58 additions and 2 deletions
|
@ -196,13 +196,69 @@ kvplist_getitem(Key, [_ | KVPs], Value) :-
|
|||
kvplist_getitem(Key, KVPs, Value).
|
||||
|
||||
|
||||
% nonvar_recursive/1: nonvar_recursive(Var)
|
||||
%
|
||||
% Extension of nonvar/1 that also checks for nonvar in lists and objects.
|
||||
% Used in order to respect the interesting project rule:
|
||||
%
|
||||
% ?- jsonaccess(jsonarray(_), [], _).
|
||||
% false.
|
||||
%
|
||||
% while still allowing, as later clarified on Moodle:
|
||||
%
|
||||
% ?- jsonaccess(jsonobj([("a", jsonarray([]))]), ["a"], A).
|
||||
% A = jsonarray([]).
|
||||
|
||||
nonvar_recursive(Var) :-
|
||||
var(Var),
|
||||
!,
|
||||
fail.
|
||||
|
||||
nonvar_recursive(jsonobj(KVPs)) :-
|
||||
!,
|
||||
nonvar(KVPs),
|
||||
nonvar_recursive(KVPs).
|
||||
|
||||
nonvar_recursive(jsonarray(Items)) :-
|
||||
!,
|
||||
nonvar(Items),
|
||||
nonvar_recursive(Items).
|
||||
|
||||
nonvar_recursive([H | T]) :-
|
||||
!,
|
||||
nonvar_recursive(H),
|
||||
nonvar_recursive(T).
|
||||
|
||||
nonvar_recursive(V) :-
|
||||
!,
|
||||
nonvar(V).
|
||||
|
||||
|
||||
% jsonaccess/3: jsonaccess(JsonObj, Fields, Result).
|
||||
%
|
||||
% Access a JSON object or array by a sequence of keys or indices.
|
||||
% Fields is either list of strings/ints, a string or an int
|
||||
|
||||
% Empty JSON path, computation is complete
|
||||
jsonaccess(JsonObj, [], JsonObj) :- !.
|
||||
% Empty JSON path, computation is complete; return only valid JSON objects
|
||||
jsonaccess(jsonobj(KVPList), [], jsonobj(KVPList)) :-
|
||||
!,
|
||||
nonvar_recursive(KVPList).
|
||||
|
||||
jsonaccess(jsonarray(Items), [], jsonarray(Items)) :-
|
||||
!,
|
||||
nonvar_recursive(Items).
|
||||
|
||||
jsonaccess(String, [], String) :-
|
||||
string(String),
|
||||
!.
|
||||
|
||||
jsonaccess(Number, [], Number) :-
|
||||
number(Number),
|
||||
!.
|
||||
|
||||
jsonaccess(true, [], true) :- !.
|
||||
jsonaccess(false, [], false) :- !.
|
||||
jsonaccess(null, [], null) :- !.
|
||||
|
||||
% Handle accessing a JSON array by index
|
||||
jsonaccess(jsonarray(JArr), [F | Fs], Result) :-
|
||||
|
|
Loading…
Add table
Reference in a new issue