首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将json模式转换为没有属性的c# poco

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

Stack Overflow用户
提问于 2022-02-12 09:10:16
回答 1查看 241关注 0票数 0

我希望将一个简单的json模式转换为一个c# poco。

代码语言:javascript
复制
{
  "$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验证run.If (需要或空)让poco填充,然后允许我序列化它,然后在对模式的json验证中可能失败。

NJsonSchema在许多方面都是完美的,但是我无法找到一种在生成时废除属性JsonProperty的方法。

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

代码语言:javascript
复制
 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);
        }

下面是生成的类

代码语言:javascript
复制
//----------------------
// <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; }


    }
}
EN

回答 1

Stack Overflow用户

发布于 2022-02-12 12:22:43

据我所能找到的。这在NJsonSchema中是不可能的。

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

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

我的解决方案是使用vim编辑器并创建一个宏,用于删除我在/[System和/[Newtonsoft“中找到的内容。

这将移除注释,并为我提供一个干净的POCO类。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71090460

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档