【问题标题】:Convert json schema to a c# poco without attributes将 json 模式转换为没有属性的 c# poco
【发布时间】:2022-02-12 20:22:43
【问题描述】:

我希望将简单的 json 模式转换为 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
    }
  }
}

我使用 NJsonSchema 来执行此操作,它做得很好。但是,我希望创建没有任何在序列化时会失败的属性的 POCO。我想填充 C# 对象,然后让 json 模式验证运行。如果需要某些东西或 null 让 poco 填充,然后允许我对其进行序列化,然后可能会在针对模式的 json 验证上失败。

NJsonSchema 在很多方面都很完美,但是我找不到在生成时取消属性 JsonProperty 的方法。

这是我的 NJsonSchema 代码生成代码。

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

这是生成的类

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


    }
}

【问题讨论】:

    标签: c# json .net jsonschema njsonschema


    【解决方案1】:

    据我所知。 NJsonSchema 无法做到这一点。

    https://github.com/RicoSuter/NJsonSchema/issues/521

    https://github.com/RicoSuter/NSwag/issues/2213

    我的解决方案是使用 vim 编辑器并创建一个宏来删除我在 /[System 和 /[Newtonsoft] 上找到的内容

    这会删除注释并给我一个干净的 POCO 类。

    【讨论】:

      猜你喜欢
      • 2015-09-22
      • 2011-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-08
      • 1970-01-01
      • 2021-10-26
      • 2013-09-15
      相关资源
      最近更新 更多