當前位置: 首頁>>代碼示例>>C#>>正文


C# JArray.Insert方法代碼示例

本文整理匯總了C#中Newtonsoft.Json.Linq.JArray.Insert方法的典型用法代碼示例。如果您正苦於以下問題:C# JArray.Insert方法的具體用法?C# JArray.Insert怎麽用?C# JArray.Insert使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Newtonsoft.Json.Linq.JArray的用法示例。


在下文中一共展示了JArray.Insert方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: ArrayUnpatch

		private JArray ArrayUnpatch(JArray right, JObject patch)
		{
			var toRemove = new List<JProperty>();
			var toInsert = new List<JProperty>();
			var toModify = new List<JProperty>();

			foreach (JProperty op in patch.Properties())
			{
				if (op.Name == "_t")
					continue;

				var value = op.Value as JArray;

				if (op.Name.StartsWith("_"))
				{
					// removed item from original array
					if (value != null && value.Count == 3 && (value[2].ToObject<int>() == (int)DiffOperation.Deleted || value[2].ToObject<int>() == (int)DiffOperation.ArrayMove))
					{
						var newOp = new JProperty(value[1].ToObject<int>().ToString(), op.Value);

						if (value[2].ToObject<int>() == (int)DiffOperation.ArrayMove)
						{
							toInsert.Add(new JProperty(op.Name.Substring(1), new JArray(right[value[1].ToObject<int>()].DeepClone())));
							toRemove.Add(newOp);
						}
						else
						{
							toInsert.Add(new JProperty(op.Name.Substring(1), new JArray(value[0])));
						}
					}
					else
					{
						throw new Exception($"Only removal or move can be applied at original array indices. Context: {value}");
					}
				}
				else
				{
					if (value != null && value.Count == 1)
					{
						toRemove.Add(op);
					}
					else
					{
						toModify.Add(op);
					}
				}
			}


			// remove items, in reverse order to avoid sawing our own floor
			toRemove.Sort((x, y) => int.Parse(x.Name).CompareTo(int.Parse(y.Name)));
			for (int i = toRemove.Count - 1; i >= 0; --i)
			{
				JProperty op = toRemove[i];
				right.RemoveAt(int.Parse(op.Name));
			}

			// insert items, in reverse order to avoid moving our own floor
			toInsert.Sort((x, y) => int.Parse(x.Name).CompareTo(int.Parse(y.Name)));
			foreach (var op in toInsert)
			{
				right.Insert(int.Parse(op.Name), ((JArray)op.Value)[0]);
			}

			foreach (var op in toModify)
			{
				JToken p = Unpatch(right[int.Parse(op.Name)], op.Value);
				right[int.Parse(op.Name)] = p;
			}

			return right;
		}
開發者ID:wbish,項目名稱:jsondiffpatch.net,代碼行數:72,代碼來源:JsonDiffPatch.cs

示例2: InsertAddEnd

    public void InsertAddEnd()
    {
      JArray array = new JArray();
      array.Insert(0, 123);
      array.Insert(1, 456);

      Assert.AreEqual(2, array.Count);
      Assert.AreEqual(123, (int) array[0]);
      Assert.AreEqual(456, (int) array[1]);
    }
開發者ID:nagyist,項目名稱:Newtonsoft.Json,代碼行數:10,代碼來源:JArrayTests.cs

示例3: InsertOutOfRangeIndexShouldThrow

    public void InsertOutOfRangeIndexShouldThrow()
    {
      JArray j = new JArray();

      ExceptionAssert.Throws<ArgumentOutOfRangeException>(
        @"Index must be within the bounds of the List.
Parameter name: index",
        () =>
          {
            j.Insert(2, new JValue(1));
          });
    }
開發者ID:nagyist,項目名稱:Newtonsoft.Json,代碼行數:12,代碼來源:JArrayTests.cs

示例4: InsertNegativeIndexShouldThrow

    public void InsertNegativeIndexShouldThrow()
    {
      JArray j = new JArray();

      ExceptionAssert.Throws<ArgumentOutOfRangeException>(
        @"Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index",
        () =>
        {
          j.Insert(-1, new JValue(1));
        });
    }
開發者ID:nagyist,項目名稱:Newtonsoft.Json,代碼行數:12,代碼來源:JArrayTests.cs

示例5: InsertNull

    public void InsertNull()
    {
      JArray j = new JArray();
      j.Insert(0, null);

      Assert.AreEqual(null, ((JValue) j[0]).Value);
    }
開發者ID:nagyist,項目名稱:Newtonsoft.Json,代碼行數:7,代碼來源:JArrayTests.cs

示例6: InsertShouldInsertAtZeroIndex

    public void InsertShouldInsertAtZeroIndex()
    {
      JValue v1 = new JValue(1);
      JValue v2 = new JValue(2);

      JArray j = new JArray();

      j.Insert(0, v1);
      Assert.AreEqual(0, j.IndexOf(v1));

      j.Insert(0, v2);
      Assert.AreEqual(1, j.IndexOf(v1));
      Assert.AreEqual(0, j.IndexOf(v2));
    }
開發者ID:nagyist,項目名稱:Newtonsoft.Json,代碼行數:14,代碼來源:JArrayTests.cs

示例7: Insert

    public void Insert()
    {
      JValue v1 = new JValue(1);
      JValue v2 = new JValue(2);
      JValue v3 = new JValue(3);
      JValue v4 = new JValue(4);

      JArray j = new JArray();

      j.Add(v1);
      j.Add(v2);
      j.Add(v3);
      j.Insert(1, v4);

      Assert.AreEqual(0, j.IndexOf(v1));
      Assert.AreEqual(1, j.IndexOf(v4));
      Assert.AreEqual(2, j.IndexOf(v2));
      Assert.AreEqual(3, j.IndexOf(v3));
    }
開發者ID:nagyist,項目名稱:Newtonsoft.Json,代碼行數:19,代碼來源:JArrayTests.cs

示例8: InsertOutOfRangeIndexShouldThrow

 public void InsertOutOfRangeIndexShouldThrow()
 {
   JArray j = new JArray();
   j.Insert(2, new JValue(1));
 }
開發者ID:pvasek,項目名稱:Newtonsoft.Json,代碼行數:5,代碼來源:JArrayTests.cs

示例9: InsertNegativeIndexShouldThrow

 public void InsertNegativeIndexShouldThrow()
 {
   JArray j = new JArray();
   j.Insert(-1, new JValue(1));
 }
開發者ID:pvasek,項目名稱:Newtonsoft.Json,代碼行數:5,代碼來源:JArrayTests.cs


注:本文中的Newtonsoft.Json.Linq.JArray.Insert方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。