public async Task Serialize_link_without_metadata() { ILink link = new Link("http://www.example.com", null); var formatter = new LinkFormatter(null); await AssertSerializeOutput(formatter, link, "Json/Fixtures/LinkFormatter/Serialize_link_without_metadata.json"); }
public async Task Serialize_link_with_metadata() { var mockMetadataFormatter = new Mock<IMetadataFormatter>(MockBehavior.Strict); mockMetadataFormatter.Setup(m => m.Serialize(It.IsAny<IMetadata>(), It.IsAny<JsonWriter>())) .Returns((IMetadata metadata, JsonWriter writer) => { writer.WriteValue("IMetadata placeholder 1"); return Task.FromResult(0); }).Verifiable(); var mockMetadata = new Mock<IMetadata>(MockBehavior.Strict); ILink link = new Link("http://www.example.com", mockMetadata.Object); var formatter = new LinkFormatter(mockMetadataFormatter.Object); await AssertSerializeOutput(formatter, link, "Json/Fixtures/LinkFormatter/Serialize_link_with_metadata.json"); mockMetadataFormatter.Verify(s => s.Serialize(mockMetadata.Object, It.IsAny<JsonWriter>()), Times.Once); }
/// <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(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)); }