本文整理汇总了C#中Newtonsoft.Json.Linq.JObject.GetKeys方法的典型用法代码示例。如果您正苦于以下问题:C# JObject.GetKeys方法的具体用法?C# JObject.GetKeys怎么用?C# JObject.GetKeys使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Newtonsoft.Json.Linq.JObject
的用法示例。
在下文中一共展示了JObject.GetKeys方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ExpandLanguageMap
/// <summary>Expands a language map.</summary>
/// <remarks>Expands a language map.</remarks>
/// <param name="languageMap">the language map to expand.</param>
/// <returns>the expanded language map.</returns>
/// <exception cref="JsonLdError">JsonLdError</exception>
/// <exception cref="JsonLD.Core.JsonLdError"></exception>
internal static JArray ExpandLanguageMap(JObject languageMap
)
{
JArray rval = new JArray();
IList<string> keys = new List<string>(languageMap.GetKeys());
keys.SortInPlace();
// lexicographically sort languages
foreach (string key in keys)
{
JToken val;
if (!IsArray(languageMap[key]))
{
val = new JArray();
((JArray)val).Add(languageMap[key]);
}
else
{
val = (JArray)languageMap[key];
}
foreach (JToken item in val)
{
if (!IsString(item))
{
throw new JsonLdError(JsonLdError.Error.SyntaxError);
}
JObject tmp = new JObject();
tmp["@value"] = item;
tmp["@language"] = key.ToLower();
rval.Add(tmp);
}
}
return rval;
}
示例2: Resolve
/// <exception cref="JsonLD.Core.JsonLdError"></exception>
private static void Resolve(JToken input, JObject cycles)
{
Pattern regex = Pattern.Compile("(http|https)://(\\w+:{0,1}\\w*@)?(\\S+)(:[0-9]+)?(/|/([\\w#!:.?+=&%@!\\-/]))?"
);
if (cycles.Count > MaxContextUrls)
{
throw new JsonLdError(JsonLdError.Error.UnknownError);
}
// for tracking the URLs to resolve
JObject urls = new JObject();
// find all URLs in the given input
if (!FindContextUrls(input, urls, false))
{
// finished
FindContextUrls(input, urls, true);
}
// queue all unresolved URLs
JArray queue = new JArray();
foreach (string url in urls.GetKeys())
{
if (urls[url].SafeCompare(false))
{
// validate URL
if (!regex.Matcher(url).Matches())
{
throw new JsonLdError(JsonLdError.Error.UnknownError);
}
queue.Add(url);
}
}
// resolve URLs in queue
int count = queue.Count;
foreach (string url_1 in queue)
{
// check for context URL cycle
if (cycles.ContainsKey(url_1))
{
throw new JsonLdError(JsonLdError.Error.UnknownError);
}
JObject _cycles = (JObject)Clone(cycles);
_cycles[url_1] = true;
try
{
JObject ctx = (JObject)new DocumentLoader().LoadDocument(url_1).Document;
if (!ctx.ContainsKey("@context"))
{
ctx = new JObject();
ctx["@context"] = new JObject();
}
Resolve(ctx, _cycles);
urls[url_1] = ctx["@context"];
count -= 1;
if (count == 0)
{
FindContextUrls(input, urls, true);
}
}
//catch (JsonParseException)
//{
// throw new JsonLdError(JsonLdError.Error.UnknownError);
//}
//catch (MalformedURLException)
//{
// throw new JsonLdError(JsonLdError.Error.UnknownError);
//}
catch (IOException)
{
throw new JsonLdError(JsonLdError.Error.UnknownError);
}
}
}
示例3: Expand
/// <summary>
/// Expansion Algorithm
/// http://json-ld.org/spec/latest/json-ld-api/#expansion-algorithm
/// </summary>
/// <param name="activeCtx"></param>
/// <param name="activeProperty"></param>
/// <param name="element"></param>
/// <returns></returns>
/// <exception cref="JsonLdError">JsonLdError</exception>
/// <exception cref="JsonLD.Core.JsonLdError"></exception>
public virtual JToken Expand(Context activeCtx, string activeProperty, JToken element
)
{
// 1)
if (element.IsNull())
{
return null;
}
// 3)
if (element is JArray)
{
// 3.1)
JArray result = new JArray();
// 3.2)
foreach (JToken item in (JArray)element)
{
// 3.2.1)
JToken v = Expand(activeCtx, activeProperty, item);
// 3.2.2)
if (("@list".Equals(activeProperty) || "@list".Equals(activeCtx.GetContainer(activeProperty
))) && (v is JArray || (v is JObject && ((IDictionary<string, JToken>)v).ContainsKey
("@list"))))
{
throw new JsonLdError(JsonLdError.Error.ListOfLists, "lists of lists are not permitted."
);
}
else
{
// 3.2.3)
if (!v.IsNull())
{
if (v is JArray)
{
JsonLD.Collections.AddAll(result, (JArray)v);
}
else
{
result.Add(v);
}
}
}
}
// 3.3)
return result;
}
else
{
// 4)
if (element is JObject)
{
// access helper
IDictionary<string, JToken> elem = (JObject)element;
// 5)
if (elem.ContainsKey("@context"))
{
activeCtx = activeCtx.Parse(elem["@context"]);
}
// 6)
JObject result = new JObject();
// 7)
JArray keys = new JArray(element.GetKeys());
keys.SortInPlace();
foreach (string key in keys)
{
JToken value = elem[key];
// 7.1)
if (key.Equals("@context"))
{
continue;
}
// 7.2)
string expandedProperty = activeCtx.ExpandIri(key, false, true, null, null);
JToken expandedValue = null;
// 7.3)
if (expandedProperty == null || (!expandedProperty.Contains(":") && !JsonLdUtils.IsKeyword
(expandedProperty)))
{
continue;
}
// 7.4)
if (JsonLdUtils.IsKeyword(expandedProperty))
{
// 7.4.1)
if ("@reverse".Equals(activeProperty))
{
throw new JsonLdError(JsonLdError.Error.InvalidReversePropertyMap, "a keyword cannot be used as a @reverse propery"
);
}
// 7.4.2)
if (result.ContainsKey(expandedProperty))
//.........这里部分代码省略.........
示例4: GraphToRDF
internal static JArray GraphToRDF(JObject graph, UniqueNamer
namer)
{
// use RDFDataset.graphToRDF
JArray rval = new JArray();
foreach (string id in graph.GetKeys())
{
JObject node = (JObject)graph[id];
JArray properties = new JArray(node.GetKeys());
properties.SortInPlace();
foreach (string property in properties)
{
var eachProperty = property;
JToken items = node[eachProperty];
if ("@type".Equals(eachProperty))
{
eachProperty = JSONLDConsts.RdfType;
}
else
{
if (JsonLdUtils.IsKeyword(eachProperty))
{
continue;
}
}
foreach (JToken item in (JArray)items)
{
// RDF subjects
JObject subject = new JObject();
if (id.IndexOf("_:") == 0)
{
subject["type"] = "blank node";
subject["value"] = namer.GetName(id);
}
else
{
subject["type"] = "IRI";
subject["value"] = id;
}
// RDF predicates
JObject predicate = new JObject();
predicate["type"] = "IRI";
predicate["value"] = eachProperty;
// convert @list to triples
if (JsonLdUtils.IsList(item))
{
ListToRDF((JArray)((JObject)item)["@list"], namer, subject
, predicate, rval);
}
else
{
// convert value or node object to triple
object @object = ObjectToRDF(item, namer);
IDictionary<string, object> tmp = new Dictionary<string, object>();
tmp["subject"] = subject;
tmp["predicate"] = predicate;
tmp["object"] = @object;
rval.Add(tmp);
}
}
}
}
return rval;
}
示例5: ToRDF
/// <summary>Adds RDF triples for each graph in the given node map to an RDF dataset.
/// </summary>
/// <remarks>Adds RDF triples for each graph in the given node map to an RDF dataset.
/// </remarks>
/// <returns>the RDF dataset.</returns>
/// <exception cref="JsonLdError">JsonLdError</exception>
/// <exception cref="JsonLD.Core.JsonLdError"></exception>
public virtual RDFDataset ToRDF()
{
// TODO: make the default generateNodeMap call (i.e. without a
// graphName) create and return the nodeMap
JObject nodeMap = new JObject();
nodeMap["@default"] = new JObject();
GenerateNodeMap(this.value, nodeMap);
RDFDataset dataset = new RDFDataset(this);
foreach (string graphName in nodeMap.GetKeys())
{
// 4.1)
if (JsonLdUtils.IsRelativeIri(graphName))
{
continue;
}
JObject graph = (JObject)nodeMap[graphName
];
dataset.GraphToRDF(graphName, graph);
}
return dataset;
}
示例6: FromRDF
/// <summary>Converts RDF statements into JSON-LD.</summary>
/// <remarks>Converts RDF statements into JSON-LD.</remarks>
/// <param name="statements">the RDF statements.</param>
/// <param name="options">the RDF conversion options.</param>
/// <param name="callback">(err, output) called once the operation completes.</param>
/// <exception cref="JSONLDProcessingError">JSONLDProcessingError</exception>
/// <exception cref="JsonLD.Core.JsonLdError"></exception>
public virtual JArray FromRDF(RDFDataset dataset)
{
// 1)
JObject defaultGraph = new JObject();
// 2)
JObject graphMap = new JObject();
graphMap["@default"] = defaultGraph;
// 3/3.1)
foreach (string name in dataset.GraphNames())
{
IList<RDFDataset.Quad> graph = dataset.GetQuads(name);
// 3.2+3.4)
JObject nodeMap;
if (!graphMap.ContainsKey(name))
{
nodeMap = new JObject();
graphMap[name] = nodeMap;
}
else
{
nodeMap = (JObject)graphMap[name];
}
// 3.3)
if (!"@default".Equals(name) && !Obj.Contains(defaultGraph, name))
{
defaultGraph[name] = new JsonLdApi.NodeMapNode(this, name);
}
// 3.5)
foreach (RDFDataset.Quad triple in graph)
{
string subject = triple.GetSubject().GetValue();
string predicate = triple.GetPredicate().GetValue();
RDFDataset.Node @object = triple.GetObject();
// 3.5.1+3.5.2)
JsonLdApi.NodeMapNode node;
if (!nodeMap.ContainsKey(subject))
{
node = new JsonLdApi.NodeMapNode(this, subject);
nodeMap[subject] = node;
}
else
{
node = (NodeMapNode)nodeMap[subject];
}
// 3.5.3)
if ((@object.IsIRI() || @object.IsBlankNode()) && !nodeMap.ContainsKey(@object.GetValue
()))
{
nodeMap[@object.GetValue()] = new JsonLdApi.NodeMapNode(this, @object.GetValue());
}
// 3.5.4)
if (JSONLDConsts.RdfType.Equals(predicate) && (@object.IsIRI() || @object.IsBlankNode
()) && !opts.GetUseRdfType())
{
JsonLdUtils.MergeValue(node, "@type", @object.GetValue());
continue;
}
// 3.5.5)
JObject value = @object.ToObject(opts.GetUseNativeTypes());
// 3.5.6+7)
JsonLdUtils.MergeValue(node, predicate, value);
// 3.5.8)
if (@object.IsBlankNode() || @object.IsIRI())
{
// 3.5.8.1-3)
((NodeMapNode)nodeMap[@object.GetValue()]).usages.Add(new JsonLdApi.UsagesNode(this, node, predicate
, value));
}
}
}
// 4)
foreach (string name_1 in graphMap.GetKeys())
{
JObject graph = (JObject)graphMap[name_1];
// 4.1)
if (!graph.ContainsKey(JSONLDConsts.RdfNil))
{
continue;
}
// 4.2)
JsonLdApi.NodeMapNode nil = (NodeMapNode)graph[JSONLDConsts.RdfNil];
// 4.3)
foreach (JsonLdApi.UsagesNode usage in nil.usages)
{
// 4.3.1)
JsonLdApi.NodeMapNode node = usage.node;
string property = usage.property;
JObject head = usage.value;
// 4.3.2)
JArray list = new JArray();
JArray listNodes = new JArray();
// 4.3.3)
while (JSONLDConsts.RdfRest.Equals(property) && node.IsWellFormedListNode())
//.........这里部分代码省略.........
示例7: FilterNode
/// <exception cref="JsonLD.Core.JsonLdError"></exception>
private bool FilterNode(JsonLdApi.FramingContext state, JObject node, JObject frame)
{
JToken types = frame["@type"];
if (!types.IsNull())
{
if (!(types is JArray))
{
throw new JsonLdError(JsonLdError.Error.SyntaxError, "frame @type must be an array"
);
}
JToken nodeTypes = node["@type"];
if (nodeTypes.IsNull())
{
nodeTypes = new JArray();
}
else
{
if (!(nodeTypes is JArray))
{
throw new JsonLdError(JsonLdError.Error.SyntaxError, "node @type must be an array"
);
}
}
if (((JArray)types).Count == 1 && ((JArray)types)[0] is JObject
&& ((JObject)((JArray)types)[0]).Count == 0)
{
return !((JArray)nodeTypes).IsEmpty();
}
else
{
foreach (JToken i in (JArray)nodeTypes)
{
foreach (JToken j in (JArray)types)
{
if (JsonLdUtils.DeepCompare(i, j))
{
return true;
}
}
}
return false;
}
}
else
{
foreach (string key in frame.GetKeys())
{
if ("@id".Equals(key) || !JsonLdUtils.IsKeyword(key) && !(node.ContainsKey(key)))
{
return false;
}
}
return true;
}
}
示例8: FilterNodes
/// <exception cref="JsonLD.Core.JsonLdError"></exception>
private JObject FilterNodes(JsonLdApi.FramingContext state, JObject nodes, JObject frame)
{
JObject rval = new JObject();
foreach (string id in nodes.GetKeys())
{
JObject element = (JObject)nodes[id];
if (element != null && FilterNode(state, element, frame))
{
rval[id] = element;
}
}
return rval;
}
示例9: Frame
/// <summary>Frames subjects according to the given frame.</summary>
/// <remarks>Frames subjects according to the given frame.</remarks>
/// <param name="state">the current framing state.</param>
/// <param name="subjects">the subjects to filter.</param>
/// <param name="frame">the frame.</param>
/// <param name="parent">the parent subject or top-level array.</param>
/// <param name="property">the parent property, initialized to null.</param>
/// <exception cref="JSONLDProcessingError">JSONLDProcessingError</exception>
/// <exception cref="JsonLD.Core.JsonLdError"></exception>
private void Frame(JsonLdApi.FramingContext state, JObject nodes
, JObject frame, JToken parent, string property)
{
// filter out subjects that match the frame
JObject matches = FilterNodes(state, nodes, frame);
// get flags for current frame
bool embedOn = GetFrameFlag(frame, "@embed", state.embed);
bool explicitOn = GetFrameFlag(frame, "@explicit", [email protected]);
// add matches to output
JArray ids = new JArray(matches.GetKeys());
ids.SortInPlace();
foreach (string id in ids)
{
if (property == null)
{
state.embeds = new Dictionary<string, JsonLdApi.EmbedNode>();
}
// start output
JObject output = new JObject();
output["@id"] = id;
// prepare embed meta info
JsonLdApi.EmbedNode embeddedNode = new JsonLdApi.EmbedNode(this);
embeddedNode.parent = parent;
embeddedNode.property = property;
// if embed is on and there is an existing embed
if (embedOn && state.embeds.ContainsKey(id))
{
JsonLdApi.EmbedNode existing = state.embeds[id];
embedOn = false;
if (existing.parent is JArray)
{
foreach (JToken p in (JArray)(existing.parent))
{
if (JsonLdUtils.CompareValues(output, p))
{
embedOn = true;
break;
}
}
}
else
{
// existing embed's parent is an object
if (((JObject)existing.parent).ContainsKey(existing.property))
{
foreach (JToken v in (JArray)((JObject)existing.parent)[existing.property])
{
if (v is JObject && ((JObject)v)["@id"].SafeCompare(id))
{
embedOn = true;
break;
}
}
}
}
// existing embed has already been added, so allow an overwrite
if (embedOn)
{
RemoveEmbed(state, id);
}
}
// not embedding, add output without any other properties
if (!embedOn)
{
AddFrameOutput(state, parent, property, output);
}
else
{
// add embed meta info
state.embeds[id] = embeddedNode;
// iterate over subject properties
JObject element = (JObject)matches[id];
JArray props = new JArray(element.GetKeys());
props.SortInPlace();
foreach (string prop in props)
{
// copy keywords to output
if (JsonLdUtils.IsKeyword(prop))
{
output[prop] = JsonLdUtils.Clone(element[prop]);
continue;
}
// if property isn't in the frame
if (!frame.ContainsKey(prop))
{
// if explicit is off, embed values
if (!explicitOn)
{
EmbedValues(state, element, prop, output);
}
continue;
//.........这里部分代码省略.........