Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] 支持JSONSchema #239

Closed
mancong opened this issue May 13, 2022 · 5 comments
Closed

[FEATURE] 支持JSONSchema #239

mancong opened this issue May 13, 2022 · 5 comments
Labels
enhancement New feature or request
Milestone

Comments

@mancong
Copy link

mancong commented May 13, 2022

请描述您的需求或者改进建议

现在 json-schema-validator 方案仅可支持 基本的数据 校验,但是不支持 更多 功能。也无法在JAVA 动态使用(需要前置条件:对应的Bean 类存在)。

请描述你建议的实现方案

参考https://json-schema.org/ 的规范,提供JSONSchema的Validate功能

@mancong mancong added the enhancement New feature or request label May 13, 2022
@wenshao wenshao added this to the 2.0.4 milestone May 13, 2022
@wenshao
Copy link
Member

wenshao commented May 13, 2022

收到需求,预计会排到6月份做

@wenshao wenshao changed the title [FEATURE] 有没有针对 JSON Schema 相关的 规划(例如:校验,生成,运算等操作)呢? [FEATURE] 支持JSONSchema May 15, 2022
wenshao added a commit that referenced this issue May 15, 2022
wenshao added a commit that referenced this issue May 15, 2022
wenshao added a commit that referenced this issue May 15, 2022
wenshao added a commit that referenced this issue May 15, 2022
wenshao added a commit that referenced this issue May 15, 2022
wenshao added a commit that referenced this issue May 15, 2022
wenshao added a commit that referenced this issue May 15, 2022
wenshao added a commit that referenced this issue May 15, 2022
wenshao added a commit that referenced this issue May 15, 2022
wenshao added a commit that referenced this issue May 15, 2022
wenshao added a commit that referenced this issue May 15, 2022
wenshao added a commit that referenced this issue May 15, 2022
wenshao added a commit that referenced this issue May 15, 2022
wenshao added a commit that referenced this issue May 15, 2022
wenshao added a commit that referenced this issue May 15, 2022
wenshao added a commit that referenced this issue May 15, 2022
wenshao added a commit that referenced this issue May 16, 2022
@wenshao
Copy link
Member

wenshao commented May 16, 2022

已经支持JSONSchema,你可以用2.0.4-SNAPSHOT版本试用。

使用例子1 直接使用JSONSchema校验

       JSONSchema jsonSchema = JSON.parseObject("{\n" +
                "  \"properties\": {\n" +
                "    \"country\": {\n" +
                "      \"const\": \"United States of America\"\n" +
                "    }\n" +
                "  }\n" +
                "}").to(JSONSchema::of);

        assertTrue(JSON.parseObject("{ \"country\": \"United States of America\" }")
                .isValid(jsonSchema));

        assertFalse(JSON.parseObject("{ \"country\": \"Canada\" }")
                        .isValid(jsonSchema)
        );

使用例子2 配置JSONType

    @Test
    public void test26() {
        JSON.parseObject("{}", Bean26.class);
        JSON.parseObject("{\"id\":123}", Bean26.class);
        JSON.parseObject("{\"name\":\"xxx\"}", Bean26.class);

        assertThrows(JSONSchemaValidException.class,
                () -> JSON.parseObject("{\"id\":\"123\", \"name\":\"xx\"}", Bean26.class)
        );
    }

    @JSONType(schema = "{'maxProperties':1}")
    public static class Bean26 {
        public Integer id;
        public String name;
    }

使用例子3 使用JSONField配置Schema校验

    @Test
    public void test25() {
        JSON.parseObject("{\"value\":{}}", Bean25.class);
        JSON.parseObject("{\"value\":{\"id\":123}}", Bean25.class);
        JSON.parseObject("{\"value\":{\"name\":\"xxx\"}}", Bean25.class);

        assertThrows(JSONSchemaValidException.class,
                () -> JSON.parseObject("{\"value\":{\"id\":\"123\", \"name\":\"xx\"}}", Bean25.class)
        );
    }

    public static class Bean25 {
        @JSONField(schema = "{'maxProperties':1}")
        public Item value;
    }

    public static class Item {
        public Integer id;
        public String name;
    }

@mancong
Copy link
Author

mancong commented May 17, 2022

感谢回复,我尝试一下。

wenshao added a commit that referenced this issue May 17, 2022
wenshao added a commit that referenced this issue May 18, 2022
wenshao added a commit that referenced this issue May 18, 2022
wenshao added a commit that referenced this issue May 19, 2022
wenshao added a commit that referenced this issue May 19, 2022
wenshao added a commit that referenced this issue May 19, 2022
wenshao added a commit that referenced this issue May 19, 2022
wenshao added a commit that referenced this issue May 20, 2022
wenshao added a commit that referenced this issue May 20, 2022
wenshao added a commit that referenced this issue May 20, 2022
wenshao added a commit that referenced this issue May 21, 2022
wenshao added a commit that referenced this issue May 21, 2022
@wenshao
Copy link
Member

wenshao commented May 21, 2022

https://github.com/alibaba/fastjson2/releases/tag/2.0.4
已经支持,请用新版本

@wenshao wenshao closed this as completed May 21, 2022
@wenshao
Copy link
Member

wenshao commented May 21, 2022

https://github.com/alibaba/fastjson2/blob/main/docs/json_schema_cn.md
可以看这里的文档介绍,性能数据非常好,fastjson2性能是networknt的9倍,everit的6倍。

Benchmark                       Mode  Cnt   Score   Error   Units
JSONSchemaBenchmark.everit     thrpt    5   3.182 ± 0.018  ops/ms
JSONSchemaBenchmark.fastjson2  thrpt    5  21.408 ± 0.147  ops/ms
JSONSchemaBenchmark.networknt  thrpt    5   2.337 ± 0.007  ops/ms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants