【问题标题】:Jackson JsonSchema - Object as string in schemaJackson JsonSchema - 对象作为模式中的字符串
【发布时间】:2018-11-02 09:28:04
【问题描述】:

我正在使用来自JSR 385 reference implementationComparableQuantity - 这不是一种情况,只是想提一下子类型是通用的,我无法修改它。

情况是我正在生成一个带有Jackson JsonSchemaModule 的Json 模式。 目前,我从该架构收到的内容当然如下:

{
  "weight": {
    "type": "object",
    "id": "Mass>",
    "required": true,
    "properties": {
      "value": {
        "type": "number"
      },
      "unit": {
        "type": "object",
        "id": "Mass>",
        "properties": {
          "name": {
            "type": "string"
          },
          ...
        }
      }
    }
  }
}

Ofc,这并没有给我很多 - 它太有表现力了。 我已经有一个解析器,它可以从字符串(带有验证)创建我的值并将其序列化为字符串(10m 将在 json 中只是“10 m”),并且我希望具有相同的模式,例如模式:

{
  "weight": {
    "type": "string",
    "pattern": "[\\d,.]+\\s*(kg|t)",
    "required": true
  }
}

我知道Jdk8JacksonModule 可以做出与Optional 类似的东西,但是当我看到Jdk8OptionalBeanPropertyWriter 时,我已经通过了——甚至不确定它是否是正确的寻找位置。

另外,我认为可以使用ObjectMapper$acceptJsonFormatVisitor,也许可以使用SchemaFactoryWrapper#expectObjectFormat - 不确定。

我注意到的是,当我将ComparableQuantity序列化模块添加到Schema序列化器(实际上是从模式序列化的类)时,它将返回

{
  "weight": {
    "type": "any",
    "required": true
  }
}

所以也许这是某种方式......

任何例子都会很好:)。

【问题讨论】:

    标签: java jackson jsonschema


    【解决方案1】:

    我已经完成了覆盖方法JsonSerializer#acceptJsonFormatVisitor,我调用visitor.expectStringFormat(type),此外,为了添加一个模式,我在JsonSchemaGenerator 的构造函数中添加了visitor

    此访问者基于com.fasterxml.jackson.module.jsonSchema.customProperties.ValidationSchemaFactoryWrapper,但当然不是expectObjectFormat 覆盖expectStringFormat

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-09
      • 2016-11-26
      • 1970-01-01
      • 2019-04-10
      • 1970-01-01
      • 2019-08-21
      相关资源
      最近更新 更多