Having a dilemma what to choose Flexible and Extendable JSON or Strongly Typed Class? Take advantage of both. Extendable Data implementation which will help you extend your strongly typed classes with additional data which could be validated against configured JSON Schema.
Get yourself familiar with JSON Schema. Special thanks to Newtonsoft.
classFlexibleEntity:V.Udodov.Json.Entity{
...}
...
string flexibleJsonSchema=@"{ 'type': 'object', 'properties': { 'shoe_size': { 'type': 'number', 'minimum': 5, 'maximum': 12, 'multipleOf': 1.0 } } }";FlexibleEntityentity=newFlexibleEntity{ExtensionDataJsonSchema=flexibleJsonSchema};entity["shoe_size"]=20;// throws a JsonEntityValidationExceptionentity["shoe_size"]=8.5;Console.WriteLine(entity["shoe_size"]);// prints "8.5"Console.WriteLine(entity.JsonSchema);// will return full JSON Schema of an object. Extensible and static partsBuilt in protection prevents JSON Schema taking over control of your class properties definition. And will result in exception if JSON Schema will try to override property of the class. Meaning the Json Schema must contain only declaration for extension data. There should be no collisions in class and extensible data declarations.
classFlexibleEntity:V.Udodov.Json.Entity{publicstringName{get;set;}
...}
...
string flexibleJsonSchema=@"{ 'type': 'object', 'properties': { 'name': { 'type': 'string', 'minLength': 3 } } }";// Code below throws JsonSchemaValidationException because both class and JSON Schema declarations// have name property.FlexibleEntityentity=newFlexibleEntity{ExtensionDataJsonSchema=flexibleJsonSchema};No schema- no problems. Use your object as extensible flexible entity.
classFlexibleEntity:V.Udodov.Json.Entity{publicstringName{get;set;}
...}
...FlexibleEntity entity =newFlexibleEntity{entity["pants_size"]=49;}
Console.WriteLine(entity["pants_size"]);// prints "49"entity["middle_name"]="peanut";Console.WriteLine(entity["middle_name"]);// prints "peanut"Easily get JSON string from your class instance
classFlexibleEntity:V.Udodov.Json.Entity{publicstringName{get;set;}
...}
...FlexibleEntity entity =newFlexibleEntity{Name="Peter Parker",["pants_size"]=49};Console.WriteLine(entity);/*prints{ name: "Peter Parker", pants_size: 49}*/