public Task Serialize(ISingleResourceDocument document, JsonWriter writer) { writer.WriteStartObject(); writer.WritePropertyName(PrimaryDataKeyName); _resourceObjectFormatter.Serialize(document.PrimaryData, writer); if (document.RelatedData != null && document.RelatedData.Any()) { writer.WritePropertyName(RelatedDataKeyName); writer.WriteStartArray(); foreach (var resourceObject in document.RelatedData) { _resourceObjectFormatter.Serialize(resourceObject, writer); } writer.WriteEndArray(); } if (document.Metadata != null) { writer.WritePropertyName(MetaKeyName); _metadataFormatter.Serialize(document.Metadata, writer); } writer.WriteEndObject(); return(Task.FromResult(0)); }
/// <summary> /// Serializes the relationship object's metadata. /// </summary> protected virtual void SerializeMetadata(IRelationshipObject relationshipObject, JsonWriter writer) { if (relationshipObject.Metadata != null) { writer.WritePropertyName(MetaKeyName); _metadataFormatter.Serialize(relationshipObject.Metadata, writer); } }
public Task Serialize(ILink link, JsonWriter writer) { if (link.Metadata == null) { writer.WriteValue(link.Href); } else { writer.WriteStartObject(); writer.WritePropertyName(HrefKeyName); writer.WriteValue(link.Href); writer.WritePropertyName(MetaKeyName); _metadataFormatter.Serialize(link.Metadata, writer); writer.WriteEndObject(); } return(Task.FromResult(0)); }
public Task Serialize(IErrorDocument document, JsonWriter writer) { writer.WriteStartObject(); writer.WritePropertyName("errors"); writer.WriteStartArray(); foreach (var error in document.Errors) { _errorFormatter.Serialize(error, writer); } writer.WriteEndArray(); if (document.Metadata != null) { writer.WritePropertyName("meta"); _metadataFormatter.Serialize(document.Metadata, writer); } writer.WriteEndObject(); return(Task.FromResult(0)); }
public Task Serialize(IError error, JsonWriter writer) { writer.WriteStartObject(); if (error.Id != null) { writer.WritePropertyName("id"); writer.WriteValue(error.Id); } if (error.AboutLink != null) { writer.WritePropertyName("links"); writer.WriteStartObject(); writer.WritePropertyName("about"); _linkFormatter.Serialize(error.AboutLink, writer); writer.WriteEndObject(); } if (error.Status != default(HttpStatusCode)) { writer.WritePropertyName("status"); writer.WriteValue(((int)error.Status).ToString()); } if (error.Code != null) { writer.WritePropertyName("code"); writer.WriteValue(error.Code); } if (error.Title != null) { writer.WritePropertyName("title"); writer.WriteValue(error.Title); } if (error.Detail != null) { writer.WritePropertyName("detail"); writer.WriteValue(error.Detail); } if (error.Pointer != null || error.Parameter != null) { writer.WritePropertyName("source"); writer.WriteStartObject(); if (error.Pointer != null) { writer.WritePropertyName("pointer"); writer.WriteValue(error.Pointer); } if (error.Parameter != null) { writer.WritePropertyName("parameter"); writer.WriteValue(error.Parameter); } writer.WriteEndObject(); } if (error.Metadata != null) { writer.WritePropertyName("meta"); _metadataFormatter.Serialize(error.Metadata, writer); } writer.WriteEndObject(); return(Task.FromResult(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)); }