コード例 #1
0
        /// <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)
        {
            var count = urls.Count;

            if (input is JArray)
            {
                foreach (var i in (JArray)input)
                {
                    FindContextUrls(i, urls, replace);
                }
                return(count < urls.Count);
            }

            if (input is JObject)
            {
                foreach (var key in input.GetKeys())
                {
                    if (!"@context".Equals(key))
                    {
                        FindContextUrls(((JObject)input)[key], urls, replace);
                        continue;
                    }

                    // get @context
                    var ctx = ((JObject)input)[key];

                    // array @context
                    if (ctx is JArray)
                    {
                        var length = ((JArray)ctx).Count;
                        for (var i = 0; i < length; i++)
                        {
                            var _ctx = ((JArray)ctx)[i];
                            if (_ctx.Type == JTokenType.String)
                            {
                                if (replace)
                                {
                                    _ctx = urls[(string)_ctx];
                                    if (_ctx is JArray)
                                    {
                                        // add flattened context
                                        ((JArray)ctx).RemoveAt(i);
                                        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)
                        {
                            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);
        }