0

我正在寻找将一个简单的 json 模式转换为 ac# 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; }


    }
}
4

1 回答 1

0

从我能找到的。这对于 NJsonSchema 是不可能的。

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

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

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

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

于 2022-02-12T12:22:43.880 回答