在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:linked-data-dotnet/json-ld.net开源软件地址:https://github.com/linked-data-dotnet/json-ld.net开源编程语言:C# 99.2%开源软件介绍:json-ld.netIntroductionThis library is an implementation of the JSON-LD specification in C#. JSON, as specified in RFC7159, is a simple language for representing objects on the Web. Linked Data is a way of describing content across different documents or Web sites. Web resources are described using IRIs, and typically are dereferencable entities that may be used to find more information, creating a "Web of Knowledge". JSON-LD is intended to be a simple publishing method for expressing not only Linked Data in JSON, but for adding semantics to existing JSON. JSON-LD is designed as a light-weight syntax that can be used to express Linked Data. It is primarily intended to be a way to express Linked Data in JavaScript and other Web-based programming environments. It is also useful when building interoperable Web Services and when storing Linked Data in JSON-based document storage engines. It is practical and designed to be as simple as possible, utilizing the large number of JSON parsers and existing code that is in use today. It is designed to be able to express key-value pairs, RDF data, RDFa data, Microformats data, and Microdata. That is, it supports every major Web-based structured data model in use today. The syntax does not require many applications to change their JSON, but easily add meaning by adding context in a way that is either in-band or out-of-band. The syntax is designed to not disturb already deployed systems running on JSON, but provide a smooth migration path from plain JSON to semantically enhanced JSON. Finally, the format is intended to be fast to parse, fast to generate, stream-based and document-based processing compatible, and require a very small memory footprint in order to operate. You can read more about JSON-LD on the JSON-LD website. ConformanceThis library aims to conform with the following:
The [JSON-LD Working Group][json-ld-wg] is now developing JSON-LD 1.1. Library updates to conform with newer specifications will happen as features stabilize and development time and resources permit.
The test runner is often updated to note or skip newer tests that are not yet supported. Supported frameworks
Installationdotnet CLIdotnet new console
dotnet add package json-ld.net using JsonLD.Core;
using Newtonsoft.Json.Linq;
using System;
namespace JsonLD.Demo
{
internal class Program
{
private static void Main()
{
var json = "{'@context':{'test':'http://www.test.com/'},'test:hello':'world'}";
var document = JObject.Parse(json);
var expanded = JsonLdProcessor.Expand(document);
Console.WriteLine(expanded);
}
}
}
ExamplesExample data and context used throughout examples below: doc.json{
"@id": "http://example.org/ld-experts",
"http://schema.org/name": "LD Experts",
"http://schema.org/member": [{
"@type": "http://schema.org/Person",
"http://schema.org/name": "Manu Sporny",
"http://schema.org/url": {"@id": "http://manu.sporny.org/"},
"http://schema.org/image": {"@id": "http://manu.sporny.org/images/manu.png"}
}]
} context.json{
"name": "http://schema.org/name",
"member": "http://schema.org/member",
"homepage": {"@id": "http://schema.org/url", "@type": "@id"},
"image": {"@id": "http://schema.org/image", "@type": "@id"},
"Person": "http://schema.org/Person",
"@vocab": "http://example.org/",
"@base": "http://example.org/"
} CompactCompaction is the process of applying a developer-supplied context to shorten IRIs to terms or compact IRIs, and JSON-LD values expressed in expanded form to simple values such as strings or numbers. Often this makes it simpler to work with a document as the data is expressed in application-specific terms. Compacted documents are also typically easier to read for humans. var doc = JObject.Parse(_docJson);
var context = JObject.Parse(_contextJson);
var opts = new JsonLdOptions();
var compacted = JsonLdProcessor.Compact(doc, context, opts);
Console.WriteLine(compacted);
/*
Output:
{
"@id": "ld-experts",
"member": {
"@type": "Person",
"image": "http://manu.sporny.org/images/manu.png",
"name": "Manu Sporny",
"homepage": "http://manu.sporny.org/"
},
"name": "LD Experts",
"@context": . . .
}
*/ ExpandExapansion is the process of taking a JSON-LD document and applying a @context such that all IRIs, types, and values are expanded so that the @context is no longer necessary. var expanded = JsonLdProcessor.Expand(compacted);
Console.WriteLine(expanded);
/*
Output:
[
{
"@id": "http://test.com/ld-experts",
"http://schema.org/member": [
{
"http://schema.org/url": [
{
"@id": "http://manu.sporny.org/"
}
],
"http://schema.org/image": [
{
"@id": "http://manu.sporny.org/images/manu.png"
}
],
"http://schema.org/name": [
{
"@value": "Manu Sporny"
}
]
}
],
"http://schema.org/name": [
{
"@value": "LD Experts"
}
]
}
]
*/
*/ FlattenFlattening collects all properties of a node in a single JSON object and labels all blank nodes with blank node identifiers. This ensures a shape of the data and consequently may drastically simplify the code required to process JSON-LD in certain applications. var doc = JObject.Parse(_docJson);
var context = JObject.Parse(_contextJson);
var opts = new JsonLdOptions();
var flattened = JsonLdProcessor.Flatten(doc, context, opts);
Console.WriteLine(flattened);
/*
Output:
{
"@context": . . .,
"@graph": [
{
"@id": "_:b0",
"@type": "Person",
"image": "http://manu.sporny.org/images/manu.png",
"name": "Manu Sporny",
"homepage": "http://manu.sporny.org/"
},
{
"@id": "ld-experts",
"member": {
"@id": "_:b0"
},
"name": "LD Experts"
}
]
}
*/
FrameFraming is used to shape the data in a JSON-LD document, using an example frame document which is used to both match the flattened data and show an example of how the resulting data should be shaped. Matching is performed by using properties present in the frame to find objects in the data that share common values. Matching can be done either using all properties present in the frame, or any property in the frame. By chaining together objects using matched property values, objects can be embedded within one another. A frame also includes a context, which is used for compacting the resulting framed output. For the framing example below, the framing document is defined as follows: {
"@context": {
"name": "http://schema.org/name",
"member": {"@id": "http://schema.org/member", "@type": "@id"},
"homepage": {"@id": "http://schema.org/url", "@type": "@id"},
"image": {"@id": "http://schema.org/image", "@type": "@id"},
"Person": "http://schema.org/Person"
},
"@type": "Person"
} And we use it like this: var doc = JObject.Parse(_docJson);
var frame = JObject.Parse(_frameJson);
var opts = new JsonLdOptions();
var flattened = JsonLdProcessor.Frame(doc, frame, opts);
Console.WriteLine(flattened);
/*
Output:
{
"@context": . . .,
"@graph": [
{
"@id": "_:b0",
"@type": "Person",
"image": "http://manu.sporny.org/images/manu.png",
"name": "Manu Sporny",
"homepage": "http://manu.sporny.org/"
}
]
}
*/ NormalizeNormalization (aka. canonicalization) converts the document into a graph of objects that is a canonical representation of the document that can be used for hashing, comparison, etc. var doc = JObject.Parse(_docJson);
var opts = new JsonLdOptions();
var normalized = (RDFDataset)JsonLdProcessor.Normalize(doc, opts);
Console.WriteLine(normalized.Dump());
/*
Output:
@default
subject
type blank node
value _:c14n0
predicate
type IRI
value http://schema.org/image
object
type IRI
value http://manu.sporny.org/images/manu.png
---
subject
type blank node
value _:c14n0
predicate
type IRI
value http://schema.org/name
object
type literal
value Manu Sporny
datatype http://www.w3.org/2001/XMLSchema#string
---
subject
type blank node
value _:c14n0
predicate
type IRI
value http://schema.org/url
object
type IRI
value http://manu.sporny.org/
---
subject
type blank node
value _:c14n0
predicate
type IRI
value http://www.w3.org/1999/02/22-rdf-syntax-ns#type
object
type IRI
value http://schema.org/Person
---
subject
type IRI
value http://example.org/ld-experts
predicate
type IRI
value http://schema.org/member
object
type blank node
value _:c14n0
---
subject
type IRI
value http://example.org/ld-experts
predicate
type IRI
value http://schema.org/name
object
type literal
value LD Experts
datatype http://www.w3.org/2001/XMLSchema#string
---
*/ ToRDFJSON-LD is a concrete RDF syntax as described in RDF 1.1 Concepts and Abstract Syntax. Hence, a JSON-LD document is both an RDF document and a JSON document and correspondingly represents an instance of an RDF data model. The procedure to deserialize a JSON-LD document to an RDF dataset (and, optionally, to RDF N-Quads) involves the following steps:
The processor's var doc = JObject.Parse(_docJson);
var opts = new JsonLdOptions();
var rdf = (RDFDataset)JsonLdProcessor.ToRDF(doc, opts);
var serialized = RDFDatasetUtils.ToNQuads(rdf); // serialize RDF to string
Console.WriteLine(serialized);
/*
Output:
<http://example.org/ld-experts> <http://schema.org/member> _:b0 .
<http://example.org/ld-experts> <http://schema.org/name> "LD Experts" .
_:b0 <http://schema.org/image> <http://manu.sporny.org/images/manu.png> .
_:b0 <http://schema.org/name> "Manu Sporny" .
_:b0 <http://schema.org/url> <http://manu.sporny.org/> .
_:b0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://schema.org/Person> .
*/ or using a custom RDF renderer object, like this: private class JSONLDTripleCallback : IJSONLDTripleCallback
{
public object Call(RDFDataset dataset) =>
RDFDatasetUtils.ToNQuads(dataset); // serialize the RDF dataset as NQuads
}
internal static void Run()
{
var doc = JObject.Parse(_docJson);
var callback = new JSONLDTripleCallback();
var serialized = JsonLdProcessor.ToRDF(doc, callback);
Console.WriteLine(serialized);
/*
Output:
<http://example.org/ld-experts> <http://schema.org/member> _:b0 .
<http://example.org/ld-experts> <http://schema.org/name> "LD Experts" .
_:b0 <http://schema.org/image> <http://manu.sporny.org/images/manu.png> .
_:b0 <http://schema.org/name> "Manu Sporny" .
_:b0 <http://schema.org/url> <http://manu.sporny.org/> .
_:b0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://schema.org/Person> .
*/
} FromRDFSerialization from RDF N-Quads into JSON-LD can be thought of as the inverse of
the last of the three steps described in summary Deserialization described in
the In practice, it looks like this: the variable var opts = new JsonLdOptions();
var jsonld = JsonLdProcessor.FromRDF(serialized, opts);
Console.WriteLine(jsonld);
/*
Output:
[
{
"@id": "_:b0",
"http://schema.org/image": [
{
"@id": "http://manu.sporny.org/images/manu.png"
}
],
"http://schema.org/name": [
{
"@value": "Manu Sporny"
}
],
"http://schema.org/url": [
{
"@id": "http://manu.sporny.org/"
}
],
"@type": [
"http://schema.org/Person"
]
},
{
"@id": "http://example.org/ld-experts",
"http://schema.org/member": [
{
"@id": "_:b0"
}
],
"http://schema.org/name": [
{
"@value": "LD Experts"
}
]
}
]
*/ or using a custom RDF parser: private class CustomRDFParser : IRDFParser
{
public RDFDataset Parse(JToken input)
{
// by public decree, references to example.org are normalized to https going forward...
var converted = ((string)input).Replace("http://example.org/", "https://example.org/");
return RDFDatasetUtils.ParseNQuads(converted);
}
}
internal static void Run()
{
var parser = new CustomRDFParser();
var jsonld = JsonLdProcessor.FromRDF(_serialized, parser);
Console.WriteLine(jsonld);
/*
Output:
[
{
"@id": "_:b0",
"http://schema.org/image": [
{
"@id": "http://manu.sporny.org/images/manu.png"
}
],
"http://schema.org/name": [
{
"@value": "Manu Sporny"
}
],
"http://schema.org/url": [
{
"@id": "http://manu.sporny.org/"
}
],
"@type": [
"http://schema.org/Person"
]
},
{
"@id": "https://example.org/ld-experts",
"http://schema.org/member": [
{
"@id": "_:b0"
}
],
"http://schema.org/name": [
{
"@value": "LD Experts"
}
]
}
]
*/
} Custom DocumentLoaderBy replacing the default public class CustomDocumentLoader : DocumentLoader
{
private static readonly string _cachedExampleOrgContext = Res.ReadString("context.json");
public override RemoteDocument LoadDocument(string url)
{
if (url == "http://example.org/context.jsonld") // we have this cached locally
{
var doc = new JObject(new JProperty("@context", JObject.Parse(_cachedExampleOrgContext)));
return new RemoteDocument(url, doc);
}
else
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论