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 void Serialize <TModel>( TModel data, StreamWriter outputStream, Action <JsonSerializerSettings>?settings, bool minify = false ) where TModel : class { // Write to stream directly using the custom JSON writer without closing the stream using var writer = new DescriptiveJsonWriter(outputStream) { Minify = minify }; // Setup JSON Settings var clonedSettings = CommentedJsonProvider.CloneSettings(this.jsonSettings); settings?.Invoke(clonedSettings); // Serialize JsonSerializer.CreateDefault(clonedSettings).Serialize(writer, data); writer.Flush(); }