Key/Value Pairs

I wanted to use library(error) to validate key/value pairs to be used with atom_json_term/3. However the pair type in library(error) has to be in the form of -/2 whereas json needs =/2. Does anyone know of a way to get library(error) pair to validate =/2 as a pair, or to have atom_json_term/3 to use -/2? It seems silly to convert a whole list from one form to the other simply to reconcile the different form of pairs. I would sooner make my own modified version of library(error) that considered =/2 as a pair.

IIRC library(error) has has_type/2 for use in adding additional types.

Also there is is_json_term/1,2 but that will only check the syntax and not the semantics.

For a semantic check of JSON library(quasi_quotations) is what I would consider.


pairs have a long standing tradition in Prolog to mean Key-Value and are supported by library(pairs) and builtin’s like keysort/2. I wouldn’t touch that.

Wondering what you are after though. atom_json_term/3 does raise an exception on invalid input. Why add your own check?

?- atom_json_term(A, json([a=b]), []).
A = '{"a":"b"}'.
?- atom_json_term(A, json([a+b]), []).
ERROR: Type error: `pair' expected, found `a+b' (a compound)
ERROR:   [11] with_output_to(atom(_14924),json_write(current_output,json(...),[]))

It’s not so much about atom_json_term/3. In fact I may not use that particular predicate at all. I need to validate a json/1 term, do some processing that will fetch external data also as a json/1 and validate the content of each key/value pair. library(error) provides most but not all of the validation process. I don’t want to write my own validation when must_be/2 and is_of_type/2 do so much of the work for me. I guess the next logical question is: “If Prolog’s tradition says Key-Value is the proper format, why doesn’t library(http/json) use it?”. Either way, I’ll deal with it.

This looks interesting. The docs don’t say much about it. It’s multifile. I can probably figure it out.

1 Like

I can infer from the source for has_type/2 how to use it.