当前位置: 首页>>代码示例>>C#>>正文


C# JToken.Equals方法代码示例

本文整理汇总了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;
 }
开发者ID:NuGet,项目名称:json-ld.net,代码行数:33,代码来源:JsonLdUtils.cs

示例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;
        }
开发者ID:screamish,项目名称:pact-net,代码行数:91,代码来源:HttpBodyComparer.cs

示例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);
                 }
             }
         }
     }
 }
开发者ID:NuGet,项目名称:json-ld.net,代码行数:80,代码来源:JsonLdUtils.cs

示例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];
//.........这里部分代码省略.........
开发者ID:rafalrosochacki,项目名称:RomanticWeb,代码行数:101,代码来源:JsonLdProcessor.toRdf.cs


注:本文中的JToken.Equals方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。