I'm looking to convert a simple json schema into a c# poco.
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Test_Schema",
"description": "A schema for validating a test object",
"type": "object",
"additionalProperties": false,
"properties": {
"GeneralData": {
"type": "object",
"description": "Advsor and admin customer information",
"properties": {
"Name": {
"type": [ "string", "null" ],
"description": "Customer's advisor name"
},
"Age": {
"type": [ "string", "null" ],
"description": "Customer's advisor email"
},
"Location": {
"type": [ "string", "null" ],
"description": "The advisor's manager email'"
}
},
"required": [ "Name", "Location", "Age" ],
"additionalProperties": false
},
"ClientData": {
"type": "object",
"description": "Customer's information",
"properties": {
"Title": {
"type": [ "string", "null" ]
},
"Forename": {
"type": [ "string", "null" ]
},
"Surname": {
"type": [ "string", "null" ]
}
},
"required": [ "Title" ],
"if": {
"properties": {
"Forename": { "enum": [ "Someone" ] }
}
},
"then": { "required": [ "Surname" ] },
"additionalProperties": false
}
}
}
I was using NJsonSchema to do this and it does a great job. However, I'm looking to create the POCO without any attributes which would fail on the serialization. I want to populate the C# object and then let the json schema validation run.If something is required or null let the poco populate, then allow me to serialize it and then potentially fail on the json validation against the schema.
NJsonSchema is perfect in many ways, however I cannot find a way to abolish the attribute JsonProperty on generation.
Here is my NJsonSchema code generation code.
internal void WriteJsonToFile(JsonSchema jsonSchema)
{
var generator = new CSharpGenerator(jsonSchema)
{
Settings =
{
GenerateDataAnnotations = false,
RequiredPropertiesMustBeDefined = false,
EnforceFlagEnums = false,
ClassStyle = CSharpClassStyle.Poco,
GenerateNativeRecords = false,
Namespace = nameof(JsonValidation)+"."+ nameof(TestSchemaPoco),
SchemaType = SchemaType.JsonSchema,
PropertyNameGenerator = new CSharpPropertyNameGenerator{}
}
};
var file = generator.GenerateFile();
File.WriteAllText("somefile.cs", file);
}
Here is the class which is generated
//----------------------
// <auto-generated>
// Generated using the NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0) (http://NJsonSchema.org)
// </auto-generated>
//----------------------
namespace JsonValidation.TestSchemaPoco
{
#pragma warning disable // Disable all warnings
/// <summary>
/// A schema for validating a test object
/// </summary>
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "11.0.0.0 (Newtonsoft.Json v13.0.0.0)")]
public partial class Test_Schema
{
/// <summary>
/// Advsor and admin customer information
/// </summary>
[Newtonsoft.Json.JsonProperty("GeneralData", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public GeneralData GeneralData { get; set; }
/// <summary>
/// Customer's information
/// </summary>
[Newtonsoft.Json.JsonProperty("ClientData", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public ClientData ClientData { get; set; }
}
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "11.0.0.0 (Newtonsoft.Json v13.0.0.0)")]
public partial class GeneralData
{
/// <summary>
/// Customer's advisor name
/// </summary>
[Newtonsoft.Json.JsonProperty("Name", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public string Name { get; set; }
/// <summary>
/// Customer's advisor email
/// </summary>
[Newtonsoft.Json.JsonProperty("Age", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public string Age { get; set; }
/// <summary>
/// The advisor's manager email'
/// </summary>
[Newtonsoft.Json.JsonProperty("Location", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public string Location { get; set; }
}
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "11.0.0.0 (Newtonsoft.Json v13.0.0.0)")]
public partial class ClientData
{
[Newtonsoft.Json.JsonProperty("Title", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public string Title { get; set; }
[Newtonsoft.Json.JsonProperty("Forename", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public string Forename { get; set; }
[Newtonsoft.Json.JsonProperty("Surname", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public string Surname { get; set; }
}
}