LateApexEarlySpeed.Json.Schema - Json schema validator generation from code

除了用户手动传入标准的json schema来生成validator以外,LateApexEarlySpeed.Json.Schema 实现库也支持直接从用户代码中生成json schema validator.

基本用法

JsonValidator validator = JsonSchemaGenerator.GenerateJsonValidator<TestClass>();

// Now use validator instance as normal

目前为止支持的.net类型

Numeric types: byte, sbyte, short, ushort, int, uint, long, ulong, float, double, decimal.

Boolean, String, Dictionary<string,TAny>, JsonElement, JsonDocument, JsonNode, JsonValue, JsonArray, JsonObject, IEnumerable, Enum, Guid, Uri, DateTimeOffset, DateTime, Nullable value type (Nullable), Custom object.

目前为止支持的validation attributes

LateApexEarlySpeed.Json.Schema实现库支持通过attributes来表示需要的数据限制。查看各attributes的构造函数的参数应该就可以了解其作用:

  • EmailAttribute
  • ExclusiveMaximumAttribute
  • ExclusiveMinimumAttribute
  • MaximumAttribute
  • MinimumAttribute
  • MultipleOfAttribute
  • StringEnumAttribute
  • IntegerEnumAttribute
  • IPv4Attribute
  • IPv6Attribute
  • LengthRangeAttribute (for both string length and array length)
  • MaxLengthAttribute (for both string length and array length)
  • MinLengthAttribute (for both string length and array length)
  • UniqueItemsAttribute (for array)
  • NumberRangeAttribute
  • PatternAttribute (for string)

attribute用法:

class TestClass
{
[Maximum(2)]
public int Prop { get; set; } [LengthRange(10, 20)]
[Pattern("*abc*")]
public string StringProp { get; set; }
}

Nullable

默认情况下,library会认为所有reference类型为可空。如果你希望指定某引用类型的属性不能为空,可以给这个属性加[LateApexEarlySpeed.Json.Schema.Generator.NotNullAttribute]

Required or ignored

默认情况下,当json数据中出现要验证的类属性时,library才会验证这个json中的属性的值。

如果你希望验证某属性必须出现在json数据中,可以给这个属性加[System.Text.Json.Serialization.JsonRequiredAttribute][System.ComponentModel.DataAnnotations.RequiredAttribute]

如果你希望library忽略对于某个属性的验证,可以给这个属性加[System.Text.Json.Serialization.JsonIgnoreAttribute]

library为这种需求设计为复用.net core默认自带的attribute,是为了尽可能让用户代码有一致体验。

自定义属性名

和System.Text.Json类似, library支持通过attribute和option来提供用户定义的属性名称:

System.Text.Json.Serialization.JsonPropertyNameAttribute:

class CustomNamedPropertyTestClass
{
[JsonPropertyName("NewPropName")]
public int Prop { get; set; }
}

JsonSchemaNamingPolicy options:

  • JsonSchemaNamingPolicy.CamelCase:

    First word starts with a lower case character. Successive words start with an uppercase character. TempCelsius => tempCelsius

  • JsonSchemaNamingPolicy.KebabCaseLower: Words are separated by hyphens. All characters are lowercase. TempCelsius -> temp-celsius

  • JsonSchemaNamingPolicy.KebabCaseUpper: Words are separated by hyphens. All characters are uppercase. TempCelsius => TEMP-CELSIUS

  • JsonSchemaNamingPolicy.SnakeCaseLower: Words are separated by underscores. All characters are lowercase. TempCelsius -> temp_celsius

  • JsonSchemaNamingPolicy.SnakeCaseUpper: Words are separated by underscores. All characters are uppercase. TempCelsius -> TEMP_CELSIUS

  • JsonSchemaNamingPolicy.SharedDefault: default option, not change original property name

  • 也可以写自己需要的其他JsonSchemaNamingPolicy:

internal class YourNamingPolicy : JsonSchemaNamingPolicy
{
public override string ConvertName(string name)
{
// convert and return new name.
}
}

用option来自定义property naming policy:

JsonValidator validator = JsonSchemaGenerator.GenerateJsonValidator(type, new JsonSchemaGeneratorOptions { PropertyNamingPolicy = JsonSchemaNamingPolicy.CamelCase }));

Note: 当对某类属性指定了 JsonPropertyNameAttribute,同时又在option中指定了自定义 PropertyNamingPolicy, 则那个属性将使用JsonPropertyNameAttribute,其他属性将使用option.

Issue report

使用中遇到任何问题,或者希望增加的功能,欢迎提到doc repo issue中。

