7

好的,首先这是从我的 Web 服务返回的 JSON。在我的 Android ContentProvider 中的 ResponseHandler 中进行异步查询后,我试图将其反序列化为 pojos。

{"exampleList" : [{
"locationId" : "00001" , 
"owners" : [ 
  { 
    "paidID" : { "$oid" : "50a9c951300493f64fbffdb6"} , 
    "userID" : { "$oid" : "50a9c951300493f64fbffdb6"}
  } , 
  { 
    "paidID" : { "$oid" : "50a9c951300493f64fbffdb7"} , 
    "userID" : { "$oid" : "50a9c951300493f64fbffdb7"}
  } 
]
}]}

起初,我对所看到的问题感到困惑,因为我在我的 Web 服务中使用了与我在 Android 应用程序中相同的 Jackson 注释 bean——但后来我意识到该owners对象从未在示例 JSON 中发送到我的 Web 服务(它跳过我的 Web 服务上的 POJO,并通过来自 DAO 的原子更新添加到 mongoDB 中的文档中)。

那么好吧。到目前为止,Jackson 不必处理该owners对象,现在它被它窒息了,即:

JsonMappingException:无法反序列化[char position where you can find and ] 通过引用链 [包含所有者类的我的 Jackson bean 的路径]处java.lang.String的 令牌实例START_OBJECT"userID""paidID"

我的 Jackson bean 有一个包装器,这就是“exampleList”的全部内容:

public class Examples extends HashMap<String, ArrayList<Example>> {

}

然后是实际的Example课程:

@JsonIgnoreProperties(ignoreUnknown = true)
public class Example implements Comparable<Example> {

@ObjectId @Id
private String id;

@JsonProperty(Constants.Example.location)
private String location;

@JsonProperty(Constants.Example.OWNERS)
private List<Owners> owners;

public int compareTo(Example _o) {
    return getId().compareTo(_o.getId());
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getLocation() {
    return location;
}

public void setLocation(String location) {
    this.location = location;
}
public List<Example.Owners> getOwners() {
    return owners;
}

public void setOwners(List<Example.Owners> owners) {
    this.owners = owners;
}

public Example() {
}

@JsonCreator
public Example(@Id @ObjectId String id) {
    this.id = id;
}

@JsonIgnoreProperties(ignoreUnknown = true)
public static class Owners implements Comparable<Owners> {

    @JsonProperty(Constants.Example.USERID)
    private String userID;

    @JsonProperty(Constants.Example.PAIDID)
    private String paidID;

    public Owners() {
    }

    public int compareTo(Owners _o) {
        return getUserID().compareTo(_o.getUserID());
    }

    @ObjectId
    public String getUserID() {
        return userID;
    }

    @ObjectId
    public void setUserID(String userID) {
        this.userID = userID;
    }

    @ObjectId
    public String getPaidID() {
        return paidID;
    }

    @ObjectId
    public void setPaidID(String paidID) {
        this.paidID = paidID;
    }

}

}

最后,ResponseHandler 中的代码全部失败(第二行产生 JsonMappingException):

objectMapper = MongoJacksonMapperModule.configure(objectMapper);    
mExamples = objectMapper.readValue(jsonParser, Examples.class);

我有一种感觉,问题是杰克逊仍然不知道如何映射那些$oidmongoDB ObjectIds。MongoJacksonMapper 库应该通过提供@ObjectId注释和配置 ObjectMapper 以使用该库的方法来帮助实现这一点,但它仍然无法正常工作。出于某种原因,它仍在寻找作为字符串的 userID 或paidID,而不是 ObjectId。有任何想法吗?

4

6 回答 6

20

另一种选择是 com.fasterxml.jackson.databind.ser.std.ToStringSerializer.

@Id
@JsonSerialize(using = ToStringSerializer.class)
private final ObjectId id;

这将导致:

{
  "id": "5489f420c8306b6ac8d33897"
}
于 2014-12-11T19:59:49.600 回答
4

对于未来的用户:使用自定义 jackson 反序列化器将 $oid 转换回 ObjectId。

public class ObjectIdDeserializer extends JsonDeserializer<ObjectId> {

