Did you know ... | Search Documentation: |
Packs (add-ons) for SWI-Prolog |
Title: | Declarative JSON |
---|---|
Rating: | Not rated. Create the first rating! |
Latest version: | 0.0.3 |
SHA1 sum: | 229df468231af3950db7e923868001c6cf5dfdd7 |
Author: | Michael Hendricks <michael@ndrix.org> |
Maintainer: | Michael Hendricks <michael@ndrix.org> |
Packager: | Michael Hendricks <michael@ndrix.org> |
Home page: | http://packs.ndrix.com/djson/index.html |
Download URL: | http://packs.ndrix.com/djson/djson-0.0.3.tgz |
Requires: | delay |
list_util |
No reviews. Create the first review!.
Version | SHA1 | #Downloads | URL |
---|---|---|---|
0.0.3 | 229df468231af3950db7e923868001c6cf5dfdd7 | 146 | http://packs.ndrix.com/djson/djson-0.0.3.tgz |
0.0.2 | ce2551118c773b8297a2781f955f0e80a808a9e1 | 3 | http://packs.ndrix.com/djson/djson-0.0.2.tgz |
0.0.1 | 31ffac35d1a5d36e4dc41225d4196d4d596bd7aa | 3 | http://packs.ndrix.com/djson/djson-0.0.1.tgz |
:- use_module(library(djson)). :- multifile djson:json//1. djson:json(point(X,Y)) --> json({ x: X, y: Y }). ?- json_term(Json, point(1,2)). Json = '{"x":1, "y":2}'. ?- json_term('{"y":2, "x":1}', Term). Term = {y:2, x:1} ; Term = json([y=2, x=1]) ; Term = point(1, 2) ; false. ?- json_term( '{"name":{"first":"John","last":"Doe"},"age":27}' , { name: { first: Name } } ). Name = 'John' ; false.
Easy conversion between JSON atoms and Prolog terms. Because the notation is nearly identical to JSON, one can copy and paste examples from API documentation, add variables and immediately have working code. The library also makes it easy to describe complex, nested JSON objects.
We also recognize the inherent ambiguity of JSON serialization. Because JSON rarely indicates an object's type, we can't be certain which Prolog term represents it best. Instead of making you choose one, library(djson)
bactracks over possible representations. This allows the context inherent in your code to clarify which representation is wanted.
It's common for web services to return JSON with a great deal of extraneous information. We rarely care about all of it. This library makes it easy to extract the pieces you want. The last example in the Synopsis above extracts a person's name from inside a nested JSON object. This pattern allows one to reuse predicates across different JSON documents, as long as they share the same structure.
Both library(http/json) and library(http/json_convert) facilitate conversion between JSON and Prolog terms.
Using SWI-Prolog 6.3 or later:
?- pack_install(djson).
This module uses semantic versioning.
Source code available and pull requests accepted at http://github.com/mndrix/djson
Pack contains 5 files holding a total of 8.9K bytes.