0

I'm trying to use NJsonSchema to generate C# classes, but it's naming the classes for objects in an array "anonymous_".

For example, this json schema snippet

"Identifiers": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "ID": {
                "type": "string"
              },
              "IDType": {
                "type": "string"
              }
            },
            "required": [
              "ID",
              "IDType"
            ]
          }

Generates this C# property and class

        [Newtonsoft.Json.JsonProperty("Identifiers", Required = Newtonsoft.Json.Required.Always)]
        [System.ComponentModel.DataAnnotations.Required]
        public System.Collections.ObjectModel.ObservableCollection<Anonymous> Identifiers { get; set; } = new System.Collections.ObjectModel.ObservableCollection<Anonymous>();
    [System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "9.3.0.0")]
    public partial class Anonymous 
    {
        [Newtonsoft.Json.JsonProperty("ID", Required = Newtonsoft.Json.Required.Always)]
        [System.ComponentModel.DataAnnotations.Required]
        public string ID { get; set; }

        [Newtonsoft.Json.JsonProperty("IDType", Required = Newtonsoft.Json.Required.Always)]
        [System.ComponentModel.DataAnnotations.Required]
        public string IDType { get; set; }

        public string ToJson() 
        {
            return Newtonsoft.Json.JsonConvert.SerializeObject(this);
        }

        public static Anonymous FromJson(string data)
        {
            return Newtonsoft.Json.JsonConvert.DeserializeObject<Anonymous>(data);
        }
    }

Wondering if there's a way to force NJsonSchema to name objects in a collection according to a singularized version of the collection name (e.g. in this case I'd want the anonymous class to be named something like Identifier since the class describes objects in a collection called Identifiers). I've tried using a custom TypeNameGenerator for this but the name of the collection isn't provided to the Generate function.

chennen
  • 1
  • 1

1 Answers1

2

If you can modify the schema, then you can do the following:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "Identifiers": {
      "type": "array",
      "items": {
        "allOf": [
          {
            "$ref": "#/definitions/Identifier"
          }
        ]
      }
    }
  },
  "required": [
    "Identifiers"
  ],
  "definitions": {
    "Identifier": {
      "type": "object",
      "x-typeName": "Identifier",
      "properties": {
        "ID": {
          "type": "string"
        },
        "IDType": {
          "type": "string"
        }
      },
      "required": [
        "ID",
        "IDType"
      ]
    }
  }
}

Tested with the below program:

public static async Task Main(string[] args)
{
    var json = File.ReadAllText("json-schema-sample.json");

    var schema = await JsonSchema4.FromJsonAsync(json);

    var csharpSetting = new CSharpGeneratorSettings {Namespace = "Generated.Json", ClassStyle = CSharpClassStyle.Poco};
    var generator = new CSharpGenerator(schema, csharpSetting);
    var file = generator.GenerateFile("Root");

    using (var sw = File.CreateText("Generated.cs"))
    {
        sw.Write(file);
    }
}

And it generates the below classes:

//----------------------
// <auto-generated>
//     Generated using the NJsonSchema v9.7.7.0 (Newtonsoft.Json v9.0.0.0) (http://NJsonSchema.org)
// </auto-generated>
//----------------------

namespace Generated.Json
{
    #pragma warning disable // Disable all warnings

    [System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "9.7.7.0 (Newtonsoft.Json v9.0.0.0)")]
    public partial class Identifier 
    {
        [Newtonsoft.Json.JsonProperty("ID", Required = Newtonsoft.Json.Required.Always)]
        [System.ComponentModel.DataAnnotations.Required]
        public string ID { get; set; }

        [Newtonsoft.Json.JsonProperty("IDType", Required = Newtonsoft.Json.Required.Always)]
        [System.ComponentModel.DataAnnotations.Required]
        public string IDType { get; set; }

        public string ToJson() 
        {
            return Newtonsoft.Json.JsonConvert.SerializeObject(this);
        }

        public static Identifier FromJson(string data)
        {
            return Newtonsoft.Json.JsonConvert.DeserializeObject<Identifier>(data);
        }
    }

    [System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "9.7.7.0 (Newtonsoft.Json v9.0.0.0)")]
    public partial class Root 
    {
        [Newtonsoft.Json.JsonProperty("Identifiers", Required = Newtonsoft.Json.Required.Always)]
        [System.ComponentModel.DataAnnotations.Required]
        public System.Collections.ObjectModel.ObservableCollection<Identifier> Identifiers { get; set; } = new System.Collections.ObjectModel.ObservableCollection<Identifier>();

        public string ToJson() 
        {
            return Newtonsoft.Json.JsonConvert.SerializeObject(this);
        }

        public static Root FromJson(string data)
        {
            return Newtonsoft.Json.JsonConvert.DeserializeObject<Root>(data);
        }
    }
}
Szabolcs Dézsi
  • 8,743
  • 21
  • 29