public override void Write(Utf8JsonWriter writer, AnonymousResourceCollection value, JsonSerializerOptions options) { var tracked = new TrackedResources(); writer.WriteStartObject(); writer.WritePropertyName(JsonApiMembers.DataEncoded); WriteWrapped(writer, ref tracked, value, options); if (tracked.Count > 0) { writer.WritePropertyName(JsonApiMembers.IncludedEncoded); writer.WriteStartArray(); var index = 0; while (index < tracked.Count) { var included = tracked.Get(index); included.Converter.Write(writer, ref tracked, included.Value, options); index++; } writer.WriteEndArray(); } writer.WriteEndObject(); }
private void WriteRelationships(Utf8JsonWriter writer, JsonTypeInfo info, ref TrackedResources tracked, object value) { var relationshipsWritten = false; foreach (var member in info.AttributeMembers) { if (member.IsRelationship) { member.WriteRelationship(writer, ref tracked, value, ref relationshipsWritten); } } for (var i = 0; i < tracked.Relationships.Count; i++) { var relationship = tracked.Relationships.Get(i); if (!relationshipsWritten) { writer.WritePropertyName(JsonApiMembers.RelationshipsEncoded); writer.WriteStartObject(); relationshipsWritten = true; } writer.WritePropertyName(relationship.Name); writer.WriteStartObject(); writer.WritePropertyName(JsonApiMembers.DataEncoded); writer.WriteStartArray(); for (var j = 0; j < tracked.Count; j++) { var included = tracked.Get(j); if (included.IsUnwritten() && included.RelationshipId == relationship.Id) { writer.WriteStartObject(); writer.WriteString(JsonApiMembers.IdEncoded, included.Identifier.Id); writer.WriteString(JsonApiMembers.TypeEncoded, included.Identifier.Type); writer.WriteEndObject(); } } writer.WriteEndArray(); writer.WriteEndObject(); } if (relationshipsWritten) { writer.WriteEndObject(); } tracked.Relationships.Clear(); }