What is the easiest way to do streaming parsing of non-uniform JSON data using System.Text.Json?
I am trying to stream-parse JSON in the SPARQL JSON results format, e.g.:
{ "head": {
"vars": [ "s" , "p" , "o" ]
} ,
"results": {
"bindings": [
{
"s": { "type": "uri" , "value": "https://example.com/ontology/al/bnode/1" } ,
"p": { "type": "uri" , "value": "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" } ,
"o": { "type": "uri" , "value": "https://example.com/ontology/al/Module" }
} ,
{
"s": { "type": "bnode" , "value": "b0" } ,
"p": { "type": "uri" , "value": "http://www.w3.org/1999/02/22-rdf-syntax-ns#rest" } ,
"o": { "type": "uri" , "value": "http://www.w3.org/1999/02/22-rdf-syntax-ns#nil" }
} ,
{
"s": {
"type": "triple" ,
"value": {
"subject": { "type": "uri" , "value": "https://example.com/ontology/al/bnode/1798" } ,
"predicate": { "type": "uri" , "value": "https://example.com/ontology/al/type" } ,
"object": { "type": "uri" , "value": "https://example.com/ontology/al/bnode/6" }
}
} ,
"p": { "type": "uri" , "value": "https://example.com/ontology/al/dimensions" } ,
"o": { "type": "bnode" , "value": "b0" }
} ,
{
"s": { "type": "uri" , "value": "https://example.com/ontology/al/bnode/1" } ,
"p": { "type": "uri" , "value": "https://example.com/ontology/al/publisher" } ,
"o": { "type": "literal" , "value": "Example" }
}
]
}
}
The head/vars
mentions variables, which are specific to the query at hand and bound to specific values in the results/bindings
array.
Normally I would JsonSerializer.DeserializeAsyncEnumerable<T>
but since the top-level element is not an array, that does not seem workable. That the individual variable bindings are non-uniform is not a problem per se, as T
could be the JsonElement
type.
The data is comming from an HTTP stream. For a quasi-long-running query, it would give a better UX if the results could be incrementally integrated into the UI of the client application.