在没有任何POJO的情况下,使用Java代码+ GSON生成JSON SCHEMA Manally [英] Generate JSON SCHEMA Manally using java code+GSON without any POJO
问题描述
我想使用GSON手动创建JSON Schema,但是我在GSON中找不到任何JsonSchema元素支持.我不想将pojo转换为架构,但希望以编程方式创建架构. GSON有什么办法吗?可能像下面这样.
I want to create JSON Schema manually using GSON but i dont find any JsonSchema element support in GSON. I dont want to convert a pojo to schema but want to create schema programatically . Is there any way in GSON ? May be something like following.
**1 JsonSchema schema = new JsonSchema();
2 schema.Type = JsonSchemaType.Object;
3 schema.Properties = new Dictionary<string, JsonSchema>
4{
5 { "name", new JsonSchema { Type = JsonSchemaType.String } },
6 {
7 "hobbies", new JsonSchema
8 {
9 Type = JsonSchemaType.Array,
10 Items = new List<JsonSchema> { new JsonSchema { Type = JsonSchemaType.String } }
11 }
12 },
13};**
推荐答案
您可以考虑使用 everit -org/json-schema 用于以编程方式创建JSON模式.尽管没有正确记录,但其构建器类形成了使您能够使用的流畅API.示例:
You may consider using everit-org/json-schema for programmatically creating JSON Schemas. Although it is not properly documented, its builder classes form a fluent API which lets you do it. Example:
Schema schema = ObjectSchema.builder()
.addPropertySchema("name", StringSchema.builder().build())
.addPropertySchema("hobbies", ArraySchema.builder()
.allItemSchema(StringSchema.builder().build())
.build())
.build();
它的语法与您描述的语法略有不同,但是对于相同的目的可能是有益的.
It is slightly different syntax than what you described, but it can be good for the same purpose.
(免责声明:我是 everit-org/json-schema )
这篇关于在没有任何POJO的情况下,使用Java代码+ GSON生成JSON SCHEMA Manally的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!