Java Jackson中字符串到int[]的自动转换

Java Jackson中字符串到int[]的自动转换,java,arrays,json,jackson,Java,Arrays,Json,Jackson,我有一个简单的POJO: public class Entry { private Integer day; private String site; private int[] cpms; ... // getters/setters/constructor } 我希望读取的日志文件如下所示: { "day":"1", "site":"google.com", "cpms":"[1,2,3,4,5,6,7]"} Jackson根据文档自动将字符串转换为整数。

我有一个简单的POJO:

public class Entry {
    private Integer day;
    private String site;
    private int[] cpms;
    ... // getters/setters/constructor
}
我希望读取的日志文件如下所示:

{ "day":"1", "site":"google.com", "cpms":"[1,2,3,4,5,6,7]"}
Jackson根据文档自动将字符串转换为整数。但它不是用于现场CPM。最好的阅读方式是什么? 我知道我可以在构造器中将CPM定义为字符串,然后用数组解析这个字符串,如下所示:

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY, true);
this.cpm = objectMapper.readValue(cpms, int[].class);

但是有任何智能转换吗?

此转换也可以使用自定义JsonDeserializer完成

1实现专用的反序列化器类,例如:

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;

import java.io.IOException;

public class IntArrayDeserializer extends StdDeserializer<int[]> {

  public IntArrayDeserializer() {
    super(int[].class);
  }

  @Override
  public int[] deserialize(JsonParser jsonParser,
                           DeserializationContext deserializationContext) throws IOException {
    String arrayAsString = jsonParser.getText();
    if (arrayAsString.length() == 2) { // case of empty array "[]"
      return new int[0];
    }
    String[] strIds = arrayAsString.substring(1, arrayAsString.length() - 1).split(",");
    return Arrays.stream(strIds).mapToInt(Integer::parseInt).toArray();
  }

}

这种方法的优点是,如果有其他字段需要以相同的方式进行转换,则此反序列化器可以重用,它符合Jackson API,并且不需要实施特殊的解决方法。

此转换也可以使用自定义JsonDeserializer完成

1实现专用的反序列化器类,例如:

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;

import java.io.IOException;

public class IntArrayDeserializer extends StdDeserializer<int[]> {

  public IntArrayDeserializer() {
    super(int[].class);
  }

  @Override
  public int[] deserialize(JsonParser jsonParser,
                           DeserializationContext deserializationContext) throws IOException {
    String arrayAsString = jsonParser.getText();
    if (arrayAsString.length() == 2) { // case of empty array "[]"
      return new int[0];
    }
    String[] strIds = arrayAsString.substring(1, arrayAsString.length() - 1).split(",");
    return Arrays.stream(strIds).mapToInt(Integer::parseInt).toArray();
  }

}

这种方法的优点是,如果有其他字段需要以相同的方式转换,该反序列化器是可重用的,它符合Jackson API,并且不需要实施特殊的解决方法。

据我所知,Jackson没有任何内置的解决方案。数组是用引号括起来的,所以它是一个字符串。但是,有一些方法可能适合您:

JSON创建者 定义一个用@JsonCreator注释的构造函数,然后解析字符串:

公开课入学{ 私人整数日; 私人网站; 私有int[]cpms; @JsonCreator 平民的Entry@JsonPropertycpms字符串CPM{ 字符串[]值=cpms.replace[,.replace],.split,; this.cpms=Arrays.streamvalues.mapToIntInteger::parseInt.toArray; } //接球手和接球手 } 自定义反序列化程序 编写自己的反序列化程序,然后可以在其他bean中重用它:

公共类QuotedArraydSerializer扩展JsonDeserializer{ @凌驾 public int[]反序列化JSONParser jp, 反序列化上下文ctxt引发IOException{ 字符串rawValue=jp.getValueAsString; 字符串[]值=rawValue.replace[,.replace],.split,; 返回Arrays.streamvalues.mapToIntInteger::parseInt.toArray; } } 公开课入学{ 私人整数日; 私人网站; @JsonDeserializeusing=QuotedArrayDeserializer.class 私有int[]cpms; //接球手和接球手 }
据我所知,杰克逊没有任何内置的解决方案。数组是用引号括起来的,所以它是一个字符串。但是,有一些方法可能适合您:

JSON创建者 定义一个用@JsonCreator注释的构造函数,然后解析字符串:

公开课入学{ 私人整数日; 私人网站; 私有int[]cpms; @JsonCreator 平民的Entry@JsonPropertycpms字符串CPM{ 字符串[]值=cpms.replace[,.replace],.split,; this.cpms=Arrays.streamvalues.mapToIntInteger::parseInt.toArray; } //接球手和接球手 } 自定义反序列化程序 编写自己的反序列化程序,然后可以在其他bean中重用它:

公共类QuotedArraydSerializer扩展JsonDeserializer{ @凌驾 public int[]反序列化JSONParser jp, 反序列化上下文ctxt引发IOException{ 字符串rawValue=jp.getValueAsString; 字符串[]值=rawValue.replace[,.replace],.split,; 返回Arrays.streamvalues.mapToIntInteger::parseInt.toArray; } } 公开课入学{ 私人整数日; 私人网站; @JsonDeserializeusing=QuotedArrayDeserializer.class 私有int[]cpms; //接球手和接球手 }
您的cpms:[1,2,3,4,5,6,7]字段是否与此完全相同?如果它失去了阵列周围的磁场,它会工作,不会?你的CPM:[1,2,3,4,5,6,7]场与此完全一样吗?如果它失去了阵列周围的数据,它会工作,不是吗?这两种解决方案看起来都很好,比我的。。。谢谢两种解决方案看起来都很好,比我的。。。谢谢