    @Override
    public ObjectId deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        JsonNode oid = ((JsonNode)p.readValueAsTree()).get("$oid");
        return new ObjectId(oid.asText());
    }

}

如何使用:

ObjectMapper mapper = new ObjectMapper();
SimpleModule mod = new SimpleModule("ObjectId", new Version(1, 0, 0, null, null, null));
        
mod.addDeserializer(ObjectId.class, new ObjectIdDeserializer());
mapper.registerModule(mod);

YourClass obj = mapper.readValue("{your json with $oid}", YourClass.class);
于 2017-05-17T09:38:59.590 回答
2

我的代码至少有两个问题很难在网上找到答案,所以我会确保在此处链接。基本上,子类需要在父类中调用 Jackson 的 readValue() 来映射子类的构造函数。就 mongoDB $oid 而言,您应该创建一个单独的 MongoId 类来表示这些 mongo 对象,并遵循与子类类似的模式来映射数据以进行反序列化。这是我发现的一篇博客文章,很好地描述了这一点并提供了一些示例。

于 2012-12-30T21:47:21.060 回答
2

Jackson 不知道如何序列化 ObjectId。我调整了 Arny 的代码以序列化任何 ObjectId 并提供这个工作示例:

public class SerialiserTest {

 private ObjectMapper mapper = new ObjectMapper();

 public static class T {
    private ObjectId objectId;

    public ObjectId getObjectId() {
        return objectId;
    }

    public void setObjectId(ObjectId objectId) {
        this.objectId = objectId;
    }
 }

 @Test
 public final void serDeser() throws IOException {
    T t = new T();
    t.setObjectId(new ObjectId());
    List<T> ls = Collections.singletonList(t);
    String json = mapper.writeValueAsString(ls);
    System.out.println(json);
    SimpleModule mod = new SimpleModule("ObjectId", new Version(1, 0, 0, null, null, null));
    mod.addDeserializer(ObjectId.class, new ObjectIdDeserializer());
    mapper.registerModule(mod);
    JavaType type = mapper.getTypeFactory().
            constructCollectionType(List.class, T.class);
    List<?> l  = mapper.readValue(json, type);
    System.out.println(l);
 }
}

public class ObjectIdDeserializer extends JsonDeserializer<ObjectId> {

 @Override
 public ObjectId deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    JsonNode n = (JsonNode)p.readValueAsTree();
    return new ObjectId(n.get("timestamp").asInt(), n.get("machineIdentifier").asInt(), (short) n.get("processIdentifier").asInt(), n.get("counter").asInt());
 }

}
于 2017-07-07T16:06:10.877 回答
1

这里记录了一种更简单的方法,这对我来说是救命稻草。现在您可以在 Java 中使用 ObjectId,但是当您访问/来自 JSON 时,它将是一个字符串。

public class ObjectIdJsonSerializer extends JsonSerializer<ObjectId> {
    @Override
    public void serialize(ObjectId o, JsonGenerator j, SerializerProvider s) throws IOException, JsonProcessingException {
        if(o == null) {
            j.writeNull();
        } else {
            j.writeString(o.toString());
        }
    }
}

然后在你的豆子里:

@JsonSerialize(using=ObjectIdJsonSerializer.class)
private ObjectId id;
于 2014-05-19T02:22:46.823 回答
1

我是这样做的:

@Configuration
public class SpringWebFluxConfig {

    @Bean
    @Primary
    ObjectMapper objectMapper() {
      Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
      builder.serializerByType(ObjectId.class, new ToStringSerializer());
      builder.deserializerByType(ObjectId.class, new JsonDeserializer() {
        @Override
        public Object deserialize(JsonParser p, DeserializationContext ctxt) 
            throws IOException {
          Map oid = p.readValueAs(Map.class);
          return new ObjectId(
              (Integer) oid.get("timestamp"),
              (Integer) oid.get("machineIdentifier"),
              ((Integer) oid.get("processIdentifier")).shortValue(),
              (Integer) oid.get("counter"));
        }
      });
      return builder.build();
    }
}
于 2018-12-23T18:08:59.760 回答