Пример #1
0
        private async Task <IDictionary <string, IRelationshipObject> > DeserializeRelationships(JsonReader reader, string currentPath)
        {
            if (reader.TokenType != JsonToken.StartObject)
            {
                throw new DeserializationException("Invalid relationship object", "Expected an object but encountered " + reader.TokenType, currentPath);
            }

            var relationships = new Dictionary <string, IRelationshipObject>();

            while (reader.Read())
            {
                if (reader.TokenType == JsonToken.EndObject)
                {
                    break;
                }

                var relationshipName = (string)reader.Value;
                reader.Read();

                var relationship = await RelationshipObjectFormatter.Deserialize(reader, currentPath + "/" + relationshipName);

                relationships.Add(relationshipName, relationship);
            }

            return(relationships);
        }
Пример #2
0
        public Task Serialize(IResourceObject resourceObject, JsonWriter writer)
        {
            if (resourceObject == null)
            {
                writer.WriteNull();
                return(Task.FromResult(0));
            }

            writer.WriteStartObject();

            writer.WritePropertyName(TypeKeyName);
            writer.WriteValue(resourceObject.Type);
            writer.WritePropertyName(IdKeyName);
            writer.WriteValue(resourceObject.Id);

            if (resourceObject.Attributes != null && resourceObject.Attributes.Any())
            {
                writer.WritePropertyName(AttributesKeyName);
                writer.WriteStartObject();
                foreach (var attribute in resourceObject.Attributes)
                {
                    writer.WritePropertyName(attribute.Key);
                    if (attribute.Value == null)
                    {
                        writer.WriteNull();
                    }
                    else
                    {
                        if (attribute.Value.Type == JTokenType.Integer)
                        {
                            var ulongValue = attribute.Value.Value <ulong>();
                            if (ulongValue > long.MaxValue)
                            {
                                writer.WriteRawValue(ulongValue.ToString());
                                continue;
                            }
                        }
                        attribute.Value.WriteTo(writer);
                    }
                }
                writer.WriteEndObject();
            }

            if (resourceObject.Relationships != null)
            {
                var relationshipsToRender = resourceObject.Relationships.Where(r => r.Value != null).ToArray();
                if (relationshipsToRender.Any())
                {
                    writer.WritePropertyName(RelationshipsKeyName);
                    writer.WriteStartObject();
                    foreach (var relationship in relationshipsToRender)
                    {
                        if (relationship.Value == null)
                        {
                            continue;
                        }
                        writer.WritePropertyName(relationship.Key);
                        RelationshipObjectFormatter.Serialize(relationship.Value, writer);
                    }
                    writer.WriteEndObject();
                }
            }

            if (resourceObject.SelfLink != null)
            {
                writer.WritePropertyName(LinksKeyName);
                writer.WriteStartObject();
                writer.WritePropertyName(SelfLinkKeyName);
                LinkFormatter.Serialize(resourceObject.SelfLink, writer);
                writer.WriteEndObject();
            }

            if (resourceObject.Metadata != null)
            {
                writer.WritePropertyName(MetaKeyName);
                MetadataFormatter.Serialize(resourceObject.Metadata, writer);
            }

            writer.WriteEndObject();

            return(Task.FromResult(0));
        }