Json Schema高性能.net实现库 LateApexEarlySpeed.Json.Schema - 直接从code生成json schema validator的更多相关文章

  1. 在SQL 中生成JSON数据

    这段时间接手一个数据操作记录的功能,刚拿到手上的时候打算用EF做,后来经过仔细考虑最后还是觉定放弃,最后思考再三决定: 1.以模块为单位分表.列固定(其实可以所有的操作记录都放到同一个表,但是考虑到数 ...

  2. C#如何生成JSON字符串提交给接口(服务器)

    C#如何生成JSON字符串提交给接口(服务器)   第一章:C#如何拿到从http上返回JSON数据? 第二章:C#如何解析JSON数据?(反序列化对象) 第三章:C#如何生成JSON字符串?(序列化 ...

  3. C#如何生成JSON字符串?(序列化对象)

    第一章:C#如何拿到从http上返回JSON数据? 第二章:C#如何解析JSON数据?(反序列化对象) 第三章:C#如何生成JSON字符串?(序列化对象) 第四章:C#如何生成JSON字符串提交给接口 ...

  4. 生成json格式

    html页面 <input type="button" value="重新生成JSON" class="button1" id=&qu ...

  5. C#.Net 使用 JsonReader/JsonWriter 高性能解析/生成 Json 文档

    Swifter.Json 是由本人编写的高性能且多功能的 Json 解析库.下图是 Swifter.Json 与 .Net 平台上的其他 Json 库性能对比: 在 Swifter.Json 近期更新 ...

  6. 微信小程序:app.json中通过使用扩展库userExtendedLib的方式,引入并使用weui

    微信小程序  PK  APP: 1.微信有海量⽤⼾,⽽且粘性很⾼,在微信⾥开发产品更容易触达⽤⼾:而推⼴app的成本太⾼. 2.微信小程序也可以跨平台(Android和IOS). 一.project. ...

  7. 基于async/non-blocking高性能redis组件库BeetleX.Redis

    BeetleX.Redis是基于async/non-blocking模式实现的高性能redis组件库,组件支持redis基础指令集,并封装更简便的List,Hashset和Subscribe操作.除了 ...

  8. Spring MVC生成JSON数据

    以下示例演示如何使用Spring Web MVC框架生成JSON数据格式.首先使用Eclipse IDE,并按照以下步骤使用Spring Web Framework开发基于动态表单的Web应用程序: ...

  9. Hibernate实体生成JSON的问题及解决

    1.延迟加载所造成的代理对象无法正常序列化的问题 在实体类上添加注解: @JsonIgnoreProperties({ "hibernateLazyInitializer", &q ...

  10. ASP.NET用SQL Server中的数据来生成JSON字符串

    原文引自:  作者: 缺水的海豚  来源: 博客园  发布时间: 2010-09-21 21:47  阅读: 6136 次  推荐: 0   原文链接   [收藏] 摘要:ExtJs用到的数据内容基本 ...

随机推荐

  1. 题解 [HDU6746] Civilization(贪心+模拟)

    来源:2020 年百度之星·程序设计大赛 - 初赛一 一道贪心 + 细节模拟题 题意很简单,这里不详细写了 观察题目,\(n\) 只有 500 ,可以 \(n \times n\) 枚举每个位置作为起 ...

  2. vivo 悟空活动中台 - H5 活动加载优化

    本文首发于 vivo互联网技术 微信公众号 链接: https://mp.weixin.qq.com/s/6gtVR0nVNcZvREjwftZgzA作者:悟空中台研发团队 [悟空活动中台]系列往期精 ...

  3. 倾斜摄影技术构建图扑 WebGIS 智慧展馆

    前言 智慧展馆通过"云大物移智链"等技术将"物"(展品.设备.环境等)进行互联,并感知"人"(工作人员.观众等)的行为.结合 GIS.BIM ...

  4. uni-app 预览pdf文件

    安卓uni-app实现pdf文件预览功能: 1.https://mozilla.github.io/pdf.js/getting_started/#download下载 放在根目录下, 2.新建一个w ...

  5. uni-app点赞效果

  6. freeswitch隐藏fs标识

    概述 freeswitch是一款好用的开源软交换平台. fs的默认配置环境下,sip消息中有很多freeswitch的标识,比如"user-agent","contact ...

  7. C#利用折线图分析产品销售走势

    图形界面 数据 查询效果 代码 private void button1_Click(object sender, EventArgs e) { G++; DrowFont(this.comboBox ...

  8. C#语言 十大经典排序算法动画与解析!(动态演示+代码)(java改写成C# )

    以下内容是根据 https://www.cnblogs.com/fivestudy/p/10212306.html   进行改写 排序算法是<数据结构与算法>中最基本的算法之一. 排序算法 ...

  9. linux tcpdump 使用小结(一)

    转载请注明出处: 很多情况在服务联调和定位过程中,需要在服务器上进行抓包进行问题定位.在Linux环境上使用tcpdump命令进行抓包: 1.tcpdump -s 0 -l -i any -w - p ...

  10. GraduationProject

    GraduationProject 为了毕设寻找的一些springboot项目资源 后台项目: FEBS-Shiro: https://github.com/wuyouzhuguli/FEBS-Shi ...