/// <summary>Creates an array of RDF triples for the given graph.</summary> /// <remarks>Creates an array of RDF triples for the given graph.</remarks> /// <param name="graph">the graph to create RDF triples for.</param> internal virtual void GraphToRDF(string graphName, JObject graph ) { // 4.2) IList <RDFDataset.Quad> triples = new List <RDFDataset.Quad>(); // 4.3) IEnumerable <string> subjects = graph.GetKeys(); // Collections.sort(subjects); foreach (string id in subjects) { if (JsonLdUtils.IsRelativeIri(id)) { continue; } JObject node = (JObject)graph[id]; JArray properties = new JArray(node.GetKeys()); properties.SortInPlace(); foreach (string property in properties) { var localProperty = property; JArray values; // 4.3.2.1) if ("@type".Equals(localProperty)) { values = (JArray)node["@type"]; localProperty = JSONLDConsts.RdfType; } else { // 4.3.2.2) if (JsonLdUtils.IsKeyword(localProperty)) { continue; } else { // 4.3.2.3) if (localProperty.StartsWith("_:") && !api.opts.GetProduceGeneralizedRdf()) { continue; } else { // 4.3.2.4) if (JsonLdUtils.IsRelativeIri(localProperty)) { continue; } else { values = (JArray)node[localProperty]; } } } } RDFDataset.Node subject; if (id.IndexOf("_:") == 0) { // NOTE: don't rename, just set it as a blank node subject = new RDFDataset.BlankNode(id); } else { subject = new RDFDataset.IRI(id); } // RDF predicates RDFDataset.Node predicate; if (localProperty.StartsWith("_:")) { predicate = new RDFDataset.BlankNode(localProperty); } else { predicate = new RDFDataset.IRI(localProperty); } foreach (JToken item in values) { // convert @list to triples if (JsonLdUtils.IsList(item)) { JArray list = (JArray)((JObject)item)["@list"]; RDFDataset.Node last = null; RDFDataset.Node firstBNode = nil; if (!list.IsEmpty()) { last = ObjectToRDF(list[list.Count - 1]); firstBNode = new RDFDataset.BlankNode(api.GenerateBlankNodeIdentifier()); } triples.Add(new RDFDataset.Quad(subject, predicate, firstBNode, graphName)); for (int i = 0; i < list.Count - 1; i++) { RDFDataset.Node @object = ObjectToRDF(list[i]); triples.Add(new RDFDataset.Quad(firstBNode, first, @object, graphName)); RDFDataset.Node restBNode = new RDFDataset.BlankNode(api.GenerateBlankNodeIdentifier ()); triples.Add(new RDFDataset.Quad(firstBNode, rest, restBNode, graphName)); firstBNode = restBNode; } if (last != null) { triples.Add(new RDFDataset.Quad(firstBNode, first, last, graphName)); triples.Add(new RDFDataset.Quad(firstBNode, rest, nil, graphName)); } } else { // convert value or node object to triple RDFDataset.Node @object = ObjectToRDF(item); if (@object != null) { triples.Add(new RDFDataset.Quad(subject, predicate, @object, graphName)); } } } } } this[graphName] = triples; }
/// <summary> /// Converts a JSON-LD value object to an RDF literal or a JSON-LD string or /// node object to an RDF resource. /// </summary> /// <remarks> /// Converts a JSON-LD value object to an RDF literal or a JSON-LD string or /// node object to an RDF resource. /// </remarks> /// <param name="item">the JSON-LD value or node object.</param> /// <param name="namer">the UniqueNamer to use to assign blank node names.</param> /// <returns>the RDF literal or RDF resource.</returns> private RDFDataset.Node ObjectToRDF(JToken item) { // convert value object to RDF if (JsonLdUtils.IsValue(item)) { JToken value = ((JObject)item)["@value"]; JToken datatype = ((JObject)item)["@type"]; // convert to XSD datatypes as appropriate if (value.Type == JTokenType.Boolean || value.Type == JTokenType.Float || value.Type == JTokenType.Integer) { // convert to XSD datatype if (value.Type == JTokenType.Boolean) { var serializeObject = JsonConvert.SerializeObject(value, Formatting.None).Trim('"'); return(new RDFDataset.Literal(serializeObject, datatype.IsNull() ? JSONLDConsts.XsdBoolean : (string)datatype, null)); } else { if (value.Type == JTokenType.Float || datatype.SafeCompare(JSONLDConsts.XsdDouble)) { // Workaround for Newtonsoft.Json's refusal to cast a JTokenType.Integer to a double. if (value.Type == JTokenType.Integer) { int number = (int)value; value = new JValue((double)number); } // canonical double representation return(new RDFDataset.Literal(string.Format(CultureInfo.InvariantCulture, "{0:0.0###############E0}", (double)value), datatype.IsNull() ? JSONLDConsts.XsdDouble : (string)datatype, null)); } else { return(new RDFDataset.Literal(string.Format("{0:0}", value), datatype.IsNull() ? JSONLDConsts.XsdInteger : (string)datatype, null)); } } } else { if (((JObject)item).ContainsKey("@language")) { return(new RDFDataset.Literal((string)value, datatype.IsNull() ? JSONLDConsts.RdfLangstring : (string)datatype, (string)((JObject)item)["@language"])); } else { var serializeObject = JsonConvert.SerializeObject(value, Formatting.None).Trim('"'); return(new RDFDataset.Literal(serializeObject, datatype.IsNull() ? JSONLDConsts.XsdString : (string)datatype, null)); } } } else { // convert string/node object to RDF string id; if (JsonLdUtils.IsObject(item)) { id = (string)((JObject)item)["@id"]; if (JsonLdUtils.IsRelativeIri(id)) { return(null); } } else { id = (string)item; } if (id.IndexOf("_:") == 0) { // NOTE: once again no need to rename existing blank nodes return(new RDFDataset.BlankNode(id)); } else { return(new RDFDataset.IRI(id)); } } }
/// <summary> /// Converts a JSON-LD value object to an RDF literal or a JSON-LD string or /// node object to an RDF resource. /// </summary> /// <remarks> /// Converts a JSON-LD value object to an RDF literal or a JSON-LD string or /// node object to an RDF resource. /// </remarks> /// <param name="item">the JSON-LD value or node object.</param> /// <param name="namer">the UniqueNamer to use to assign blank node names.</param> /// <returns>the RDF literal or RDF resource.</returns> private RDFDataset.Node ObjectToRDF(JToken item) { // convert value object to RDF if (JsonLdUtils.IsValue(item)) { JToken value = ((JObject)item)["@value"]; JToken datatype = ((JObject)item)["@type"]; // convert to XSD datatypes as appropriate if (value.Type == JTokenType.Boolean || value.Type == JTokenType.Float || value.Type == JTokenType.Integer) { // convert to XSD datatype if (value.Type == JTokenType.Boolean) { return(new RDFDataset.Literal(value.ToString(), datatype.IsNull() ? JSONLDConsts.XsdBoolean : (string)datatype, null)); } else { if (value.Type == JTokenType.Float || datatype.SafeCompare(JSONLDConsts.XsdDouble)) { // canonical double representation return(new RDFDataset.Literal(string.Format("{0:0.0###############E0}", value), datatype.IsNull() ? JSONLDConsts.XsdDouble : (string)datatype, null)); } else { return(new RDFDataset.Literal(string.Format("{0:0}", value), datatype.IsNull() ? JSONLDConsts.XsdInteger : (string)datatype, null)); } } } else { if (((JObject)item).ContainsKey("@language")) { return(new RDFDataset.Literal((string)value, datatype.IsNull() ? JSONLDConsts.RdfLangstring : (string)datatype, (string)((JObject)item)["@language"])); } else { return(new RDFDataset.Literal((string)value, datatype.IsNull() ? JSONLDConsts.XsdString : (string)datatype, null)); } } } else { // convert string/node object to RDF string id; if (JsonLdUtils.IsObject(item)) { id = (string)((JObject)item)["@id"]; if (JsonLdUtils.IsRelativeIri(id)) { return(null); } } else { id = (string)item; } if (id.IndexOf("_:") == 0) { // NOTE: once again no need to rename existing blank nodes return(new RDFDataset.BlankNode(id)); } else { return(new RDFDataset.IRI(id)); } } }