1/* Part of ClioPatria SeRQL and SPARQL server 2 3 Author: Michiel Hildebrand and Jan Wielemaker 4 E-mail: michielh@few.vu.nl 5 WWW: http://www.swi-prolog.org 6 Copyright (c) 2010-2018, E-Culture/MultimediaN 7 VU University Amsterdam 8 All rights reserved. 9 10 Redistribution and use in source and binary forms, with or without 11 modification, are permitted provided that the following conditions 12 are met: 13 14 1. Redistributions of source code must retain the above copyright 15 notice, this list of conditions and the following disclaimer. 16 17 2. Redistributions in binary form must reproduce the above copyright 18 notice, this list of conditions and the following disclaimer in 19 the documentation and/or other materials provided with the 20 distribution. 21 22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 26 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 27 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 29 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 30 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 32 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 POSSIBILITY OF SUCH DAMAGE. 34*/ 35 36:- module(rdf_json, 37 [ graph_json/2, % +Graph, -JSON 38 properties_to_json/2, % +Pairs:property-values, -JSONList 39 rdf_object_to_json/2 % +RDFObject, -JSONTerm 40 ]). 41:- use_module(library(semweb/rdf_db)). 42:- use_module(library(semweb/rdf_describe)). 43:- use_module(library(semweb/rdf_bnode)). 44:- use_module(library(pairs)). 45:- use_module(library(apply)). 46:- use_module(library(assoc)). 47:- use_module(library(http/json)). 48:- use_module(library(http/json_convert)). 49:- use_module(library(http/http_json)). 50 51/** <module> JSON Representation for RDF graphs 52 53@see http://n2.talis.com/wiki/RDF_JSON_Specification 54*/ 55 56%! graph_json(+Graph, -JSON) is det. 57% 58% JSON is a Prolog JSON object representing Graph. 59% 60% @param Graph is a list of rdf(S,P,O) 61% @see json_write/3 for serializing JSON 62 63graph_json(Graph, json(JSON)) :- 64 bnode_vars(Graph, Graph1, BNodes), 65 bnode_ids(BNodes, 1), 66 map_list_to_pairs(arg(1), Graph1, Pairs), 67 keysort(Pairs, Pairs1), 68 group_pairs_by_key(Pairs1, SubjectKeyed), 69 maplist(json_description, SubjectKeyed, JSON). 70 71json_description(S-RDF, Key=json(JSON)) :- 72 uri_key(S, Key, _Type), 73 maplist(po, RDF, POList), 74 keysort(POList, POSorted), 75 group_pairs_by_key(POSorted, PList), 76 properties_to_json(PList, JSON). 77 78po(rdf(_,P,O), P-O). 79 80bnode_ids([], _). 81bnode_ids([bnode(N)|T], N) :- 82 N2 is N + 1, 83 bnode_ids(T, N2). 84 85uri_key(bnode(NodeID), Key, bnode) :- 86 !, 87 atom_concat('_:', NodeID, Key). 88uri_key(BNode, _, _) :- 89 rdf_is_bnode(BNode), 90 !, 91 type_error(rdf_resource, BNode). 92uri_key(URI, URI, uri). 93 94%! graph_to_json(+Pairs:property-values, -JSON) is det. 95% 96% JSON is a prolog json term of grouped Pairs. 97 98properties_to_json([], []) :- !. 99properties_to_json([P-Vs|T], [P=JSON|Rest]) :- 100 objects_to_json(Vs, JSON), 101 properties_to_json(T, Rest). 102 103 104%! objects_to_json(+Objects:list(rdf_object), -JSON) is det. 105% 106% Convert all objects in Rs to a JSON term. 107 108objects_to_json([], []) :- !. 109objects_to_json([R|T], [json(JSON)|Vs]) :- 110 rdf_object_to_json(R, JSON), 111 objects_to_json(T, Vs). 112 113 114%! rdf_object_to_json(+RDFObject, -JSON) is det. 115% 116% Convert an RDF Object to a JSON term. RDFObject can be a term 117% bnode(NodeID). RDFObject is *not* allowed to be a blank-node. 118% 119% @error type_error(rdf_resource, BNode) if RDFObject is a 120% blank node. 121 122rdf_object_to_json(literal(Lit), Object) :- 123 !, 124 Object = [value=Txt, type=literal|Rest], 125 literal_to_json(Lit, Txt, Rest). 126rdf_object_to_json(URI, [value=Key, type=Type]) :- 127 uri_key(URI, Key, Type). 128 129%! literal_to_json(+Literal, -Text, -Attributes) 130% 131% Extract text and Attributes from Literal object. 132 133literal_to_json(lang(Lang, Txt), Txt, [lang=Lang]) :- !. 134literal_to_json(type(Type, Txt), Txt, [datatype=Type]) :- !. 135literal_to_json(Txt, Txt, [])