简体   繁体   English

使用Jackson对象映射器将Map转换为Java对象非常慢

[英]Converting Map to Java object using Jackson Object Mapper is very slow

I am currently trying to create 52 Java objects from a Map using the Jackson databinding library and it currently takes a total of 3.518 seconds to convert all 52 objects, which seems to be very slow. 我目前正在尝试使用Jackson数据绑定库从Map创建52个Java对象,目前总共花费3.518秒来转换所有52个对象,这似乎很慢。 I am not sure why this is the case. 我不知道为什么会这样。 Is this an anomaly or is there something that can be done to make these conversions faster? 这是异常现象还是可以采取措施使这些转换更快?

Here is my Java object class: 这是我的Java对象类:

public class MoodDatapoint extends DocumentModelHelper {

    @JsonProperty(value = "happiness")
    private int happiness;

    @JsonProperty(value = "stress")
    private int stress;

    @JsonProperty(value = "pain")
    private int pain;

    @JsonProperty(value = "timestamp")
    private long timestamp;

    @JsonProperty(value = "lat")
    private double lat;

    @JsonProperty(value = "lng")
    private double lng;

    @JsonProperty(value = "comment")
    private String comment;

    @JsonProperty(value = "event_name")
    private String eventName;

    @JsonProperty(value = "is_before")
    private boolean isBefore;

    @JsonProperty(value = "related_event_id")
    private String relatedEventID;
}

Here is the Map I am trying to convert into the class: 这是我要转换为类的Map:

    {
    stress=0, 

    pain=0, 

    happiness=10, 

    timestamp=1488464269384, 

    is_before=false, 

    lng=-79.6208645, 

    event_name=null, 

    comment=, 

    lat=43.6462939,

    related_event_id=null
}

And my code to convert the Map to an object: 我的代码将Map转换为对象:

ObjectMapper m = new ObjectMapper();

MoodDatapoint datapoint = m.convertValue(map, MoodDatapoint.class);

Using logcat to calculate the duration of each object conversion, it seems like each conversion takes an average of 62 milliseconds: 使用logcat计算每次对象转换的持续时间,似乎每次转换平均需要62毫秒:

...
D/M DATAPOINT CONVERSION DURATION: 68
D/M DATAPOINT CONVERSION DURATION: 45
D/TOTAL DURATION (S): 3550
D/AVG DURATION: 68
D/Total objects: 

Do not create ObjectMapper for every conversion, instead create a single instance and use that for all conversions. 不要为每个转换都创建ObjectMapper,而是创建一个实例并将其用于所有转换。 ObjectMapper creation is a very expensive operation. 创建ObjectMapper是一项非常昂贵的操作。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM