private static void WriteObject( object value, JObject token, DescriptiveJsonWriter writer, JsonSerializer serializer ) { var descriptions = new Dictionary <string, string>(); var childrenValues = new Dictionary <string, object?>(); // Get all the property descriptions foreach (var property in value.GetType().GetProperties()) { DescriptiveJsonConverter.GetMemberData( property, property.GetValue(value), childrenValues, descriptions ); } // Get all the field descriptions foreach (var field in value.GetType().GetFields()) { DescriptiveJsonConverter.GetMemberData( field, field.GetValue(value), childrenValues, descriptions ); } // Write the object writer.WriteStartObject(); foreach (var property in token.Properties()) { // Write the property's description if (descriptions.TryGetValue(property.Name, out var description)) { writer.WritePropertyComment(description); } // Write the property's name writer.WritePropertyName(property.Name); if (childrenValues.TryGetValue(property.Name, out var childValue)) { // Write the child object serializer.Serialize(writer, childValue); } else { // Write the value property.Value.WriteTo(writer, serializer.Converters.ToArray()); } } writer.WriteEndObject(); }
public override void WriteJson(JsonWriter writer, object?value, JsonSerializer serializer) { if (value is null || writer is not DescriptiveJsonWriter commentableWriter) { this.enabled = false; serializer.Serialize(writer, value); this.enabled = true; return; } // Serialize the object normally this.enabled = false; var token = JObject.FromObject(value); this.enabled = true; // Write the object DescriptiveJsonConverter.WriteObject(value, token, commentableWriter, serializer); }