Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add code generation option <jacksonAnnotations/> to generate JsonDeserializer and JsonRawValue annotations on POJOs #13333

Open
lukaseder opened this issue Mar 24, 2022 · 3 comments

Comments

@lukaseder
Copy link
Member

Generated POJOs could have the following annotations generated on their JSON and JSONB properties:

public record POJO (
    int id,
    @JsonRawValue
    @JsonDeserialize(using = JSONDeserializer.class)
    JSON json
) {
}

Where the JSONDeserializer class could be generated into the catalog:

class JSONDeserializer extends JsonDeserializer<JSON> {

    @Override
    public JSON deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        Object t = p.readValueAsTree();
        return t == null ? null : JSON.json("" + t);
    }
}

This would allow for serialising / deserialising JSON and JSONB values using Jackson:

ObjectMapper mapper = new ObjectMapper();

System.out.println(mapper.writeValueAsString(new P(1, JSON.json("[1,2]"))));
System.out.println(mapper.readValue("""
    {"id":1,"json":[1,2]}
    """, P.class));
@jetaggart
Copy link

Is there a recommended way to work around this today?

@lukaseder
Copy link
Member Author

@jetaggart Well, you can register RecordMapperProvider and ConverterProvider SPI implementations to override default ObjectMapper behaviour.

@sseth-github
Copy link

sseth-github commented Aug 30, 2023

Ran into similar issue,

Tech Stack and other details:

  • Spring boot
  • jOOQ
  • REST API returning response for field with type Map<String, Optional<Object>> where Object comes from a jOOQ generated class and could be JSON/JSONB on certain occasions. Spring boot /Jackson wasn't able to serialize it to JSON response and returned the following mentioned error.
  • Also, @JsonRawValue annotation could not be added in a jOOQ generated class.
  • Error: No serializer found for class org.jooq.JSON

Solution/WA:

Configured jackson to use custom serializer, details in this blog: https://medium.com/@sahilseth/custom-serialisers-with-jackson-jooq-and-spring-boot-da8fb9494813

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

No branches or pull requests

3 participants