本文整理汇总了C#中Newtonsoft.Json.Linq.JArray.SortInPlace方法的典型用法代码示例。如果您正苦于以下问题:C# JArray.SortInPlace方法的具体用法?C# JArray.SortInPlace怎么用?C# JArray.SortInPlace使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Newtonsoft.Json.Linq.JArray
的用法示例。
在下文中一共展示了JArray.SortInPlace方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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;
}
示例2: Compact
/// <summary>
/// Compaction Algorithm
/// http://json-ld.org/spec/latest/json-ld-api/#compaction-algorithm
/// </summary>
/// <param name="activeCtx"></param>
/// <param name="activeProperty"></param>
/// <param name="element"></param>
/// <param name="compactArrays"></param>
/// <returns></returns>
/// <exception cref="JsonLD.Core.JsonLdError"></exception>
public virtual JToken Compact(Context activeCtx, string activeProperty, JToken element
, bool compactArrays)
{
// 2)
if (element is JArray)
{
// 2.1)
JArray result = new JArray();
// 2.2)
foreach (JToken item in element)
{
// 2.2.1)
JToken compactedItem = Compact(activeCtx, activeProperty, item, compactArrays);
// 2.2.2)
if (!compactedItem.IsNull())
{
result.Add(compactedItem);
}
}
// 2.3)
if (compactArrays && result.Count == 1 && activeCtx.GetContainer(activeProperty)
== null)
{
return result[0];
}
// 2.4)
return result;
}
// 3)
if (element is JObject)
{
// access helper
IDictionary<string, JToken> elem = (IDictionary<string, JToken>)element;
// 4
if (elem.ContainsKey("@value") || elem.ContainsKey("@id"))
{
JToken compactedValue = activeCtx.CompactValue(activeProperty, (JObject)element);
if (!(compactedValue is JObject || compactedValue is JArray))
{
return compactedValue;
}
}
// 5)
bool insideReverse = ("@reverse".Equals(activeProperty));
// 6)
JObject result = new JObject();
// 7)
JArray keys = new JArray(element.GetKeys());
keys.SortInPlace();
foreach (string expandedProperty in keys)
{
JToken expandedValue = elem[expandedProperty];
// 7.1)
if ("@id".Equals(expandedProperty) || "@type".Equals(expandedProperty))
{
JToken compactedValue;
// 7.1.1)
if (expandedValue.Type == JTokenType.String)
{
compactedValue = activeCtx.CompactIri((string)expandedValue, "@type".Equals(expandedProperty
));
}
else
{
// 7.1.2)
JArray types = new JArray();
// 7.1.2.2)
foreach (string expandedType in (JArray)expandedValue)
{
types.Add(activeCtx.CompactIri(expandedType, true));
}
// 7.1.2.3)
if (types.Count == 1)
{
compactedValue = types[0];
}
else
{
compactedValue = types;
}
}
// 7.1.3)
string alias = activeCtx.CompactIri(expandedProperty, true);
// 7.1.4)
result[alias] = compactedValue;
continue;
}
// TODO: old add value code, see if it's still relevant?
// addValue(rval, alias, compactedValue,
// isArray(compactedValue)
//.........这里部分代码省略.........
示例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: FromRDF
//.........这里部分代码省略.........
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())
{
// 4.3.3.1)
list.Add(((JArray)node[JSONLDConsts.RdfFirst])[0]);
// 4.3.3.2)
listNodes.Add((string)node["@id"]);
// 4.3.3.3)
JsonLdApi.UsagesNode nodeUsage = node.usages[0];
// 4.3.3.4)
node = nodeUsage.node;
property = nodeUsage.property;
head = nodeUsage.value;
// 4.3.3.5)
if (!JsonLdUtils.IsBlankNode(node))
{
break;
}
}
// 4.3.4)
if (JSONLDConsts.RdfFirst.Equals(property))
{
// 4.3.4.1)
if (JSONLDConsts.RdfNil.Equals(node["@id"]))
{
continue;
}
// 4.3.4.3)
string headId = (string)head["@id"];
// 4.3.4.4-5)
head = (JObject)((JArray)graph[headId][JSONLDConsts.RdfRest
])[0];
// 4.3.4.6)
list.RemoveAt(list.Count - 1);
listNodes.RemoveAt(listNodes.Count - 1);
}
// 4.3.5)
JsonLD.Collections.Remove(head, "@id");
// 4.3.6)
JsonLD.Collections.Reverse(list);
// 4.3.7)
head["@list"] = list;
// 4.3.8)
foreach (string nodeId in listNodes)
{
JsonLD.Collections.Remove(graph, nodeId);
}
}
}
// 5)
JArray result = new JArray();
// 6)
JArray ids = new JArray(defaultGraph.GetKeys());
ids.SortInPlace();
foreach (string subject_1 in ids)
{
JsonLdApi.NodeMapNode node = (NodeMapNode)defaultGraph[subject_1];
// 6.1)
if (graphMap.ContainsKey(subject_1))
{
// 6.1.1)
node["@graph"] = new JArray();
// 6.1.2)
JArray keys = new JArray(graphMap[subject_1].GetKeys());
keys.SortInPlace();
foreach (string s in keys)
{
JsonLdApi.NodeMapNode n = (NodeMapNode)graphMap[subject_1][s];
if (n.Count == 1 && n.ContainsKey("@id"))
{
continue;
}
((JArray)node["@graph"]).Add(n.Serialize());
}
}
// 6.2)
if (node.Count == 1 && node.ContainsKey("@id"))
{
continue;
}
result.Add(node.Serialize());
}
return result;
}
示例5: 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;
//.........这里部分代码省略.........
示例6: GenerateNodeMap
//.........这里部分代码省略.........
);
}
else
{
// 6.6)
if (activeProperty != null)
{
JObject reference = new JObject();
reference["@id"] = id;
// 6.6.2)
if (list == null)
{
// 6.6.2.1+2)
JsonLdUtils.MergeValue(node, activeProperty, reference);
}
else
{
// 6.6.3) TODO: SPEC says to add ELEMENT to @list member, should
// be REFERENCE
JsonLdUtils.MergeValue(list, "@list", reference);
}
}
}
// TODO: SPEC this is removed in the spec now, but it's still needed (see 6.4)
node = (JObject)graph[id];
// 6.7)
if (elem.ContainsKey("@type"))
{
foreach (JToken type in (JArray)JsonLD.Collections.Remove(elem, "@type"
))
{
JsonLdUtils.MergeValue(node, "@type", type);
}
}
// 6.8)
if (elem.ContainsKey("@index"))
{
JToken elemIndex = JsonLD.Collections.Remove(elem, "@index");
if (node.ContainsKey("@index"))
{
if (!JsonLdUtils.DeepCompare(node["@index"], elemIndex))
{
throw new JsonLdError(JsonLdError.Error.ConflictingIndexes);
}
}
else
{
node["@index"] = elemIndex;
}
}
// 6.9)
if (elem.ContainsKey("@reverse"))
{
// 6.9.1)
JObject referencedNode = new JObject();
referencedNode["@id"] = id;
// 6.9.2+6.9.4)
JObject reverseMap = (JObject)JsonLD.Collections.Remove
(elem, "@reverse");
// 6.9.3)
foreach (string property in reverseMap.GetKeys())
{
JArray values = (JArray)reverseMap[property];
// 6.9.3.1)
foreach (JToken value in values)
{
// 6.9.3.1.1)
GenerateNodeMap(value, nodeMap, activeGraph, referencedNode, property, null);
}
}
}
// 6.10)
if (elem.ContainsKey("@graph"))
{
GenerateNodeMap(JsonLD.Collections.Remove(elem, "@graph"), nodeMap, id, null,
null, null);
}
// 6.11)
JArray keys = new JArray(element.GetKeys());
keys.SortInPlace();
foreach (string property_1 in keys)
{
var eachProperty_1 = property_1;
JToken value = elem[eachProperty_1];
// 6.11.1)
if (eachProperty_1.StartsWith("_:"))
{
eachProperty_1 = GenerateBlankNodeIdentifier(eachProperty_1);
}
// 6.11.2)
if (!node.ContainsKey(eachProperty_1))
{
node[eachProperty_1] = new JArray();
}
// 6.11.3)
GenerateNodeMap(value, nodeMap, activeGraph, id, eachProperty_1, null);
}
}
}
}