Quantcast

JSON-LD Serializer for XQuery -> RESTXQ

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

JSON-LD Serializer for XQuery -> RESTXQ

Christopher Johnson
Hi list,

I am considering how to serialize JSON-LD using RESTXQ and am wondering if anyone has implemented this already.  I guess that this would first involve extending the XQuerySerializer in eXist with a JSON-LD library.  

My main use case is that I am currently using RESTXQ to serialize JSON, but I would like to persist the xml namespaces for the elements in an @context.

Cheers,
Christopher Johnson

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Exist-open mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/exist-open
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: JSON-LD Serializer for XQuery -> RESTXQ

Joe Wicentowski
Hi Christopher,


Joe

On Thu, Apr 20, 2017 at 4:55 AM Christopher Johnson <[hidden email]> wrote:
Hi list,

I am considering how to serialize JSON-LD using RESTXQ and am wondering if anyone has implemented this already.  I guess that this would first involve extending the XQuerySerializer in eXist with a JSON-LD library.  

My main use case is that I am currently using RESTXQ to serialize JSON, but I would like to persist the xml namespaces for the elements in an @context.

Cheers,
Christopher Johnson
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot_______________________________________________
Exist-open mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/exist-open
--
Sent from my iPhone

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Exist-open mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/exist-open
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: JSON-LD Serializer for XQuery -> RESTXQ

Adam Retter
In reply to this post by Christopher Johnson
You could write your own serialization in XQuery and use the existing
text serializer, or as you say you would need to write your own
serializer in Java, the class to extend is
org.exist.util.serializer.XMLWriter and add an entry for it to
org.exist.util.serializer.AbstractSerializer

On 20 April 2017 at 04:53, Christopher Johnson <[hidden email]> wrote:

> Hi list,
>
> I am considering how to serialize JSON-LD using RESTXQ and am wondering if
> anyone has implemented this already.  I guess that this would first involve
> extending the XQuerySerializer in eXist with a JSON-LD library.
>
> My main use case is that I am currently using RESTXQ to serialize JSON, but
> I would like to persist the xml namespaces for the elements in an @context.
>
> Cheers,
> Christopher Johnson
>
> ------------------------------------------------------------------------------
> Check out the vibrant tech community on one of the world's most
> engaging tech sites, Slashdot.org! http://sdm.link/slashdot
> _______________________________________________
> Exist-open mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/exist-open
>



--
Adam Retter

eXist Developer
{ United Kingdom }
[hidden email]
irc://irc.freenode.net/existdb

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Exist-open mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/exist-open
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: JSON-LD Serializer for XQuery -> RESTXQ

Christopher Johnson
Hi Adam,

Thanks for the tip.  Serializing JSON-LD from an non-RDF/XML nodeset would be a whole lot easier if the org.xml.sax.XMLReader would simply allow elements with an "@".   If that were possible, then one could create elements like <@context/>, <@id> and <@type> in the XQuery output and the existing LD agnostic JSON serializer could be used (because JSON-LD is always valid JSON).   

I guess one way forward would be to first rewrite my local nodesets as RDF/XML with XQuery (very annoying!) and then post-process RDFS validated results with https://github.com/jsonld-java/jsonld-java-rdf2go in a custom serializer.  Adding an annotation that specifies output method "application/ld+json" to RESTXQ would then work only on valid RDF/XML.

Cheers,
Christopher  

On 20 April 2017 at 15:39, Adam Retter <[hidden email]> wrote:
You could write your own serialization in XQuery and use the existing
text serializer, or as you say you would need to write your own
serializer in Java, the class to extend is
org.exist.util.serializer.XMLWriter and add an entry for it to
org.exist.util.serializer.AbstractSerializer

On 20 April 2017 at 04:53, Christopher Johnson <[hidden email]> wrote:
> Hi list,
>
> I am considering how to serialize JSON-LD using RESTXQ and am wondering if
> anyone has implemented this already.  I guess that this would first involve
> extending the XQuerySerializer in eXist with a JSON-LD library.
>
> My main use case is that I am currently using RESTXQ to serialize JSON, but
> I would like to persist the xml namespaces for the elements in an @context.
>
> Cheers,
> Christopher Johnson
>
> ------------------------------------------------------------------------------
> Check out the vibrant tech community on one of the world's most
> engaging tech sites, Slashdot.org! http://sdm.link/slashdot
> _______________________________________________
> Exist-open mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/exist-open
>



--
Adam Retter

eXist Developer
{ United Kingdom }
[hidden email]
irc://irc.freenode.net/existdb


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Exist-open mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/exist-open
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: JSON-LD Serializer for XQuery -> RESTXQ

