Closest I've come to the desired generator is by using a library NJsonSchema. Thanks to @Helen for giving me valuable directions to Json Schema. Usage example below:
public string Generate(Type type)
{
return JsonSchema
.FromType(type)
.ToSampleJson()
.ToString();
}
It isn't perfect though. For example, for model defined as follows:
public class User
{
public string String { get; set; }
public int Int { get; set; }
public double Double { get; set; }
public float Float { get; set; }
public short Short { get; set; }
public byte Byte { get; set; }
public long Long { get; set; }
public decimal Decimal { get; set; }
public DateTime DateTime { get; set; }
public TimeSpan TimeSpan { get; set; }
public bool Bool { get; set; }
public BindingFlags Enum2 { get; set; }
public Blog NestedObject { get; set; }
public Blog[] ArrayOfObjects { get; set; }
public int[] ArrayOfValues { get; set; }
}
public class Blog
{
public int Id { get; set; }
public string Name { get; set; }
}
it generates a sample JSON:
{
"String": "String",
"Int": 0,
"Double": 0,
"Float": 0,
"Short": 0,
"Byte": 0,
"Long": 0,
"Decimal": 0,
"DateTime": "2020-08-02T19:43:59.8759099+00:00",
"TimeSpan": "TimeSpan",
"Bool": false,
"Enum2": 0,
"NestedObject": null,
"ArrayOfObjects": [
{
"Id": 0,
"Name": "Name"
}
],
"ArrayOfValues": [
0
]
}
Sadly NestedObject
property is null
when it shouldn't be and floating point types should have example value suggesting they are indeed floating type, not just 0
suggesting that they are of integer type. So yeah, it could be improved. But it's definitely better than nothing!