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


C# JToken.GetKeys方法代码示例

本文整理汇总了C#中JToken.GetKeys方法的典型用法代码示例。如果您正苦于以下问题:C# JToken.GetKeys方法的具体用法?C# JToken.GetKeys怎么用?C# JToken.GetKeys使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在JToken的用法示例。


在下文中一共展示了JToken.GetKeys方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: FindContextUrls

 /// <summary>Finds all @context URLs in the given JSON-LD input.</summary>
 /// <remarks>Finds all @context URLs in the given JSON-LD input.</remarks>
 /// <param name="input">the JSON-LD input.</param>
 /// <param name="urls">a map of URLs (url =&gt; false/@contexts).</param>
 /// <param name="replace">true to replace the URLs in the given input with the</param>
 /// <contexts>from the urls map, false not to.</contexts>
 /// <returns>true if new URLs to resolve were found, false if not.</returns>
 private static bool FindContextUrls(JToken input, JObject urls
     , bool replace)
 {
     int count = urls.Count;
     if (input is JArray)
     {
         foreach (JToken i in (JArray)input)
         {
             FindContextUrls(i, urls, replace);
         }
         return count < urls.Count;
     }
     else
     {
         if (input is JObject)
         {
             foreach (string key in input.GetKeys())
             {
                 if (!"@context".Equals(key))
                 {
                     FindContextUrls(((JObject)input)[key], urls, replace);
                     continue;
                 }
                 // get @context
                 JToken ctx = ((JObject)input)[key];
                 // array @context
                 if (ctx is JArray)
                 {
                     int length = ((JArray)ctx).Count;
                     for (int i = 0; i < length; i++)
                     {
                         JToken _ctx = ((JArray)ctx)[i];
                         if (_ctx.Type == JTokenType.String)
                         {
                             // replace w/@context if requested
                             if (replace)
                             {
                                 _ctx = urls[(string)_ctx];
                                 if (_ctx is JArray)
                                 {
                                     // add flattened context
                                     ((JArray)ctx).RemoveAt(i);
                                     JsonLD.Collections.AddAllObj(((JArray)ctx), (ICollection)_ctx);
                                     i += ((JArray)_ctx).Count;
                                     length += ((JArray)_ctx).Count;
                                 }
                                 else
                                 {
                                     ((JArray)ctx)[i] = _ctx;
                                 }
                             }
                             else
                             {
                                 // @context URL found
                                 if (!urls.ContainsKey((string)_ctx))
                                 {
                                     urls[(string)_ctx] = false;
                                 }
                             }
                         }
                     }
                 }
                 else
                 {
                     // string @context
                     if (ctx.Type == JTokenType.String)
                     {
                         // replace w/@context if requested
                         if (replace)
                         {
                             ((JObject)input)[key] = urls[(string)ctx];
                         }
                         else
                         {
                             // @context URL found
                             if (!urls.ContainsKey((string)ctx))
                             {
                                 urls[(string)ctx] = false;
                             }
                         }
                     }
                 }
             }
             return (count < urls.Count);
         }
     }
     return false;
 }
开发者ID:NuGet,项目名称:json-ld.net,代码行数:95,代码来源:JsonLdUtils.cs

示例2: RemovePreserve

 /// <summary>Removes the @preserve keywords as the last step of the framing algorithm.
 /// 	</summary>
 /// <remarks>Removes the @preserve keywords as the last step of the framing algorithm.
 /// 	</remarks>
 /// <param name="ctx">the active context used to compact the input.</param>
 /// <param name="input">the framed, compacted output.</param>
 /// <param name="options">the compaction options used.</param>
 /// <returns>the resulting output.</returns>
 /// <exception cref="JsonLdError">JsonLdError</exception>
 /// <exception cref="JsonLD.Core.JsonLdError"></exception>
 internal static JToken RemovePreserve(Context ctx, JToken input, JsonLdOptions opts
     )
 {
     // recurse through arrays
     if (IsArray(input))
     {
         JArray output = new JArray();
         foreach (JToken i in (JArray)input)
         {
             JToken result = RemovePreserve(ctx, i, opts);
             // drop nulls from arrays
             if (!result.IsNull())
             {
                 output.Add(result);
             }
         }
         input = output;
     }
     else
     {
         if (IsObject(input))
         {
             // remove @preserve
             if (((JObject)input).ContainsKey("@preserve"))
             {
                 if (((JObject)input)["@preserve"].SafeCompare("@null"))
                 {
                     return null;
                 }
                 return ((JObject)input)["@preserve"];
             }
             // skip @values
             if (IsValue(input))
             {
                 return input;
             }
             // recurse through @lists
             if (IsList(input))
             {
                 ((JObject)input)["@list"] = RemovePreserve(ctx, ((JObject)input)["@list"], opts);
                 return input;
             }
             // recurse through properties
             foreach (string prop in input.GetKeys())
             {
                 JToken result = RemovePreserve(ctx, ((JObject)input)[prop], opts
                     );
                 string container = ctx.GetContainer(prop);
                 if (opts.GetCompactArrays() && IsArray(result) && ((JArray)result).Count ==
                      1 && container == null)
                 {
                     result = ((JArray)result)[0];
                 }
                 ((JObject)input)[prop] = result;
             }
         }
     }
     return input;
 }
开发者ID:NuGet,项目名称:json-ld.net,代码行数:69,代码来源:JsonLdUtils.cs

示例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))
//.........这里部分代码省略.........
开发者ID:hydrojumbo,项目名称:json-ld.net,代码行数:101,代码来源:JsonLdApi.cs

示例4: 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)
//.........这里部分代码省略.........
开发者ID:hydrojumbo,项目名称:json-ld.net,代码行数:101,代码来源:JsonLdApi.cs

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


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