本文整理汇总了C#中JToken.Equals方法的典型用法代码示例。如果您正苦于以下问题:C# JToken.Equals方法的具体用法?C# JToken.Equals怎么用?C# JToken.Equals使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类JToken
的用法示例。
在下文中一共展示了JToken.Equals方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CompareValues
/// <summary>Compares two JSON-LD values for equality.</summary>
/// <remarks>
/// Compares two JSON-LD values for equality. Two JSON-LD values will be
/// considered equal if:
/// 1. They are both primitives of the same type and value. 2. They are both @values
/// with the same @value, @type, and @language, OR 3. They both have @ids
/// they are the same.
/// </remarks>
/// <param name="v1">the first value.</param>
/// <param name="v2">the second value.</param>
/// <returns>true if v1 and v2 are considered equal, false if not.</returns>
internal static bool CompareValues(JToken v1, JToken v2)
{
if (v1.Equals(v2))
{
return true;
}
if (IsValue(v1) && IsValue(v2) && Obj.Equals(((JObject)v1)["@value"
], ((JObject)v2)["@value"]) && Obj.Equals(((JObject)v1)["@type"], ((JObject)v2)["@type"]) && Obj.Equals
(((JObject)v1)["@language"], ((JObject)v2
)["@language"]) && Obj.Equals(((JObject)v1)["@index"], ((JObject)v2)["@index"]))
{
return true;
}
if ((v1 is JObject && ((JObject)v1).ContainsKey("@id")) &&
(v2 is JObject && ((JObject)v2).ContainsKey("@id")) &&
((JObject)v1)["@id"].Equals(((JObject)v2
)["@id"]))
{
return true;
}
return false;
}
示例2: AssertPropertyValuesMatch
private bool AssertPropertyValuesMatch(JToken httpBody1, JToken httpBody2)
{
switch (httpBody1.Type)
{
case JTokenType.Array:
{
if (httpBody1.Count() != httpBody2.Count())
{
_reporter.ReportError(expected: httpBody1.Root, actual: httpBody2.Root);
return false;
}
for (var i = 0; i < httpBody1.Count(); i++)
{
if (httpBody2.Count() > i)
{
var isMatch = AssertPropertyValuesMatch(httpBody1[i], httpBody2[i]);
if (!isMatch)
{
break;
}
}
}
break;
}
case JTokenType.Object:
{
foreach (JProperty item1 in httpBody1)
{
var item2 = httpBody2.Cast<JProperty>().SingleOrDefault(x => x.Name == item1.Name);
if (item2 != null)
{
var isMatch = AssertPropertyValuesMatch(item1, item2);
if (!isMatch)
{
break;
}
}
else
{
_reporter.ReportError(expected: httpBody1.Root, actual: httpBody2.Root);
return false;
}
}
break;
}
case JTokenType.Property:
{
var httpBody2Item = httpBody2.SingleOrDefault();
var httpBody1Item = httpBody1.SingleOrDefault();
if (httpBody2Item == null && httpBody1Item == null)
{
return true;
}
if (httpBody2Item != null && httpBody1Item != null)
{
AssertPropertyValuesMatch(httpBody1Item, httpBody2Item);
}
else
{
_reporter.ReportError(expected: httpBody1.Root, actual: httpBody2.Root);
return false;
}
break;
}
case JTokenType.Integer:
case JTokenType.String:
{
if (!httpBody1.Equals(httpBody2))
{
_reporter.ReportError(expected: httpBody1.Root, actual: httpBody2.Root);
return false;
}
break;
}
default:
{
if (!JToken.DeepEquals(httpBody1, httpBody2))
{
_reporter.ReportError(expected: httpBody1.Root, actual: httpBody2.Root);
return false;
}
break;
}
}
return true;
}
示例3: DeepCompare
public static bool DeepCompare(JToken v1, JToken v2, bool listOrderMatters)
{
if (v1 == null)
{
return v2 == null;
}
else
{
if (v2 == null)
{
return v1 == null;
}
else
{
if (v1 is JObject && v2 is JObject)
{
JObject m1 = (JObject)v1;
JObject m2 = (JObject)v2;
if (m1.Count != m2.Count)
{
return false;
}
foreach (string key in m1.GetKeys())
{
if (!((IDictionary<string,JToken>)m2).ContainsKey(key) ||
!DeepCompare(m1[key], m2[key], listOrderMatters))
{
return false;
}
}
return true;
}
else
{
if (v1 is JArray && v2 is JArray)
{
JArray l1 = (JArray)v1;
JArray l2 = (JArray)v2;
if (l1.Count != l2.Count)
{
return false;
}
// used to mark members of l2 that we have already matched to avoid
// matching the same item twice for lists that have duplicates
bool[] alreadyMatched = new bool[l2.Count];
for (int i = 0; i < l1.Count; i++)
{
JToken o1 = l1[i];
bool gotmatch = false;
if (listOrderMatters)
{
gotmatch = DeepCompare(o1, l2[i], listOrderMatters);
}
else
{
for (int j = 0; j < l2.Count; j++)
{
if (!alreadyMatched[j] && DeepCompare(o1, l2[j], listOrderMatters))
{
alreadyMatched[j] = true;
gotmatch = true;
break;
}
}
}
if (!gotmatch)
{
return false;
}
}
return true;
}
else
{
return v1.Equals(v2);
}
}
}
}
}
示例4: GenerateNodeMap
private void GenerateNodeMap(JObject element, JObject nodeMap, IDictionary<string, string> identifierMap, ref int counter, string activeGraph = Default, JToken activeSubject = null, string activeProperty = null, JObject list = null)
{
JObject graph = (JObject)nodeMap[activeGraph];
if (graph == null)
{
nodeMap[activeGraph] = graph = new JObject();
}
JObject node = null;
if (activeSubject != null)
{
node = (JObject)graph[activeSubject.ToString()];
}
if (element.IsPropertySet(Type))
{
foreach (JValue item in (JArray)element[Type].AsArray())
{
if (item.ValueAs<string>().StartsWith("_:"))
{
item.Value = CreateBlankNodeId(item.ValueAs<string>(), identifierMap, ref counter);
}
}
}
if (element.IsPropertySet(Value))
{
if (list == null)
{
if (!node.IsPropertySet(activeProperty))
{
node[activeProperty] = new JArray(element);
}
else if (!((JArray)node[activeProperty]).Any(item => element.Equals(item)))
{
((JArray)node[activeProperty]).Add(element);
}
}
else
{
((JArray)list[List]).Add(element);
}
}
else if (element.IsPropertySet(List))
{
JObject result = new JObject(new JProperty(List, new JArray()));
GenerateNodeMap(element[List], nodeMap, identifierMap, ref counter, activeGraph, activeSubject, activeProperty, result);
((JArray)node[activeProperty]).Add(result);
}
else
{
string id;
if (element.IsPropertySet(Id))
{
id = ((id = element.Property(Id).ValueAs<string>()).StartsWith("_:") ? CreateBlankNodeId(id, identifierMap, ref counter) : id);
element.Remove(Id);
}
else
{
id = CreateBlankNodeId(null, identifierMap, ref counter);
}
if (!graph.IsPropertySet(id))
{
graph[id] = new JObject(new JProperty(Id, id));
}
if (activeSubject is JObject)
{
node = (JObject)graph[id];
if (!node.IsPropertySet(activeProperty))
{
node[activeProperty] = new JArray(activeSubject);
}
else if (!((JArray)node[activeProperty]).Any(item => activeSubject.Equals(item)))
{
((JArray)node[activeProperty]).Add(activeSubject);
}
}
else if (activeProperty != null)
{
JObject reference = new JObject(new JProperty(Id, id));
if (list == null)
{
if (!node.IsPropertySet(activeProperty))
{
node[activeProperty] = new JArray(reference);
}
else if (!((JArray)node[activeProperty]).Any(item => reference.Equals(item)))
{
((JArray)node[activeProperty]).Add(reference);
}
}
else
{
((JArray)list[List]).Add(reference);
}
}
node = (JObject)graph[id];
//.........这里部分代码省略.........