更快的 xml 对象映射器将 java 对象错误地转换为 json?
Faster xml object mapper converting java object to json incorrectly?
您好,我有一个包含对象数组的 json 数据文件。我正在读取 json 文件并将其转换为 java 对象的列表。但是,当我使用对象映射器将其转换为 json 文档时,我看到打印的值的精度后跟零。知道我该如何解决它。
下面是我的 json 数据文件。
[
{
"inventory": "SMS",
"msg_text": "This is random text",
"status": "ENROUTE",
"@timestamp": "2019-09-02T03:26:26.770Z",
"o_error": "",
"flight_id": "92348fa1-ca6c-456a-b3b2-85fba2d2deed",
"recipient": 420736408283,
"account_id": "a56f7e14-20f9-40e6-90c6-10604140ac5f",
"sender": 8800111,
"submission_ts": 1567681407,
"campaign_id": "6f2abca3-b46d-43f3-91be-3278a8dd7dc0",
"nof_segments": 1,
"@version": 1,
"delivery_ts": 1558761537
},
{
"inventory": "SMS",
"msg_text": "This is random text",
"status": "ENROUTE",
"@timestamp": "2019-09-02T09:48:09.213Z",
"o_error": "",
"flight_id": "92348fa1-ca6c-456a-b3b2-85fba2d2deed",
"recipient": 420736408283,
"account_id": "a56f7e14-20f9-40e6-90c6-10604140ac5f",
"sender": 8800111,
"submission_ts": 1567681407,
"campaign_id": "6f2abca3-b46d-43f3-91be-3278a8dd7dc0",
"nof_segments": 1,
"@version": 1,
"delivery_ts": 1549246209
}]
以下是读取文件的代码,将其转换为 json 对象并打印。
ObjectMapper objMapper = new ObjectMapper();
objMapper.registerModule(new JavaTimeModule());
objMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, 假);
List<MessageHistory> messageHistories = objMapper.readValue(getMessageHistoryResourceAsStream(),
objMapper.getTypeFactory().constructCollectionType(List.class, MessageHistory.class));
for (int i = 0; i < messageHistories.size(); i++) {
System.out.println(objMapper.writeValueAsString(messageHistories.get(i)));
}
输出低于
{"inventory":"SMS","msg_text":"This is random text","status":"ENROUTE","@timestamp":"2019-09-04T17:43:03.518Z","o_error":"","flight_id":"92348fa1-ca6c-456a-b3b2-85fba2d2deed","recipient":"420736408281","account_id":"a56f7e14-20f9-40e6-90c6-10604140ac5f","sender":"8800111","campaign_id":"6f2abca3-b46d-43f3-91be-3278a8dd7dc0","nof_segments":1,"@version":1,"submission_ts":1567681407.000000000,"delivery_ts":1562941055.000000000}
{"inventory":"SMS","msg_text":"This is random text","status":"ENROUTE","@timestamp":"2019-09-01T09:05:45.540Z","o_error":"","flight_id":"92348fa1-ca6c-456a-b3b2-85fba2d2deed","recipient":"420736408283","account_id":"a56f7e14-20f9-40e6-90c6-10604140ac5f","sender":"8800111","campaign_id":"6f2abca3-b46d-43f3-91be-3278a8dd7dc0","nof_segments":1,"@version":1,"submission_ts":1567681407.000000000,"delivery_ts":1558368683.000000000}
你可以看到 submission_ts 和 delivery_ts 有 .000000000 ..
下面是我的 MessageHistory.java 文件,我用它来将 json 数据绑定到对象。
import java.time.Instant;
import java.util.UUID;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
public class MessageHistory {
@JsonProperty("inventory")
private String inventory;
@JsonProperty("msg_text")
private String messageText;
@JsonProperty("status")
private String status;
@JsonProperty("@timestamp")
private Instant timeStamp;
@JsonProperty("o_error")
private String error;
@JsonProperty("flight_id")
private UUID flightId;
@JsonProperty("recipient")
private String recipient;
@JsonProperty("account_id")
private UUID accountId;
@JsonProperty("sender")
private String sender;
@JsonProperty("campaign_id")
private UUID campaignId;
@JsonProperty("nof_segments")
private Integer segmentCount;
@JsonProperty("@version")
private Integer version;
@JsonProperty("submission_ts")
@JsonFormat(shape = JsonFormat.Shape.NUMBER_INT, timezone = "UTC")
private Instant submissionTimeStamp;
@JsonProperty("delivery_ts")
@JsonFormat(shape = JsonFormat.Shape.NUMBER_INT, timezone = "UTC")
private Instant deliveryTimeStamp;
public String getInventory() {
return inventory;
}
public void setInventory(String inventory) {
this.inventory = inventory;
}
public String getMessageText() {
return messageText;
}
public void setMessageText(String messageText) {
this.messageText = messageText;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Instant getTimeStamp() {
return timeStamp;
}
public void setTimeStamp(Instant timeStamp) {
this.timeStamp = timeStamp;
}
public String getError() {
return error;
}
public void setError(String error) {
this.error = error;
}
public UUID getFlightId() {
return flightId;
}
public void setFlightId(UUID flightId) {
this.flightId = flightId;
}
public String getRecipient() {
return recipient;
}
public void setRecipient(String recipient) {
this.recipient = recipient;
}
public String getSender() {
return sender;
}
public void setSender(String sender) {
this.sender = sender;
}
public UUID getAccountId() {
return accountId;
}
public void setAccountId(UUID accountId) {
this.accountId = accountId;
}
public UUID getCampaignId() {
return campaignId;
}
public void setCampaignId(UUID campaignId) {
this.campaignId = campaignId;
}
public Integer getSegmentCount() {
return segmentCount;
}
public void setSegmentCount(Integer segmentCount) {
this.segmentCount = segmentCount;
}
public Integer getVersion() {
return version;
}
public void setVersion(Integer version) {
this.version = version;
}
public Instant getSubmissionTimeStamp() {
return submissionTimeStamp;
}
public void setSubmissionTimeStamp(Instant submissionTimeStamp) {
this.submissionTimeStamp = submissionTimeStamp;
}
public Instant getDeliveryTimeStamp() {
return deliveryTimeStamp;
}
public void setDeliveryTimeStamp(Instant deliveryTimeStamp) {
this.deliveryTimeStamp = deliveryTimeStamp;
}
}
我正在注释文件
@JsonProperty("submission_ts")
@JsonFormat(shape = JsonFormat.Shape.NUMBER_INT, timezone = "UTC")
private Instant submissionTimeStamp;
@JsonProperty("delivery_ts")
@JsonFormat(shape = JsonFormat.Shape.NUMBER_INT, timezone = "UTC")
private Instant deliveryTimeStamp;
非常感谢任何帮助解决为什么打印为 json 的对象在这两个字段上附加了 .000000
谢谢
您想禁用 WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS
功能。
来自文档:
Feature that controls whether numeric timestamp values are to be written using nanosecond timestamps (enabled) or not (disabled);
您可以在 ObjectMapper
中禁用它(适用于所有字段):
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
mapper.disable(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS);
或在字段级别禁用它:
@JsonFormat(shape = Shape.NUMBER,
without = Feature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS)
private Instant instant;
如果您打算在多个字段中使用此配置,您可能需要考虑 @JacksonAnnotationsInside
,它允许您创建包含一个或多个 Jackson 注释的自定义注释:
@Retention(value = RUNTIME)
@JacksonAnnotationsInside
@JsonFormat(shape = Shape.NUMBER,
without = Feature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS)
public @interface JsonTimestamp {
}
然后简单地使用新创建的注解:
@JsonTimestamp
private Instant instant;
您好,我有一个包含对象数组的 json 数据文件。我正在读取 json 文件并将其转换为 java 对象的列表。但是,当我使用对象映射器将其转换为 json 文档时,我看到打印的值的精度后跟零。知道我该如何解决它。
下面是我的 json 数据文件。
[
{
"inventory": "SMS",
"msg_text": "This is random text",
"status": "ENROUTE",
"@timestamp": "2019-09-02T03:26:26.770Z",
"o_error": "",
"flight_id": "92348fa1-ca6c-456a-b3b2-85fba2d2deed",
"recipient": 420736408283,
"account_id": "a56f7e14-20f9-40e6-90c6-10604140ac5f",
"sender": 8800111,
"submission_ts": 1567681407,
"campaign_id": "6f2abca3-b46d-43f3-91be-3278a8dd7dc0",
"nof_segments": 1,
"@version": 1,
"delivery_ts": 1558761537
},
{
"inventory": "SMS",
"msg_text": "This is random text",
"status": "ENROUTE",
"@timestamp": "2019-09-02T09:48:09.213Z",
"o_error": "",
"flight_id": "92348fa1-ca6c-456a-b3b2-85fba2d2deed",
"recipient": 420736408283,
"account_id": "a56f7e14-20f9-40e6-90c6-10604140ac5f",
"sender": 8800111,
"submission_ts": 1567681407,
"campaign_id": "6f2abca3-b46d-43f3-91be-3278a8dd7dc0",
"nof_segments": 1,
"@version": 1,
"delivery_ts": 1549246209
}]
以下是读取文件的代码,将其转换为 json 对象并打印。
ObjectMapper objMapper = new ObjectMapper();
objMapper.registerModule(new JavaTimeModule());
objMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, 假);
List<MessageHistory> messageHistories = objMapper.readValue(getMessageHistoryResourceAsStream(),
objMapper.getTypeFactory().constructCollectionType(List.class, MessageHistory.class));
for (int i = 0; i < messageHistories.size(); i++) {
System.out.println(objMapper.writeValueAsString(messageHistories.get(i)));
}
输出低于
{"inventory":"SMS","msg_text":"This is random text","status":"ENROUTE","@timestamp":"2019-09-04T17:43:03.518Z","o_error":"","flight_id":"92348fa1-ca6c-456a-b3b2-85fba2d2deed","recipient":"420736408281","account_id":"a56f7e14-20f9-40e6-90c6-10604140ac5f","sender":"8800111","campaign_id":"6f2abca3-b46d-43f3-91be-3278a8dd7dc0","nof_segments":1,"@version":1,"submission_ts":1567681407.000000000,"delivery_ts":1562941055.000000000}
{"inventory":"SMS","msg_text":"This is random text","status":"ENROUTE","@timestamp":"2019-09-01T09:05:45.540Z","o_error":"","flight_id":"92348fa1-ca6c-456a-b3b2-85fba2d2deed","recipient":"420736408283","account_id":"a56f7e14-20f9-40e6-90c6-10604140ac5f","sender":"8800111","campaign_id":"6f2abca3-b46d-43f3-91be-3278a8dd7dc0","nof_segments":1,"@version":1,"submission_ts":1567681407.000000000,"delivery_ts":1558368683.000000000}
你可以看到 submission_ts 和 delivery_ts 有 .000000000 ..
下面是我的 MessageHistory.java 文件,我用它来将 json 数据绑定到对象。
import java.time.Instant;
import java.util.UUID;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
public class MessageHistory {
@JsonProperty("inventory")
private String inventory;
@JsonProperty("msg_text")
private String messageText;
@JsonProperty("status")
private String status;
@JsonProperty("@timestamp")
private Instant timeStamp;
@JsonProperty("o_error")
private String error;
@JsonProperty("flight_id")
private UUID flightId;
@JsonProperty("recipient")
private String recipient;
@JsonProperty("account_id")
private UUID accountId;
@JsonProperty("sender")
private String sender;
@JsonProperty("campaign_id")
private UUID campaignId;
@JsonProperty("nof_segments")
private Integer segmentCount;
@JsonProperty("@version")
private Integer version;
@JsonProperty("submission_ts")
@JsonFormat(shape = JsonFormat.Shape.NUMBER_INT, timezone = "UTC")
private Instant submissionTimeStamp;
@JsonProperty("delivery_ts")
@JsonFormat(shape = JsonFormat.Shape.NUMBER_INT, timezone = "UTC")
private Instant deliveryTimeStamp;
public String getInventory() {
return inventory;
}
public void setInventory(String inventory) {
this.inventory = inventory;
}
public String getMessageText() {
return messageText;
}
public void setMessageText(String messageText) {
this.messageText = messageText;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Instant getTimeStamp() {
return timeStamp;
}
public void setTimeStamp(Instant timeStamp) {
this.timeStamp = timeStamp;
}
public String getError() {
return error;
}
public void setError(String error) {
this.error = error;
}
public UUID getFlightId() {
return flightId;
}
public void setFlightId(UUID flightId) {
this.flightId = flightId;
}
public String getRecipient() {
return recipient;
}
public void setRecipient(String recipient) {
this.recipient = recipient;
}
public String getSender() {
return sender;
}
public void setSender(String sender) {
this.sender = sender;
}
public UUID getAccountId() {
return accountId;
}
public void setAccountId(UUID accountId) {
this.accountId = accountId;
}
public UUID getCampaignId() {
return campaignId;
}
public void setCampaignId(UUID campaignId) {
this.campaignId = campaignId;
}
public Integer getSegmentCount() {
return segmentCount;
}
public void setSegmentCount(Integer segmentCount) {
this.segmentCount = segmentCount;
}
public Integer getVersion() {
return version;
}
public void setVersion(Integer version) {
this.version = version;
}
public Instant getSubmissionTimeStamp() {
return submissionTimeStamp;
}
public void setSubmissionTimeStamp(Instant submissionTimeStamp) {
this.submissionTimeStamp = submissionTimeStamp;
}
public Instant getDeliveryTimeStamp() {
return deliveryTimeStamp;
}
public void setDeliveryTimeStamp(Instant deliveryTimeStamp) {
this.deliveryTimeStamp = deliveryTimeStamp;
}
}
我正在注释文件
@JsonProperty("submission_ts")
@JsonFormat(shape = JsonFormat.Shape.NUMBER_INT, timezone = "UTC")
private Instant submissionTimeStamp;
@JsonProperty("delivery_ts")
@JsonFormat(shape = JsonFormat.Shape.NUMBER_INT, timezone = "UTC")
private Instant deliveryTimeStamp;
非常感谢任何帮助解决为什么打印为 json 的对象在这两个字段上附加了 .000000 谢谢
您想禁用 WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS
功能。
来自文档:
Feature that controls whether numeric timestamp values are to be written using nanosecond timestamps (enabled) or not (disabled);
您可以在 ObjectMapper
中禁用它(适用于所有字段):
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
mapper.disable(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS);
或在字段级别禁用它:
@JsonFormat(shape = Shape.NUMBER,
without = Feature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS)
private Instant instant;
如果您打算在多个字段中使用此配置,您可能需要考虑 @JacksonAnnotationsInside
,它允许您创建包含一个或多个 Jackson 注释的自定义注释:
@Retention(value = RUNTIME)
@JacksonAnnotationsInside
@JsonFormat(shape = Shape.NUMBER,
without = Feature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS)
public @interface JsonTimestamp {
}
然后简单地使用新创建的注解:
@JsonTimestamp
private Instant instant;