public override void Write(Utf8JsonWriter writer, TC value, JsonSerializerOptions options) { if (value == null) { writer.WriteNullValue(); return; } using (new DepthHandler(options)) { ReferenceHandler referenceResolver = null; if (_referenceHandling == ReferenceHandling.Ignore) { referenceResolver = SerializationContext.Current.ReferenceHandler; } else if (_referenceHandling == ReferenceHandling.Preserve) { writer.WriteStartObject(); referenceResolver = SerializationContext.Current.ReferenceHandler; string reference = referenceResolver.GetReference(value, out bool firstReference); if (firstReference) { writer.WriteString(ReferenceHandler.ID_MEMBER_NAME, reference); writer.WritePropertyName(ReferenceHandler.VALUES_MEMBER_NAME); } else { writer.WriteString(ReferenceHandler.REF_MEMBER_NAME, reference); writer.WriteEndObject(); return; } } writer.WriteStartArray(); foreach (TI item in value) { if (_referenceHandling == ReferenceHandling.Ignore) { if (referenceResolver.IsReferenced(item)) { continue; } else { referenceResolver.AddReference(item); } } _itemConverter.Write(writer, item, options); } writer.WriteEndArray(); if (_referenceHandling == ReferenceHandling.Preserve) { writer.WriteEndObject(); } } }