View source with formatted comments or as raw
    1/*  Part of ClioPatria SeRQL and SPARQL server
    2
    3    Author:        Michiel Hildebrand
    4    Author:        Jan Wielemaker
    5    E-mail:        michielh@few.vu.nl
    6    WWW:           http://www.swi-prolog.org
    7    Copyright (C): 2007-2010, E-Culture/MultimediaN
    8			      VU University Amsterdam
    9
   10    This program is free software; you can redistribute it and/or
   11    modify it under the terms of the GNU General Public License
   12    as published by the Free Software Foundation; either version 2
   13    of the License, or (at your option) any later version.
   14
   15    This program is distributed in the hope that it will be useful,
   16    but WITHOUT ANY WARRANTY; without even the implied warranty of
   17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18    GNU General Public License for more details.
   19
   20    You should have received a copy of the GNU General Public
   21    License along with this library; if not, write to the Free Software
   22    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23
   24    As a special exception, if you link this library with other files,
   25    compiled with a Free Software compiler, to produce an executable, this
   26    library does not by itself cause the resulting executable to be covered
   27    by the GNU General Public License. This exception does not however
   28    invalidate any other reasons why the executable file might be covered by
   29    the GNU General Public License.
   30*/
   31
   32
   33:- module(rdf_json,
   34	  [ graph_json/2,		% +Graph, -JSON
   35	    properties_to_json/2,	% +Pairs:property-values, -JSONList
   36	    rdf_object_to_json/2	% +RDFObject, -JSONTerm
   37	  ]).   38:- use_module(library(semweb/rdf_db)).   39:- use_module(library(semweb/rdf_describe)).   40:- use_module(library(semweb/rdf_bnode)).   41:- use_module(library(pairs)).   42:- use_module(library(apply)).   43:- use_module(library(assoc)).   44:- use_module(library(http/json)).   45:- use_module(library(http/json_convert)).   46:- use_module(library(http/http_json)).   47
   48/** <module> JSON Representation for RDF graphs
   49
   50@see	http://n2.talis.com/wiki/RDF_JSON_Specification
   51*/
   52
   53%%	graph_json(+Graph, -JSON) is det.
   54%
   55%	JSON is a Prolog JSON object representing Graph.
   56%
   57%	@param	Graph is a list of rdf(S,P,O)
   58%	@see	json_write/3 for serializing JSON
   59
   60graph_json(Graph, json(JSON)) :-
   61	bnode_vars(Graph, Graph1, BNodes),
   62	bnode_ids(BNodes, 1),
   63	map_list_to_pairs(arg(1), Graph1, Pairs),
   64	keysort(Pairs, Pairs1),
   65	group_pairs_by_key(Pairs1, SubjectKeyed),
   66	maplist(json_description, SubjectKeyed, JSON).
   67
   68json_description(S-RDF, Key=json(JSON)) :-
   69	uri_key(S, Key, _Type),
   70	maplist(po, RDF, POList),
   71	keysort(POList, POSorted),
   72	group_pairs_by_key(POSorted, PList),
   73	properties_to_json(PList, JSON).
   74
   75po(rdf(_,P,O), P-O).
   76
   77bnode_ids([], _).
   78bnode_ids([bnode(N)|T], N) :-
   79	N2 is N + 1,
   80	bnode_ids(T, N2).
   81
   82uri_key(bnode(NodeID), Key, bnode) :- !,
   83	atom_concat('_:', NodeID, Key).
   84uri_key(BNode, _, _) :-
   85	rdf_is_bnode(BNode), !,
   86	type_error(rdf_resource, BNode).
   87uri_key(URI, URI, uri).
   88
   89%%	graph_to_json(+Pairs:property-values, -JSON) is det.
   90%
   91%	JSON is a prolog json term of grouped Pairs.
   92
   93properties_to_json([], []) :- !.
   94properties_to_json([P-Vs|T], [P=JSON|Rest]) :-
   95	objects_to_json(Vs, JSON),
   96	properties_to_json(T, Rest).
   97
   98
   99%%	objects_to_json(+Objects:list(rdf_object), -JSON) is det.
  100%
  101%	Convert all objects in Rs to a JSON term.
  102
  103objects_to_json([], []) :- !.
  104objects_to_json([R|T], [json(JSON)|Vs]) :-
  105 	rdf_object_to_json(R, JSON),
  106	objects_to_json(T, Vs).
  107
  108
  109%%	rdf_object_to_json(+RDFObject, -JSON) is det.
  110%
  111%	Convert an RDF Object to a JSON   term.  RDFObject can be a term
  112%	bnode(NodeID). RDFObject is *not* allowed to be a blank-node.
  113%
  114%	@error	type_error(rdf_resource, BNode) if RDFObject is a
  115%		blank node.
  116
  117rdf_object_to_json(literal(Lit), Object) :- !,
  118	Object = [value=Txt, type=literal|Rest],
  119	literal_to_json(Lit, Txt, Rest).
  120rdf_object_to_json(URI, [value=Key, type=Type]) :-
  121	uri_key(URI, Key, Type).
  122
  123%%	literal_to_json(+Literal, -Text, -Attributes)
  124%
  125%	Extract text and Attributes from Literal object.
  126
  127literal_to_json(lang(Lang, Txt), Txt, [lang=Lang]) :- !.
  128literal_to_json(type(Type, Txt), Txt, [datatype=Type]) :- !.
  129literal_to_json(Txt, Txt, [])