/// <summary>
        /// Collect the terms from the JsonLdObject and all its child properties.
        /// </summary>
        /// <param name="obj">The JsonLdObject.</param>
        /// <returns>The list of terms.</returns>
        private static IDictionary <string, string> GetTerms(IJsonLdObject obj)
        {
            var terms = new Dictionary <string, string>();

            if (obj == null || obj.Terms == null)
            {
                return(terms);
            }

            foreach (var key in obj.Terms.Keys)
            {
                terms[key] = obj.Terms[key];
            }

            // Walk the object to find embedded JsonLdObjects
            foreach (var propertyInfo in obj.GetType().GetProperties())
            {
                if (!propertyInfo.CanRead)
                {
                    continue;
                }

                if (JsonLdObjectType.IsAssignableFrom(propertyInfo.PropertyType))
                {
                    var propertyValue = (IJsonLdObject)propertyInfo.GetValue(obj, null);
                    var propertyTerms = GetTerms(propertyValue);
                    foreach (var key in propertyTerms.Keys)
                    {
                        terms[key] = propertyTerms[key];
                    }
                }
                else if (JsonLdObjectArrayType.IsAssignableFrom(propertyInfo.PropertyType))
                {
                    var propertyValue = (IEnumerable <IJsonLdObject>)propertyInfo.GetValue(obj, null);
                    if (propertyValue != null)
                    {
                        foreach (var jsonLdObject in propertyValue)
                        {
                            var propertyTerms = GetTerms(jsonLdObject);
                            foreach (var key in propertyTerms.Keys)
                            {
                                terms[key] = propertyTerms[key];
                            }
                        }
                    }
                }
            }
            return(terms);
        }
        /// <summary>
        /// Get the external context from this object.
        /// </summary>
        /// <param name="obj">The JsonLdObject.</param>
        /// <returns>The last external context.</returns>
        private static Uri GetExternalContextId(IJsonLdObject obj)
        {
            if (obj == null)
            {
                return(null);
            }

            var externalContextId = obj.ExternalContextId;

            // Walk the object to find embedded JsonLdObjects
            foreach (var propertyInfo in obj.GetType().GetProperties())
            {
                if (!propertyInfo.CanRead)
                {
                    continue;
                }

                if (JsonLdObjectType.IsAssignableFrom(propertyInfo.PropertyType))
                {
                    var propertyValue = (IJsonLdObject)propertyInfo.GetValue(obj, null);
                    var contextId     = GetExternalContextId(propertyValue);
                    if (contextId != null)
                    {
                        externalContextId = contextId;
                    }
                }
                else if (JsonLdObjectArrayType.IsAssignableFrom(propertyInfo.PropertyType))
                {
                    var propertyValue = (IEnumerable <IJsonLdObject>)propertyInfo.GetValue(obj, null);
                    if (propertyValue != null)
                    {
                        foreach (var jsonLdObject in propertyValue)
                        {
                            var contextId = GetExternalContextId(jsonLdObject);
                            if (contextId != null)
                            {
                                externalContextId = contextId;
                            }
                        }
                    }
                }
            }
            return(externalContextId);
        }
        /// <summary>
        /// Collect the terms from the JsonLdObject and all its child properties.
        /// </summary>
        /// <param name="obj">The JsonLdObject.</param>
        /// <returns>The list of terms.</returns>
        private static IDictionary<string, string> GetTerms(IJsonLdObject obj)
        {
            var terms = new Dictionary<string, string>();
            if (obj == null || obj.Terms == null) return terms;

            foreach (var key in obj.Terms.Keys)
            {
                terms[key] = obj.Terms[key];
            }

            // Walk the object to find embedded JsonLdObjects
            foreach (var propertyInfo in obj.GetType().GetProperties())
            {
                if (!propertyInfo.CanRead) continue;

                if (JsonLdObjectType.IsAssignableFrom(propertyInfo.PropertyType))
                {
                    var propertyValue = (IJsonLdObject) propertyInfo.GetValue(obj, null);
                    var propertyTerms = GetTerms(propertyValue);
                    foreach (var key in propertyTerms.Keys)
                    {
                        terms[key] = propertyTerms[key];
                    }
                }
                else if (JsonLdObjectArrayType.IsAssignableFrom(propertyInfo.PropertyType))
                {
                    var propertyValue = (IEnumerable<IJsonLdObject>) propertyInfo.GetValue(obj, null);
                    if (propertyValue != null)
                    {
                        foreach (var jsonLdObject in propertyValue)
                        {
                            var propertyTerms = GetTerms(jsonLdObject);
                            foreach (var key in propertyTerms.Keys)
                            {
                                terms[key] = propertyTerms[key];
                            }
                        }
                    }
                }
            }
            return terms;
        }
        /// <summary>
        /// Get the external context from this object.
        /// </summary>
        /// <param name="obj">The JsonLdObject.</param>
        /// <returns>The last external context.</returns>
        private static Uri GetExternalContextId(IJsonLdObject obj)
        {
            if (obj == null) return null;

            var externalContextId = obj.ExternalContextId;

            // Walk the object to find embedded JsonLdObjects
            foreach (var propertyInfo in obj.GetType().GetProperties())
            {
                if (!propertyInfo.CanRead) continue;

                if (JsonLdObjectType.IsAssignableFrom(propertyInfo.PropertyType))
                {
                    var propertyValue = (IJsonLdObject)propertyInfo.GetValue(obj, null);
                    var contextId = GetExternalContextId(propertyValue);
                    if (contextId != null)
                        externalContextId = contextId;
                }
                else if (JsonLdObjectArrayType.IsAssignableFrom(propertyInfo.PropertyType))
                {
                    var propertyValue = (IEnumerable<IJsonLdObject>)propertyInfo.GetValue(obj, null);
                    if (propertyValue != null)
                    {
                        foreach (var jsonLdObject in propertyValue)
                        {
                            var contextId = GetExternalContextId(jsonLdObject);
                            if (contextId != null)
                                externalContextId = contextId;
                        }
                    }
                }
            }
            return externalContextId;
        }
