コード例 #1
0
ファイル: JsonLdProcessor.cs プロジェクト: johnataylor/JLD
        public static JArray Expand(JObject original)
        {
            JsonLdProcessorContext activeContext = new JsonLdProcessorContext();

            JToken token = InnerExpand(activeContext, null, original);
            if (token == null)
            {
                return null;
            }
            if (token.Type == JTokenType.Array)
            {
                return (JArray)token;
            }
            JArray result = new JArray();
            result.Add(token);
            return result;
        }
コード例 #2
0
            public void Expand(JsonLdProcessorContext activeContext)
            {
                IDictionary<string, TermDefinition> expanded = new Dictionary<string, TermDefinition>();
                foreach (KeyValuePair<string, TermDefinition> prop in _props)
                {
                    TermDefinition expandedTermDefinition = new TermDefinition(prop.Value);

                    if (expandedTermDefinition.Type != null)
                    {
                        expandedTermDefinition.Type = activeContext.ExpandValue(ExpandValue(expandedTermDefinition.Type));
                    }

                    if (expandedTermDefinition.Id != null)
                    {
                        expandedTermDefinition.Id = activeContext.ExpandValue(ExpandValue(expandedTermDefinition.Id));
                    }

                    expanded.Add(prop.Key, expandedTermDefinition);
                }
                _props = expanded;
            }
コード例 #3
0
ファイル: JsonLdProcessor.cs プロジェクト: johnataylor/JLD
        private static JToken InnerExpand(JsonLdProcessorContext activeContext, TermDefinition activePropertyTermDefinition, JToken original)
        {
            switch (original.Type)
            {
                case JTokenType.Array:
                {
                    JArray originalArray = (JArray)original;
                    JArray expanded = new JArray();
                    foreach (JToken property in originalArray)
                    {
                        expanded.Add(InnerExpand(activeContext, activePropertyTermDefinition, property));
                    }
                    return expanded;
                }
                case JTokenType.Object:
                {
                    JObject originalObj = (JObject)original;
                    JToken expanded;

                    activeContext.Push(originalObj);

                    JToken graph;
                    if (originalObj.TryGetValue("@graph", out graph))
                    {
                        expanded = InnerExpand(activeContext, null, graph);

                        JToken id;
                        if (originalObj.TryGetValue("@id", out id))
                        {
                            JObject namedGraph = new JObject();
                            namedGraph.Add("@graph", expanded);
                            namedGraph.Add("@id", id);

                            //TODO: other graph level properties

                            expanded = namedGraph;
                        }
                    }
                    else
                    {
                        JObject obj = new JObject();

                        foreach (JProperty property in originalObj.Properties())
                        {
                            string name = activeContext.Alias(property.Name);

                            switch (name)
                            {
                                case "@id":
                                    obj.Add("@id", property.Value);
                                    break;

                                case "@context":
                                    break;

                                case "@type":
                                    {
                                        string expandedName = "@type";
                                        JToken expandedValue = InnerExpand(activeContext, null, property.Value);
                                        AddToValueArray(obj, expandedName, expandedValue);
                                        break;
                                    }

                                default:
                                    {
                                        TermDefinition termDefinition;
                                        if (activeContext.TryLookUp(name, out termDefinition))
                                        {
                                            if (termDefinition.Id == null)
                                            {
                                                TermDefinition prefixTermDefinition;
                                                if (activeContext.TryLookUpPrefix(name, out prefixTermDefinition))
                                                {
                                                    termDefinition.Id = prefixTermDefinition.Id;
                                                }
                                            }
                                            JToken expandedValue = InnerExpand(activeContext, termDefinition, property.Value);
                                            AddToObject(obj, termDefinition, expandedValue);
                                        }
                                        else if (activeContext.TryLookUpPrefix(name, out termDefinition))
                                        {
                                            JToken expandedValue = InnerExpand(activeContext, termDefinition, property.Value);
                                            AddToObject(obj, termDefinition, expandedValue);
                                        }
                                        else
                                        {
                                            termDefinition = new TermDefinition(name);
                                            JToken expandedValue = InnerExpand(activeContext, termDefinition, property.Value);
                                            AddToObject(obj, termDefinition, expandedValue);
                                        }
                                    }
                                    break;
                            }
                        }

                        expanded = obj;
                    }

                    activeContext.Pop();

                    return expanded;
                }
                default:
                {
                    if (activePropertyTermDefinition == null)
                    {
                        string expandedValue = activeContext.ExpandValue(original.ToString());
                        return new JValue(expandedValue);
                    }
                    else
                    {
                        JObject expanded = new JObject();

                        if (activePropertyTermDefinition.Type == "@id")
                        {
                            string expandedValue = activeContext.ExpandValue(original.ToString());
                            expanded.Add("@id", expandedValue);
                        }
                        else if (activePropertyTermDefinition.Type == "@vocab")
                        {
                            expanded.Add("@id", original);
                        }
                        else
                        {
                            expanded.Add("@value", original);

                            if (activePropertyTermDefinition.Type != null)
                            {
                                expanded.Add("@type", activePropertyTermDefinition.Type);
                            }
                            if (activePropertyTermDefinition.Language != null)
                            {
                                expanded.Add("@language", activePropertyTermDefinition.Language);
                            }
                        }

                        return expanded;
                    }
                }
            }
        }