简体   繁体   English

根据集合属性名称为集合中的对象生成C#类名称

[英]generating C# class names for objects in collections according to collection property name

I'm trying to use NJsonSchema to generate C# classes, but it's naming the classes for objects in an array "anonymous_". 我正在尝试使用NJsonSchema生成C#类,但是它在数组“ anonymous_”中为对象命名类。

For example, this json schema snippet 例如,此json模式片段

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

Generates this C# property and class 生成此C#属性和类

[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 (eg 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 ). 想知道是否有一种方法可以强制NJsonSchema根据集合名称的单数形式来命名集合中的对象(例如,在这种情况下,我希望将anonymous类命名为Identifier之类的Identifier因为该类描述了称为Identifiers )。 I've tried using a custom TypeNameGenerator for this but the name of the collection isn't provided to the Generate function. 我尝试为此使用自定义TypeNameGenerator,但是没有将集合的名称提供给Generate函数。

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);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM