Skip to content

Use @JacksonAnnotationsInside to reduce Mongojack configuration #213

@msmerc

Description

@msmerc

Hi,

Would you consider using clever jackson annotations to reduce the amount of object mapper magic you need to get mongojack to work. Here's what I'm thinking:

First define an annotation like so:

@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotationsInside
@JsonSerialize(using = ObjectIdSerializer.class)
@JsonDeserialize(using = ObjectIdDeserializers.ToStringDeserializer.class)
@JsonProperty("_id")
@JsonInclude(JsonInclude.Include.NON_NULL)
public @interface SmartId {}

Then use it like so:

      public static class Example {
        @SmartId
        public String id;
        public String text;
    }

    @Test
    public void simpleTest() {
        CodecRegistry provider = new JacksonCodecRegistry(
                new ObjectMapper(), //Look no modules or anything!
                db.getCodecRegistry(),
                UuidRepresentation.UNSPECIFIED
        );

        var coll = db.withCodecRegistry(provider).getCollection("test", Example.class);
        Example ex = new Example();
        ex.text = "fiddlesticks";
        var io = coll.insertOne(ex);
        Example ex2 = coll.find().first();
    }

The neat thing about this is that I've only used an out-of-the-box CodecRegistry, and an out-of-the-box ObjectMapper. This is particularly valuable for projects that have complex ObjectMapper setups.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions