【问题标题】:How to remove backslash for quotes from JsonSchema?如何从 JsonSchema 中删除引号的反斜杠?
【发布时间】:2021-07-02 22:43:22
【问题描述】:

我有一堂课:

class Setting {

    String configurationName;
    String configuration; 
} 

我想返回字符串表示配置的样子。根据某些条件,这可能是不同的对象。

在下面的一项服务中:

@GET
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response getConfigurationSetting ()
{
    try {

        Setting settingPojo = new Setting();
        settingPojo.setCOnfigurationName("DataBase");

        ObjectMapper mapper = new ObjectMapper();

        SchemaFactoryWrapper visitor = new SchemaFactoryWrapper();

        mapper.acceptJsonFormatVisitor(OracleConfiguration.class, visitor);

        JsonSchema schema = visitor.finalSchema();

        settingPojo.setConfiguraton(mapper.writeValueAsString(schema));

        return Response.status(HTTP.CodeOK).entity(settingPojo).build();
    } catch (Exception ex) {
        // exception logic
    }
}

可以有不同的类,像这样:“MySQLConfiguration.class”。

示例:

{
    configurationName : "DataBase",
    configuration: "{
       \"type\":\"object\",
       \"id\":\"urn\":\"jsonschema\":\"database\":\"model\":\"OracleConfiguration\",
       \"properties\":{
          \"numberOfConnection\":{
             \"type\":\"integer\"
          },
          \"connectionDate\":{
             \"type\":\"integer\",
             \"format":\"utc-millisec\"
          },
          \"isconnected\":{
             \"type\":\"boolean\"
          }
       }
    }"
}
  

上述输出的问题:

  1. 我想从字符串中删除 id 属性。
  2. 我得到了一个奇怪的额外反斜杠作为转义字符。在调试和执行这一行时我没有看到这一点:mapper.writeValueAsString(schema)。但是我在设置属性后看到了反斜杠和额外的引号。

知道如何解决这些问题吗?

【问题讨论】:

  • 停止对 JSON 进行双重编码。
  • @chrylis-cautiouslyoptimistic- 感谢您的回复.. 没有得到你.. 你是什么意思停止双重编码 json?我想向用户展示该类将是什么数据类型
  • JsonSchema 对象被编码两次。第一次由您直接编码:mapper.writeValueAsString(schema)。第二次由 Spring 编码。将类型从 String 更改为 Object - Object configuration; 并在控制器中设置它而无需额外的序列化:settingPojo.setConfiguraton(schema);

标签: java json jackson jsonschema json-serialization


【解决方案1】:
  1. 您可以使用MixIn 功能指示Jackson 删除id。看看example
  2. 对象被序列化了两次,一次由您在控制器方法中直接序列化,第二次 - 由Spring

在您的示例中,您需要:
Setting类中使用@JsonRawValue注解:

class Setting {
    String configurationName;

    @JsonRawValue
    String configuration;
}

创建一个MixInabstract class

abstract class JsonSchemaWithoutId {

    @JsonIgnore
    public String id;

    @JsonIgnore
    public abstract String getId();
}

简单用法:

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonRawValue;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.module.jsonSchema.JsonSchema;
import com.fasterxml.jackson.module.jsonSchema.factories.SchemaFactoryWrapper;
import lombok.Data;

import java.io.File;
import java.io.IOException;

public class JsonMixInAndSchemaApp {

    public static void main(String[] args) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.enable(SerializationFeature.INDENT_OUTPUT);
        mapper.addMixIn(JsonSchema.class, JsonSchemaWithoutId.class);

        Setting settingPojo = new Setting();
        settingPojo.setConfigurationName("Setting");

        SchemaFactoryWrapper visitor = new SchemaFactoryWrapper();
        mapper.acceptJsonFormatVisitor(Setting.class, visitor);
        JsonSchema schema = visitor.finalSchema();

        settingPojo.setConfiguration(mapper.writeValueAsString(schema));

        mapper.writeValue(System.out, settingPojo);
    }
}

上面的代码打印

{
  "configurationName" : "Setting",
  "configuration" : {
  "type" : "object",
  "properties" : {
    "configurationName" : {
      "type" : "string"
    },
    "configuration" : {
      "type" : "string"
    }
  }
}
}

【讨论】:

    猜你喜欢
    • 2018-08-23
    • 2016-03-24
    • 1970-01-01
    • 1970-01-01
    • 2021-05-13
    • 1970-01-01
    • 1970-01-01
    • 2019-12-23
    • 1970-01-01
    相关资源
    最近更新 更多