Re: json_to_prolog help
Daniel Elliott <[email protected]>
| Newsgroups | gmane.comp.ai.prolog.swi |
|---|---|
| Message-ID | <CAD=Wj+YzA4M9EP+v5-X+pqA76AZYuy1AdNwN-yq6fi09ccAJOg@mail.gmail.com> |
Wouter, thank you for your response. It turns out that upgrade to 6.4
was all that I needed to make this work -- I do not need to include
the type information in the json_object specification (it works with
or without). My guess is that using the type thing is necessary only
when the json_object specifications are insufficient to be unique. Is
that your understanding?
Everyone, I have a follow-up question. Now that I have my lovely
reading from the json_to_prolog predicate, how do I use it? Here is
my example:
?-json_to_prolog(json([passwd='1234',datetime='41234',data=json([sensor_id=1,data=[json([raw=2,eng=3.3])]),json([sensor_id=2,data=json([raw=3,eng=4.3])])]]),PRLG_Obj).
PRLG_Obj = reading('1234', '41234', [reading_datum(1, datum(2, 3.3)),
reading_datum(2, datum(3, 4.3))], []).
I've tried using the access method laid out in the library(record)
documentation:
?- json_to_prolog(json([passwd='1234',datetime='41234',data=[json([sensor_id=1,data=json([raw=2,eng=3.3])]),json([sensor_id=2,data=json([raw=3,eng=4.3])])]]),PRLG_Obj),
reading_pass\
wd(PRLG_Obj,X).
ERROR: toplevel: Undefined procedure: reading_passwd/2 (DWIM could not
correct goal)
What, other than "reading_passwd(PRLG_Obj,X)" should I use to access
the data in the data structure returned by json_to_prolog.
Sorry for the bad formatting, the browser is wrecking havoc on the
code listing. Thank you for your time.
- dan
On Mon, Jul 29, 2013 at 12:37 PM, Wouter Beek <[email protected]> wrote:
> 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
>>
>