Json Schema高性能.net实现库 LateApexEarlySpeed.Json.Schema - 直接从code生成json schema validator
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 => tempCelsiusJsonSchemaNamingPolicy.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的更多相关文章
- 在SQL 中生成JSON数据
这段时间接手一个数据操作记录的功能,刚拿到手上的时候打算用EF做,后来经过仔细考虑最后还是觉定放弃,最后思考再三决定: 1.以模块为单位分表.列固定(其实可以所有的操作记录都放到同一个表,但是考虑到数 ...
- C#如何生成JSON字符串提交给接口(服务器)
C#如何生成JSON字符串提交给接口(服务器) 第一章:C#如何拿到从http上返回JSON数据? 第二章:C#如何解析JSON数据?(反序列化对象) 第三章:C#如何生成JSON字符串?(序列化 ...
- C#如何生成JSON字符串?(序列化对象)
第一章:C#如何拿到从http上返回JSON数据? 第二章:C#如何解析JSON数据?(反序列化对象) 第三章:C#如何生成JSON字符串?(序列化对象) 第四章:C#如何生成JSON字符串提交给接口 ...
- 生成json格式
html页面 <input type="button" value="重新生成JSON" class="button1" id=&qu ...
- C#.Net 使用 JsonReader/JsonWriter 高性能解析/生成 Json 文档
Swifter.Json 是由本人编写的高性能且多功能的 Json 解析库.下图是 Swifter.Json 与 .Net 平台上的其他 Json 库性能对比: 在 Swifter.Json 近期更新 ...
- 微信小程序:app.json中通过使用扩展库userExtendedLib的方式,引入并使用weui
微信小程序 PK APP: 1.微信有海量⽤⼾,⽽且粘性很⾼,在微信⾥开发产品更容易触达⽤⼾:而推⼴app的成本太⾼. 2.微信小程序也可以跨平台(Android和IOS). 一.project. ...
- 基于async/non-blocking高性能redis组件库BeetleX.Redis
BeetleX.Redis是基于async/non-blocking模式实现的高性能redis组件库,组件支持redis基础指令集,并封装更简便的List,Hashset和Subscribe操作.除了 ...
- Spring MVC生成JSON数据
以下示例演示如何使用Spring Web MVC框架生成JSON数据格式.首先使用Eclipse IDE,并按照以下步骤使用Spring Web Framework开发基于动态表单的Web应用程序: ...
- Hibernate实体生成JSON的问题及解决
1.延迟加载所造成的代理对象无法正常序列化的问题 在实体类上添加注解: @JsonIgnoreProperties({ "hibernateLazyInitializer", &q ...
- ASP.NET用SQL Server中的数据来生成JSON字符串
原文引自: 作者: 缺水的海豚 来源: 博客园 发布时间: 2010-09-21 21:47 阅读: 6136 次 推荐: 0 原文链接 [收藏] 摘要:ExtJs用到的数据内容基本 ...
随机推荐
- js import的几种用法
最近昨天公司小朋友离职,临时接收其负责的vue前端项目.vue好久没做了,很多东西都忘记或以前也没接触,几天开始慢慢写点vue的小知识,算是历程或备忘吧. import在js.ts中用了不知多少次,但 ...
- echarts自定义legend样式
https://blog.csdn.net/changyana/article/details/126281275
- 【win10 系统】idea 修改 Git 密码和账号方法
https://my.oschina.net/anxiaole/blog/1588664
- langchain中的Document类
在Langchain-Chatchat的上传文档接口(upload_docs)中有个自定义的docs字段,用到了Document类.根据发现指的是from langchain.docstore.d ...
- 当ChatGPT遇上了CoT
最近在看CoT(Chain-of-Thought,思维链)方面的论文<Chain-of-Thought Prompting Elicits Reasoning in Large Language ...
- Pgsql之查询一段时间内的所有年月yyyy-mm
前几天干活儿的时候,报表中有这么个需求,需要用pgsql查询两个日期间的所有年月,下面贴代码: 1 with recursive t(n) as ( 2 select date('2020-01-01 ...
- 【C++】类成员冒号初始化以及构造函数内赋值
From:https://blog.csdn.net/zj510/article/details/8135556 通常我们对类成员进行"初始化"有两种方式: 1. 构造函数后面跟冒 ...
- SpringMVC - 加载静态资源
静态资源过滤 spring-config.xml <!-- 3,(1)让Spring MVC不处理静态资源 .(2)加载静态资源,也称为资源过滤 --> <mvc:default-s ...
- [转帖]Jmeter学习笔记(六)——使用badboy录制脚本
https://www.cnblogs.com/pachongshangdexuebi/p/11506274.html 1.下载安装 可以去badboy官网下载地址:http://www.badboy ...
- [转帖]DD硬盘性能相关因素
https://www.jianshu.com/p/a15d7a65c876 本文简单介绍下DD测试硬盘性能时,各个因素的影响 首先列出测试结果 image.png oflag分析--/home ...