123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990(*********************************************************************************)(* OCaml-RDF *)(* *)(* Copyright (C) 2012-2021 Institut National de Recherche en Informatique *)(* et en Automatique. All rights reserved. *)(* *)(* This program is free software; you can redistribute it and/or modify *)(* it under the terms of the GNU Lesser General Public License version *)(* 3 as published by the Free Software Foundation. *)(* *)(* This program is distributed in the hope that it will be useful, *)(* but WITHOUT ANY WARRANTY; without even the implied warranty of *)(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *)(* GNU General Public License for more details. *)(* *)(* You should have received a copy of the GNU General Public License *)(* along with this program; if not, write to the Free Software *)(* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA *)(* 02111-1307 USA *)(* *)(* Contact: Maxence.Guesdon@inria.fr *)(* *)(*********************************************************************************)(** *)moduleYojson=Yojson.BasicexceptionUnexpected_jsonofstring*Yojson.tlet()=Printexc.register_printer(function|Unexpected_json(s,json)->Some(Printf.sprintf"Unexpected JSON for %S: %s"s(Yojson.to_stringjson))|_->None)letmk_termv=function|"uri"->Term.term_of_iri_stringv|"literal"->Term.(Literal(mk_literalv))|_whenString.lengthv!=0->Term.(Blank_(blank_id_of_stringv))|_->Term.Blankletterm_of_jsonjson=trylete_type=Yojson.Util.(to_string(member"type"json))inletvalue=Yojson.Util.(to_string(member"value"json))inmk_termvaluee_typewithYojson.Util.Type_error(s,_)->raise(Unexpected_json(s,json))(** {2 Serializing SPARQL Query Results in JSON}
[http://www.w3.org/TR/rdf-sparql-json-res/]
*)letcouple_of_jsonmu(name,j_term)=Sparql_ms.mu_addname(term_of_jsonj_term)muletsolution_of_jsonjson=Sparql.solution_of_mu(List.fold_leftcouple_of_jsonSparql_ms.mu_0(Yojson.Util.to_assocjson))letsolutions_of_jsonjson=List.mapsolution_of_json(Yojson.Util.to_listjson)(*
let string_of_json string_list json =
(Yojson.Util.to_string json)::string_list
let head_of_json json =
List.fold_left string_of_json [] (Yojson.Util.to_list json)
*)letsparql_result_of_jsonjson=matchYojson.Util.member"boolean"jsonwith`Boolb->Sparql.Boolb|`Null->begintryletresults_assoc=Yojson.Util.(member"results"json)inletbindings=Yojson.Util.(member"bindings"results_assoc)inSparql.Solutions(solutions_of_jsonbindings)withYojson.Util.Type_error(s,_)->raise(Unexpected_json(s,json))end|_->raise(Unexpected_json("query result",json));;