0

My application generates C# classes from json schema files, using the NJSonSchema library and the included CSharpGenerator. The main json schema file contains a reference to a local secondary json schema file, available in the same directory. The main json schema file "MyClass_schema_v0.01.json":

    "$schema": "http://json-schema.org/schema#",
    "$id": "MyClass_schema_v0.01.json",
    "title": "My class structure",
    "description": "My class",
    "type": "object",
    "properties": {
        "type": {
            "description": "Type of action.",
            "type": "string",
            "minLength": 4,
            "maxLength": 4,
            "pattern": "^Act$"
        },
        "date": {
            "description": "Date.",
            "type": "string",
            "format": "date"
        },
        "time": {
            "description": "Timestamp.",
            "type": "string",
            "format": "date-time"
        },
        "userId": {
            "description": "User id.",
            "type": "string",
            "maxLength": 8
        },
        "numberOfItems": {
            "description": "Number of items.",
            "type": "integer"
        },
        "info": {
            "description": "Comment entered by the user.",
            "type": "string",
            "maxLength": 180
        },
        "error": {
            "description": "Error.",
            "$ref": "MyExtClass_schema_v0.01.json"
        }
    },
    "required": [
        "type",
        "date",
        "time",
        "userId",
        "numberOfItems"
    ],
    "additionalProperties": true
}

The referenced json schema file "MyExtClass_schema_v0.01.json":

{
    "$schema": "http://json-schema.org/schema#",
    "$id": "MyExtClass_schema_v0.01.json",
    "title": "Error structure",
    "description": "Error structure",
    "type": "object",
    "properties": {
        "code": {
            "description": "Error code. Begins with ERR or EXC",
            "type": "string",
            "maxLength": 8,
            "pattern": "^ERR[A-Za-z0-9]+|EXC[A-Za-z0-9]+$"
        },
        "description": {
            "description": "Error description.",
            "type": "string",
            "maxLength": 180
        }
    },
    "required": [
        "code",
        "description"
    ],
    "additionalProperties": false
}

The code that generates the c# classes:

    var jsonMyExtClassSchema = File.ReadAllText($"Schemas/MyExtClass_schema_v0.01.json");
    var jsonMyExtClassSchemaFromFile = await JsonSchema.FromJsonAsync(jsonMyExtClassSchema);

    var jsonSchemaMyClass = File.ReadAllText($"Schemas/MyClass_schema_v0.01.json");

    var schemaMyClassFromFile = await JsonSchema.FromJsonAsync(
        jsonSchemaMyClass,
        ".",
        schema4 =>
        {
            JsonSchemaResolver schemaResolver = new JsonSchemaResolver(
                schema4,
                new JsonSchemaGeneratorSettings());

            var resolver = new JsonReferenceResolver(schemaResolver);
            resolver.AddDocumentReference("MyExtClass_schema_v0.01.json", jsonMyExtClassSchemaFromFile);

            return resolver;
        }

        );
    var classGeneratorMyClass = new CSharpGenerator(schemaMyClassFromFile, new CSharpGeneratorSettings
    {
        ClassStyle = CSharpClassStyle.Poco,
        JsonLibrary = CSharpJsonLibrary.SystemTextJson,
        Namespace = "MyClass.DataModel"
    });
    var codeFileMyClass = classGeneratorMyClass.GenerateFile("MyClass");
    File.WriteAllText("MyClass.cs", codeFileMyClass);

The generated MyClass.cs file:

namespace MyClass.DataModel
{
    #pragma warning disable // Disable all warnings

    /// <summary>
    /// Error structure
    /// </summary>
    [System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "10.7.2.0 (Newtonsoft.Json v9.0.0.0)")]
    public partial class MyExtClass_schema_v0
    {
        /// <summary>
        /// Error code. Begins with ERR or EXC
        /// </summary>

        [System.Text.Json.Serialization.JsonPropertyName("code")]

        [System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.Never)]   
        public string Code { get; set; }

        /// <summary>
        /// Error description.
        /// </summary>

        [System.Text.Json.Serialization.JsonPropertyName("description")]

        [System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.Never)]   
        public string Description { get; set; }
    }


    /// <summary>
    /// My class structure
    /// </summary>
    [System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "10.7.2.0 (Newtonsoft.Json v9.0.0.0)")]
    public partial class MyClass
    {
        /// <summary>
        /// Type of action.
        /// </summary>

        [System.Text.Json.Serialization.JsonPropertyName("type")]

        [System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.Never)]   
        public string Type { get; set; }

        /// <summary>
        /// Date.
        /// </summary>

        [System.Text.Json.Serialization.JsonPropertyName("date")]

        [System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.Never)]   
        [System.Text.Json.Serialization.JsonConverter(typeof(DateFormatConverter))]
        public System.DateTimeOffset Date { get; set; }

        /// <summary>
        /// Timestamp.
        /// </summary>

        [System.Text.Json.Serialization.JsonPropertyName("time")]

        [System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.Never)]   
        public System.DateTimeOffset Time { get; set; }

        /// <summary>
        /// User id.
        /// </summary>

        [System.Text.Json.Serialization.JsonPropertyName("userId")]

        [System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.Never)]   
        public string UserId { get; set; }

        /// <summary>
        /// Number of items.
        /// </summary>

        [System.Text.Json.Serialization.JsonPropertyName("numberOfItems")]

        [System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.Never)]   
        public int NumberOfItems { get; set; }

        /// <summary>
        /// Comment entered by the user.
        /// </summary>

        [System.Text.Json.Serialization.JsonPropertyName("info")]

        [System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull)]   
        public string Info { get; set; }

        /// <summary>
        /// Error.
        /// </summary>

        [System.Text.Json.Serialization.JsonPropertyName("error")]

        [System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull)]   
        public MyExtClass_schema_v0 Error { get; set; }

        private System.Collections.Generic.IDictionary<string, object> _additionalProperties = new System.Collections.Generic.Dictionary<string, object>();

        [System.Text.Json.Serialization.JsonExtensionData]
        public System.Collections.Generic.IDictionary<string, object> AdditionalProperties
        {
            get { return _additionalProperties; }
            set { _additionalProperties = value; }
        }

    }

The type name of the external class from the referenced json schema gets "by default" the part before the dot from the name of the external jsonschema file (MyExtClass_schema_v0.01.json -> MyExtClass_schema_v0). Is there any setting that allows changing the external class name ? Is it possible to the configure the csharp generator to generate the external class in a separate file? Do the referenced json schema files always have to be explicitly added to the JsonReferenceResolver by calling AddDocumentReference(...), even if the reference files are available locally in the same directory as the "main" json schema file that contains the ref?

Abra Sat
  • 11
  • 2

0 Answers0