13

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

Juan Antonio
  • 2,451
  • 3
  • 24
  • 34
user1745356
  • 4,462
  • 7
  • 42
  • 70

3 Answers3

19

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);
    }

}
Community
  • 1
  • 1
user1745356
  • 4,462
  • 7
  • 42
  • 70
  • 5
    You can use the [`ToStringSerializer`](https://fasterxml.github.io/jackson-databind/javadoc/2.2.0/com/fasterxml/jackson/databind/ser/std/ToStringSerializer.html) provided by jackson. – fracz Apr 08 '16 at 07:23
  • 1
    Is their any other setting I need to make apart from writing this two classes. because it is still not working for me. – Pratik Jul 01 '19 at 17:08
  • Should I do any other additional steps? this doesn't work for me. I use it like below: CustomObjectMapper oMapper = new CustomObjectMapper(); return oMapper.convertValue(obj, Map.class); – Jafar Amini Mar 16 '22 at 11:39
3

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());
    }
}
SuperLucky
  • 595
  • 5
  • 15
0

Here is a simple solution for serialization if you don't have a model for the object being stored:

client.getDatabase("db").getCollection("collection").find().onEach { it["_id"] = it["_id"].toString() }

"onEach" is a kotlin function. If you use Java, then change it to a simple foreach.

It's not efficient to iterate over the entire list just for the id. Only use it for small lists where performance is less important than short code.

Bobin
  • 278
  • 5
  • 15