Exemple #5
0
        /// <summary>
        /// Collect the terms from the JsonLdObject and all its child properties.
        /// </summary>
        /// <param name="obj">The JsonLdObject.</param>
        /// <returns>The list of terms.</returns>
        private static IDictionary <string, object> GetTerms(IJsonLdObject obj)
        {
            var terms = new Dictionary <string, object>();

            if (obj?.Terms == null)
            {
                return(terms);
            }

            foreach (var key in obj.Terms.Keys)
            {
                terms[key] = obj.Terms[key];
            }

            // Check for terms already in the @context
            if (obj.Context is JArray context && context.Count > 1)
            {
                for (var index = 1; index < context.Count; index++)
                {
                    var contextObject = context[index] as JObject;
                    if (contextObject?.First is JProperty term)
                    {
                        terms[term.Name] = term.Value;
                    }
                }
            }

            // Walk the object to find embedded JsonLdObjects
            foreach (var propertyInfo in obj.GetType().GetTypeInfo().GetProperties())
            {
                if (!propertyInfo.CanRead)
                {
                    continue;
                }

                if (JsonLdObjectType.GetTypeInfo().IsAssignableFrom(propertyInfo.PropertyType))
                {
                    var propertyValue = (IJsonLdObject)propertyInfo.GetValue(obj, null);
                    var propertyTerms = GetTerms(propertyValue);
                    foreach (var key in propertyTerms.Keys)
                    {
                        terms[key] = propertyTerms[key];
                    }
                }
                else if (JsonLdObjectArrayType.GetTypeInfo().IsAssignableFrom(propertyInfo.PropertyType))
                {
                    var propertyValue = (IEnumerable <IJsonLdObject>)propertyInfo.GetValue(obj, null);
                    if (propertyValue != null)
                    {
                        foreach (var jsonLdObject in propertyValue)
                        {
                            var propertyTerms = GetTerms(jsonLdObject);
                            foreach (var key in propertyTerms.Keys)
                            {
                                terms[key] = propertyTerms[key];
                            }
                        }
                    }
                }
            }
            return(terms);
        }