Adam Retter
> Thanks for the tip.  Serializing JSON-LD from an non-RDF/XML nodeset would
> be a whole lot easier if the org.xml.sax.XMLReader would simply allow
> elements with an "@".   If that were possible, then one could create
> elements like <@context/>, <@id> and <@type> in the XQuery output and the
> existing LD agnostic JSON serializer could be used (because JSON-LD is
> always valid JSON).

Well <@cotext/> etc is not valid XML, hence why the XMLReader won't
let you do it.

However if you know your mapping from XML to JSON-LD then you could
implement something at the same level as
org.exist.util.serializer.TEXTWriter to output your JSON-LD.


--
Adam Retter

eXist Developer
{ United Kingdom }
[hidden email]
irc://irc.freenode.net/existdb

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Exist-open mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/exist-open
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: JSON-LD Serializer for XQuery -> RESTXQ

Christopher Johnson
Sure, it is possible to write JSON-LD as text, but this is not a proper serialization of JSON.  There is valiid JSON and then there is plain text, and they are not the same.  It seems also not practical to pretend that JSON is text for the benefit of XQuery or XML.  For example, reading JSON as unparsed text into a nodeset using XSLT, and then sending the output to json:contents-to-json will not yield valid JSON (at least for me).  I should qualify that my interest is not in hacking JSON-LD to make it function within the functional limitations of XML (and the eXist db), but rather in providing a valid JSON-LD serialization (and RESTXQ) output method for any RDF/XML that could be stored in an eXist db collection.  I still suspect that this could be a relatively common use case that is supported by already well-developed libraries.

Cheers,
Christopher

On 21 April 2017 at 20:20, Adam Retter <[hidden email]> wrote:
> Thanks for the tip.  Serializing JSON-LD from an non-RDF/XML nodeset would
> be a whole lot easier if the org.xml.sax.XMLReader would simply allow
> elements with an "@".   If that were possible, then one could create
> elements like <@context/>, <@id> and <@type> in the XQuery output and the
> existing LD agnostic JSON serializer could be used (because JSON-LD is
> always valid JSON).

Well <@cotext/> etc is not valid XML, hence why the XMLReader won't
let you do it.

However if you know your mapping from XML to JSON-LD then you could
implement something at the same level as
org.exist.util.serializer.TEXTWriter to output your JSON-LD.


--
Adam Retter

eXist Developer
{ United Kingdom }
[hidden email]
irc://irc.freenode.net/existdb


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Exist-open mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/exist-open
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: JSON-LD Serializer for XQuery -> RESTXQ

Adam Retter
I think we might be talking at cross-purposes or certainly there is
some confusion.

> Sure, it is possible to write JSON-LD as text, but this is not a proper
> serialization of JSON.

>  There is valiid JSON and then there is plain text,
> and they are not the same.

In the end it is all just bytes. The XML Serializer is really
outputting "text" (compliant to the XML spec) from XML! If you wrote a
JSON-JD serializer in a similar for to the TEXTWriter, it would simply
take RDF/XML as input and output "text" (compliant to the JSON-JD
spec).

JSON is also a specification of a format of text ;-)

> For example, reading JSON as
> unparsed text into a nodeset using XSLT,

fn:unparsed-text does not deliver a nodeset, it delivers an xs:string.
https://www.w3.org/TR/xpath-functions-30/#func-unparsed-text

> and then sending the output to
> json:contents-to-json will not yield valid JSON (at least for me).

I see that json:contents-to-json does take a node(), I am not sure how
you are going from xs:string (from output from fn:unparsed-text) to a
node() though.

I am either missing something, or this shouldn't be possible.

> but rather in
> providing a valid JSON-LD serialization (and RESTXQ) output method for any
> RDF/XML that could be stored in an eXist db collection.  I still suspect
> that this could be a relatively common use case that is supported by already
> well-developed libraries.

Yes exactly. If you have XML as input (e.g. RDF/XML) then you can
create a serializer in Java by using XMLWriter/TEXTWriter etc as
examples, as they all take XML as input and then apply a mapping to
whatever output format (in your instance you would want a mapping to
JSON-LD).

I just realised that we already have a serialization writer for  a
basic mapping of XML -> JSON, see
org.exist.util.serializer.json.JSONWriter.

So you just need to create a
org.exist.util.serializer.json.JSONLDWriter which maps your XML/RDF to
your JSON-LD.



--
Adam Retter

eXist Developer
{ United Kingdom }
[hidden email]
irc://irc.freenode.net/existdb

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Exist-open mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/exist-open
Loading...