简体   繁体   English

将 json 模式转换为没有属性的 c# poco

[英]Convert json schema to a c# poco without attributes

I'm looking to convert a simple json schema into a c# poco.我正在寻找将简单的 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
    }
  }
}

I was using NJsonSchema to do this and it does a great job.我使用 NJsonSchema 来做这件事,它做得很好。 However, I'm looking to create the POCO without any attributes which would fail on the serialization.但是,我希望创建没有任何序列化失败的属性的 POCO。 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.我想填充 C# object,然后让 json 架构验证运行。如果需要某些东西或 null 让 poco 填充,然后允许我序列化它,然后可能会在针对架构的 json 验证上失败。

NJsonSchema is perfect in many ways, however I cannot find a way to abolish the attribute JsonProperty on generation. NJsonSchema 在很多方面都是完美的,但是我找不到在生成时取消属性 JsonProperty 的方法。

Here is my NJsonSchema code generation code.这是我的 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);
        }

Here is the class which is generated这是生成的 class

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


    }
}

From what I can find.从我能找到的。 This is not possible with NJsonSchema.这对于 NJsonSchema 是不可能的。

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

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

My solution for this was to use the vim editor and create a macro for removing what I find on /[System and /[Newtonsoft我的解决方案是使用 vim 编辑器并创建一个宏来删除我在 /[System 和 /[Newtonsoft 上找到的内容

This removes the annotations and gives me a clean POCO class.这会删除注释并给我一个干净的 POCO class。

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

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