IsValue() static private method

Returns true if the given value is a JSON-LD value
static private IsValue ( JToken v ) : bool
v JToken the value to check.
return bool
Ejemplo n.º 1
0
 /// <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));
         }
     }
 }
Ejemplo n.º 2
0
        /// <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 static JObject ObjectToRDF(JToken item, UniqueNamer namer)
        {
            JObject @object = new JObject();

            // convert value object to RDF
            if (JsonLdUtils.IsValue(item))
            {
                @object["type"] = "literal";
                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)
                    {
                        @object["value"]    = value.ToString();
                        @object["datatype"] = datatype.IsNull() ? JSONLDConsts.XsdBoolean : datatype;
                    }
                    else
                    {
                        if (value.Type == JTokenType.Float)
                        {
                            // canonical double representation
                            @object["value"]    = string.Format("{0:0.0###############E0}", (double)value);
                            @object["datatype"] = datatype.IsNull() ? JSONLDConsts.XsdDouble : datatype;
                        }
                        else
                        {
                            DecimalFormat df = new DecimalFormat("0");
                            @object["value"]    = df.Format((int)value);
                            @object["datatype"] = datatype.IsNull() ? JSONLDConsts.XsdInteger : datatype;
                        }
                    }
                }
                else
                {
                    if (((IDictionary <string, JToken>)item).ContainsKey("@language"))
                    {
                        @object["value"]    = value;
                        @object["datatype"] = datatype.IsNull() ? JSONLDConsts.RdfLangstring : datatype;
                        @object["language"] = ((IDictionary <string, JToken>)item)["@language"];
                    }
                    else
                    {
                        @object["value"]    = value;
                        @object["datatype"] = datatype.IsNull() ? JSONLDConsts.XsdString : datatype;
                    }
                }
            }
            else
            {
                // convert string/node object to RDF
                string id = JsonLdUtils.IsObject(item) ? (string)((JObject)item
                                                                  )["@id"] : (string)item;
                if (id.IndexOf("_:") == 0)
                {
                    @object["type"]  = "blank node";
                    @object["value"] = namer.GetName(id);
                }
                else
                {
                    @object["type"]  = "IRI";
                    @object["value"] = id;
                }
            }
            return(@object);
        }
Ejemplo n.º 3
0
 /// <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));
         }
     }
 }