使用JSON.NET生成具有额外属性的JSON模式 [英] Use JSON.NET to generate JSON schema with extra attributes

查看:82
本文介绍了使用JSON.NET生成具有额外属性的JSON模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JSON.NET从c#对象类生成JSON模式.但是我无法添加其他任何json模式属性,例如maxLength,pattern(用于验证电子邮件的正则表达式)等

I am using JSON.NET to generate JSON Schema from c# object class. But I was unable to add any other json schema attributes e.g. maxLength, pattern(regex to validate email), etc

下面是我的工作代码,我只能生成具有必填属性的json模式.如果任何人都可以发布一些代码示例有关如何为json模式添加这些额外的属性,那将是很好的.

Below is my working code, I can only generate json schema with required attribute. It would be great if anyone can post some code example about how to add those extra attribute for json schema.

谢谢

我的代码示例

public class Customer
{
    [JsonProperty(Required = Required.Always)]
    public int CustomerID { get; set; }

    [JsonProperty(Required = Required.Always)]
    public string FirstName { get; set; }

    [JsonProperty(Required = Required.Always)]
    public string LastName { get; set; }

    [JsonProperty(Required = Required.Always)]
    public string Email { get; set; }

    [JsonProperty(Required = Required.AllowNull)]
    public string Phone { get; set; }
}

{
    "title" : "Customer",
    "type" : "object",
    "properties" : {
        "CustomerID" : {
            "required" : true,
            "type" : "integer"
        },
        "FirstName" : {
            "required" : true,
            "type" : "string"
        },
        "LastName" : {
            "required" : true,
            "type" : "string"
        },
        "Email" : {
            "required" : true,
            "type" : "string"
        },
        "Phone" : {
            "required" : true,
            "type" : [
                "string",
                "null"
            ]
        }
    }
}

推荐答案

詹姆斯·牛顿·金(James Newton-King)就在他的答案 ,我将通过一个代码示例对其进行扩展,这样一来,踏入此页面的人们就无需研究整个文档.

James Newton-King is right in his answer, I'll just expand it with a code example so people stumbling onto this page don't need to study the whole documentation.

因此,您可以使用.NET附带的属性来指定那些附加选项,例如字符串的最大长度或允许的正则表达式模式.以下是一些示例:

So you can use the attributes provided with .NET to specify those addidional options, like maximum length of the string or allowed regex pattern. Here are some examples:

public class MyDataModel
{
    public enum SampleEnum { EnumPosition1, EnumPosition2, EnumPosition3 }

    [JsonProperty(Required = Required.Always)]
    [RegularExpression(@"^[0-9]+$")]
    public string PatternTest { get; set; }

    [JsonProperty(Required = Required.Always)]
    [MaxLength(3)]
    public string MaxLength3 { get; set; }

    [JsonProperty(Required = Required.AllowNull)]
    [EnumDataType(typeof(SampleEnum))]
    public string EnumProperty { get; set; }
}

上面的注释来自System.ComponentModel.DataAnnotations命名空间.

The annotations above come from System.ComponentModel.DataAnnotations namespace.

要使这些附加属性影响生成的json模式,您需要使用随 Json.NET Schema 包分发的JSchemaGenerator类.如果您使用的是旧版JsonSchemaGenerator,则需要进行一些升级,因为它已被弃用,并且不包含上述新功能.

To make those additional attributes affect resulting json schema, you need to use JSchemaGenerator class distributed with Json.NET Schema package. If you use older JsonSchemaGenerator, then some upgrade is needed, as it's now deprecated and does not contain new functions like the aforementioned.

这是一个为上面的类生成Json Schema的示例函数:

Here's a sample function that generates Json Schema for the class above:

    /// <summary>
    /// Generates JSON schema for a given C# class using Newtonsoft.Json.Schema library.
    /// </summary>
    /// <param name="myType">class type</param>
    /// <returns>a string containing JSON schema for a given class type</returns>
    internal static string GenerateSchemaForClass(Type myType)
    {
        JSchemaGenerator jsonSchemaGenerator = new JSchemaGenerator();
        JSchema schema = jsonSchemaGenerator.Generate(myType);
        schema.Title = myType.Name;

        return schema.ToString();
    }

,您可以像这样使用它:

and you can use it just like this:

 string schema = GenerateSchemaForClass(typeof(MyDataModel));

这篇关于使用JSON.NET生成具有额外属性的JSON模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