简体   繁体   English

如何使 fasterXml ObjectMapper 将格式属性放入 long 类型字段的模式中

[英]How can I make fasterXml ObjectMapper put a format property into schema for fields of the type long

How can I make an ObjectMapper set the format field for some properties in the generated schema of a class?如何让ObjectMapper为 class 的生成架构中的某些属性设置format字段?

I'm trying to generate a schema for a java class using a FasterXml library:我正在尝试使用FasterXml库为 java class 生成架构:

<dependency>
  <groupId>com.fasterxml.jackson.module</groupId>
  <artifactId>jackson-module-jsonSchema</artifactId>
  <version>2.14.1</version>
</dependency>
<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>2.14.1</version>
  <scope>compile</scope>
</dependency>

Test:测试:

@SneakyThrows
@Test
public void test() {
    ObjectMapper objectMapper = new ObjectMapper();
    SchemaFactoryWrapper schemaVisitor = new SchemaFactoryWrapper();
    objectMapper.acceptJsonFormatVisitor(objectMapper.constructType(MyClass.class), schemaVisitor);
    System.out.println(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(schemaVisitor.finalSchema()));
}

@Data
public static class MyClass {
    private int myInt;
    private long myLong;
}

The result is like this:结果是这样的:

{
   "type":"object",
   "id":"urn:jsonschema:........:MyClass",
   "properties":{
      "myInt":{
         "type":"integer"
      },
      "myLong":{
         "type":"integer"
      }
   }
}

Both types are good, but I'd like to have "format": "int64" set for the property myLong .两种类型都很好,但我想为属性myLong设置"format": "int64"

Json Schema does not introduce int64 type. Json Schema没有引入int64类型。 There are only number and integer types.只有numberinteger类型。 Take a look at the enum JsonFormatTypes .查看枚举JsonFormatTypes JsonSchema module uses it. JsonSchema模块使用它。 To extend it you would have to extend a lot of classes and somehow trick internal mechanism to replace integer with int64 which at the end would generate misleading schema which would be understandable only for you.要扩展它,您将不得不扩展很多类,并以某种方式欺骗内部机制以将integer替换为int64 ,这最终会产生误导性的模式,只有您才能理解。 All other libs and tools do not recognise it.所有其他库和工具都无法识别它。 I propose to use com.fasterxml.jackson.annotation.JsonPropertyDescription annotation for long property:我建议对long属性使用com.fasterxml.jackson.annotation.JsonPropertyDescription注释:

class MyClass {

    private int myInt;

    @JsonPropertyDescription("int64")
    private long myLong;
}

Which will generate something like this:这将产生这样的东西:

{
  "type" : "object",
  "id" : "urn:jsonschema:com:example:MyClass",
  "properties" : {
    "myInt" : {
      "type" : "integer"
    },
    "myLong" : {
      "type" : "integer",
      "description" : "int64"
    }
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使 fastxml ObjectMapper 与 codehaus 注释一起使用 - How to make fasterxml ObjectMapper work with codehaus annotations 使用com.fasterxml.jackson.databind.ObjectMapper序列化接口字段 - serialize interface fields using com.fasterxml.jackson.databind.ObjectMapper 如何删除ObjectMapper插入到我的JSON输出中的属性字段? - How to remove property fields that ObjectMapper inserts into my JSON Output? 如何将Jackson ObjectMapper配置为使用自定义值序列化某些bean的某些字段(如果它们为null)? - How can I configure a Jackson ObjectMapper to serialize certain fields of certain beans with a custom value if they are null? Jackson ObjectMapper:如何从序列化中省略(忽略)某些类型的字段? - Jackson ObjectMapper: How to omit (ignore) fields of certain type from serialization? Spring Data Rest:如何使json架构属性成为必需? - Spring Data Rest: How can I make a json schema property required? 如何将日期类型参数作为字符串放入 GET 请求中? 格式“yyyy-MM-dd” - How can I put Date type parameter as a String in GET request? Format "yyyy-MM-dd" 如何格式化这个Json来放置额外的空格并使它看起来可读? - How can I format this Json to put extra spaces and make it look readable? 为什么 ObjectMapper 将 Date 类型更改为 Long - Why ObjectMapper changes Date type into Long fastxml jackson objectmapper转换字符串 - fasterxml jackson objectmapper convert string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM