Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson Mapper Serialize/Deserialize ObjectId

Tags:

json

jackson

My POJO is :

import org.jongo.marshall.jackson.id.Id;

public class User {    

    @Id
    private String id;
    private String name;
    private int age;

    public String getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

I get user from mongo database and want to output him into a file with jackson mapper

ObjectMapper mapper = new ObjectMapper();
mapper.writerWithDefaultPrettyPrinter().writeValue(new File("c:/user.txt"), user);

and I get something like this in my file

{
    "name" : "John",
    "age" : 23,
    "_id" : {
      "time" : 1358443593000,
      "inc" : 660831772,
      "machine" : 2028353122,
      "new" : false,
      "timeSecond" : 1358443593
    }
}

I need id field to be stored into a file as string because when i deserialize this object my id field in pojo looks something like this

{
   "time":1358443593000,
   "inc":660831772,
   "machine":2028353122,
   "new":false,
   "timeSecond":1358443593
}

Any help will be apreciated

like image 256
user1745356 Avatar asked Jan 17 '13 22:01

user1745356


People also ask

How does ObjectMapper readValue work?

The simple readValue API of the ObjectMapper is a good entry point. We can use it to parse or deserialize JSON content into a Java object. Also, on the writing side, we can use the writeValue API to serialize any Java object as JSON output.

Is ObjectMapper thread safe?

Mapper instances are fully thread-safe provided that ALL configuration of the instance occurs before ANY read or write calls.

Which methods does Jackson rely upon to deserialize a JSON formatted string?

The @JsonSetter annotation tells Jackson to deserialize the JSON into Java object using the name given in the setter method. Use this annotation when your JSON property names are different to the fields of the Java object class, and you want to map them.


2 Answers

Answering my own question. Found solution here Spring 3.2 and Jackson 2: add custom object mapper

I needed custom object mapper and ObjectId serializer.

public class ObjectIdSerializer extends JsonSerializer<ObjectId> {

    @Override
    public void serialize(ObjectId value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
        jgen.writeString(value.toString());
    }
}


public class CustomObjectMapper extends ObjectMapper {

    public CustomObjectMapper() {
        SimpleModule module = new SimpleModule("ObjectIdmodule");
        module.addSerializer(ObjectId.class, new ObjectIdSerializer());
        this.registerModule(module);
    }

}
like image 73
user1745356 Avatar answered Oct 21 '22 04:10

user1745356


I found an easy attempt using springboot 2.5.4.

Just by adding a Jackson2ObjectMapperBuilderCustomizer bean will do the trick.

@Configuration
public class JacksonMapperConfiguration
{   
    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
        return builder -> builder.serializerByType(ObjectId.class, new ToStringSerializer());
    }
}
like image 2
SuperLucky Avatar answered Oct 21 '22 05:10

SuperLucky