using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using System.Globalization;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
public static partial class JsonExtensions
readonly static IContractResolver defaultResolver = new JsonSerializer().ContractResolver;
public static string WriteDefaultValuesForTypeToString(Type type, IContractResolver resolver = null, Formatting formatting = Formatting.None)
using (var sw = new StringWriter())
using (var jsonWriter = new JsonTextWriter(sw) { Formatting = formatting })
WriteDefaultValuesForType(type, jsonWriter, resolver);
public static void WriteDefaultValuesForType(Type type, JsonWriter writer, IContractResolver resolver = null)
var serializer = JsonSerializer.Create(new JsonSerializerSettings{ ContractResolver = resolver });
WriteDefaultValuesForType(type, writer, serializer, null, 0);
static void WriteDefaultValuesForType(Type type, JsonWriter writer, JsonSerializer serializer, JsonProperty parent, int depth)
var contract = serializer.ContractResolver.ResolveContract(type);
var defaultValue = parent?.DefaultValue;
if (defaultValue == null && type.IsValueType && Nullable.GetUnderlyingType(type) == null)
defaultValue = contract.DefaultCreator();
if (contract is JsonPrimitiveContract primitive)
serializer.Serialize(writer, defaultValue);
else if (contract is JsonObjectContract obj)
if (depth > 0 && defaultValue == null)
writer.WriteStartObject();
foreach (var p in obj.Properties)
writer.WritePropertyName(p.PropertyName);
WriteDefaultValuesForType(p.PropertyType, writer, serializer, p, depth++);
else if (contract is JsonArrayContract array)
writer.WriteStartArray();
else if (contract is JsonDictionaryContract dict)
if (depth > 0 && defaultValue == null)
writer.WriteStartObject();
else if (contract is Newtonsoft.Json.Serialization.JsonLinqContract linq)
throw new JsonSerializationException(string.Format("Unsupported contract {0}", contract));
[DefaultValue("My Default Value")]
public string StringValue { get; set; } = "My Default Value";
public TestClass child {get;set;}
public List<int> IntList { get; set; }
public DateTime DateTime { get; set; }
public static void Test()
var defaultJson = JsonExtensions.WriteDefaultValuesForTypeToString(typeof(TestClass), formatting : Formatting.Indented);
Console.WriteLine(defaultJson);
var resolver3 = new CamelCasePropertyNamesContractResolver();
var defaultJson3 = JsonExtensions.WriteDefaultValuesForTypeToString(typeof(TestClass), resolver3, formatting : Formatting.Indented);
Console.WriteLine(defaultJson3);
var resolver2 = new DefaultContractResolver() { IgnoreSerializableAttribute = false };
var defaultJson2 = JsonExtensions.WriteDefaultValuesForTypeToString(typeof(TestClass), resolver2, formatting : Formatting.Indented);
Console.WriteLine(defaultJson2);
public static void Main()
Console.WriteLine("Environment version: {0} ({1})", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription , GetNetCoreVersion());
Console.WriteLine("Json.NET version: " + typeof(JsonSerializer).Assembly.FullName);
Console.WriteLine("Failed with unhandled exception: ");
public static string GetNetCoreVersion()
var assembly = typeof(System.Runtime.GCSettings).GetTypeInfo().Assembly;
var assemblyPath = assembly.CodeBase.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
int netCoreAppIndex = Array.IndexOf(assemblyPath, "Microsoft.NETCore.App");
if (netCoreAppIndex > 0 && netCoreAppIndex < assemblyPath.Length - 2)
return assemblyPath[netCoreAppIndex + 1];