Exemple #1
0
        private JToken SerializeRelationshipData(ResourceGraphNode node, ResourceGraphRelationship relationship)
        {
            // check if the relationship property exists on the underlying model and if not bail with null
            // NOTE: this logic refers to https://github.com/joukevandermaas/saule/issues/159
            if (node.SourceObject.GetType().GetProperty(relationship.Relationship.PropertyName) == null)
            {
                return(null);
            }

            if (relationship.Relationship.Kind == RelationshipKind.BelongsTo)
            {
                if (relationship.SourceObject == null)
                {
                    return(JValue.CreateNull());
                }

                return(JObject.FromObject(new ResourceGraphNodeKey(relationship.SourceObject, relationship.Relationship.RelatedResource)));
            }

            if (relationship.Relationship.Kind == RelationshipKind.HasMany)
            {
                var content = new JArray();

                foreach (var sourceObject in (IEnumerable)relationship.SourceObject ?? new JArray())
                {
                    content.Add(JObject.FromObject(new ResourceGraphNodeKey(sourceObject, relationship.Relationship.RelatedResource)));
                }

                return(content);
            }

            return(null);
        }
Exemple #2
0
        private JToken SerializeRelationshipData(ResourceGraphRelationship relationship)
        {
            if (!relationship.Included)
            {
                return(null);
            }
            else if (relationship.Relationship.Kind == RelationshipKind.BelongsTo)
            {
                if (relationship.SourceObject == null)
                {
                    return(JValue.CreateNull());
                }
                else
                {
                    return(JObject.FromObject(new ResourceGraphNodeKey(relationship.SourceObject, relationship.Relationship.RelatedResource)));
                }
            }
            else if (relationship.Relationship.Kind == RelationshipKind.HasMany)
            {
                var content = new JArray();
                foreach (var o in (System.Collections.IEnumerable)relationship.SourceObject ?? new object[0])
                {
                    content.Add(JObject.FromObject(new ResourceGraphNodeKey(o, relationship.Relationship.RelatedResource)));
                }

                return(content);
            }
            else
            {
                return(null);
            }
        }