Re: json_to_prolog help
Wouter Beek <[email protected]>
| Newsgroups | gmane.comp.ai.prolog.swi |
|---|---|
| Message-ID | <CAE1un7PzbW8O4SL-+qh9PTyngmomYZGjoUFmeGUntWKJ9qkJeA@mail.gmail.com> |
Hi Daniel,
Nice to hear that you're using SWI-Prolog for a production system!
I've been playing around with the JSON format in SWI-Prolog myself
recently. The problem is that JSON does not give the type of an object.
Luckily, the json_object directive accounts for that, since it allows you
to add a type.
For your code fragments, this would look as follows:
~~~{.pl}
:- json_object datum(raw:integer, eng:float) + [type=datum].
:- json_object reading_error(sensor_id:integer, rply_code:integer) +
[type=reading_error].
:- json_object reading_datum(sensor_id:integer, data:datum/2) +
[type=reading_datum].
:- json_object reading(passwd:integer, datetime:integer,
data:list(reading_datum/2)) + [type=reading].
~~~
Now when you do
`prolog_to_json(reading(1234,41234,[reading_datum(1,datum(2,3.3))]), JSON)`
you get:
~~~{.pl}
JSON =
json([passwd=1234,datetime=41234,data=[json([sensor_id=1,data=json([raw=2,eng=3.3,type=datum]),type=reading_datum])],type=reading])
~~~
Notice the types in there!
When you do `json_to_prolog(JSON,PL)` on this result, you get:
~~~{.pl}
PL = reading(1234,41234,[reading_datum(1,datum(2,3.3))])
~~~
The result that you were after (I hope :-)).
PS: Updating to a version >= 6.3.19 could be beneficial for you, since JSON
support received several updates in that release. Here's the relevant
improvements from the changelog:
~~~{.txt}
* FIXED: Disambiguating JSON->Prolog translation.
* MODIFIED: Make json_to_prolog/2 ignore fields in the json that are
not specified in the json_object/1 declaration. If multiple rules
now apply, the one producing the Prolog term with the highest arity
is selected.
* ADDED: library(http/json_convert) - disjunction of types using
(TypeA|TypeB) - null `type' to demand the key is not present -
Handle specified defaults as optional fields in the JSON input.
* FIXED: library(http/json_convert): properly handle types such as
list(atom).
~~~
---
Cheers!,
Wouter.
E-mail: [email protected]
WWW: www.wouterbeek.com
Tel.: 0647674624
On Mon, Jul 29, 2013 at 6:46 PM, Daniel Elliott <[email protected]>wrote:
> Hello,
>
> I am having some difficulties with the following MWE. I'm new to
> Prolog and this is a small part of a prototype which will, hopefully,
> make Prolog a significant part of our production system! I am
> uncertain if my approach is fundamentally flawed (not using the
> library correctly), if there is a error, or if I am running up on a
> limitation of the JSON support. I am using pl-6.2.6 which I
> downloaded and built.
>
> Here, I am working on code to parse JSON. The problem is that, in my
> example, it does not return a reading record but a json record (may
> not be the proper terminology).
>
> :- use_module(library(http/json)).
> :- use_module(library(http/json_convert)).
> :- json_object
> datum(raw:integer, eng:float).
> :- json_object
> reading_error(sensor_id:integer, rply_code:integer).
> :- json_object
> reading_datum(sensor_id:integer, data:datum/2).
> :- json_object
> reading(passwd:integer, datetime:integer, data:list(reading_datum/2)).
>
> Here is the result of two illustrative queries:
>
> ?-
> prolog_to_json(reading(1234,41234,[reading_datum(1,datum(2,3.3))]),JSON_Obj),
> format(user_output, '~w', JSON_Obj).
>
> json([passwd=1234,datetime=41234,data=[json([sensor_id=1,data=json([raw=2,eng=3.3])])]])
>
> ?-
> json_to_prolog(json([passwd=1234,datetime=41234,data=[json([sensor_id=1,data=json([raw=2,eng=3.3])])]]),PRLG_Obj).
> PRLG_Obj = json([passwd=1234, datetime=41234, data=[reading_datum(1,
> datum(2, 3.3))]]).
>
> I want PRLG_Obj to be reading(...), not json(....).
>
> Any help over this hump would be greatly appreciated. Thank you in
> advance.
>
> - dan elliott
> _______________________________________________
> SWI-Prolog mailing list
> [email protected]
> https://lists.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog
>
>
-------------- next part --------------
HTML attachment scrubbed and removed