Ejemplo n.º 1
1
		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;
		}
Ejemplo n.º 2
0
 static int RemoveAt(IntPtr L)
 {
     try
     {
         ToLua.CheckArgsCount(L, 2);
         Newtonsoft.Json.Linq.JArray obj = (Newtonsoft.Json.Linq.JArray)ToLua.CheckObject <Newtonsoft.Json.Linq.JArray>(L, 1);
         int arg0 = (int)LuaDLL.luaL_checknumber(L, 2);
         obj.RemoveAt(arg0);
         return(0);
     }
     catch (Exception e)
     {
         return(LuaDLL.toluaL_exception(L, e));
     }
 }
Ejemplo n.º 3
0
    public void RemoveAtNegativeIndexShouldError()
    {
      JArray j = new JArray();

      ExceptionAssert.Throws<ArgumentOutOfRangeException>(
        @"Index is less than 0.
Parameter name: index",
        () =>
          {
            j.RemoveAt(-1);
          });
    }
Ejemplo n.º 4
0
    public void RemoveAtOutOfRangeIndexShouldError()
    {
      JArray j = new JArray();

      ExceptionAssert.Throws<ArgumentOutOfRangeException>(
        @"Index is equal to or greater than Count.
Parameter name: index",
        () =>
          {
            j.RemoveAt(0);
          });
    }
Ejemplo n.º 5
0
    public void RemoveAt()
    {
      JValue v1 = new JValue(1);
      JValue v2 = new JValue(1);
      JValue v3 = new JValue(1);

      JArray j = new JArray();

      j.Add(v1);
      j.Add(v2);
      j.Add(v3);

      Assert.AreEqual(true, j.Contains(v1));
      j.RemoveAt(0);
      Assert.AreEqual(false, j.Contains(v1));

      Assert.AreEqual(true, j.Contains(v3));
      j.RemoveAt(1);
      Assert.AreEqual(false, j.Contains(v3));

      Assert.AreEqual(1, j.Count);
    }
Ejemplo n.º 6
0
 private JToken GetAndRemove(JArray array, int index)
 {
     var value = array[index];
     array.RemoveAt(index);
     return value;
 }
Ejemplo n.º 7
0
 public void RemoveAtNegativeIndexShouldError()
 {
   JArray j = new JArray();
   j.RemoveAt(-1);
 }
Ejemplo n.º 8
0
 public void RemoveAtOutOfRangeIndexShouldError()
 {
   JArray j = new JArray();
   j.RemoveAt(0);
 }
Ejemplo n.º 9
0
		/// <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())
					{
						// 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;
		}