/// <summary> /// Serializes the relationship object's links. /// </summary> protected virtual void SerializeLinks(IRelationshipObject relationshipObject, JsonWriter writer) { if (relationshipObject.SelfLink != null || relationshipObject.RelatedResourceLink != null) { writer.WritePropertyName(LinksKeyName); writer.WriteStartObject(); if (relationshipObject.SelfLink != null) { writer.WritePropertyName(SelfLinkKeyName); _linkFormatter.Serialize(relationshipObject.SelfLink, writer); } if (relationshipObject.RelatedResourceLink != null) { writer.WritePropertyName(RelatedLinkKeyName); _linkFormatter.Serialize(relationshipObject.RelatedResourceLink, writer); } writer.WriteEndObject(); } }
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)); }