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 JsonMapper to replace generic ObjectMapper usage #2153

Closed
cowtowncoder opened this issue Oct 9, 2018 · 11 comments
Closed

Add JsonMapper to replace generic ObjectMapper usage #2153

cowtowncoder opened this issue Oct 9, 2018 · 11 comments

Comments

@cowtowncoder
Copy link
Member

Due to problems with trying to override builder factory for ObjectMapper, and introduction of JSON-specific format features (JsonReadFeature, JsonWriteFeature), it is necessary to introduce separate JsonMapper, to be used instead of ObjectMapper.
With this (and related Smile, CBOR additions), there will now be format-specific mapper subtype for all formats. ObjectMapper will still exist, and may even be instantiated (more so with 2.10, less with 3.x), but usage should move over to specific mappers for 3.x.

sschuberth added a commit to oss-review-toolkit/ort that referenced this issue Oct 2, 2019
This is more speaking than using the generic ObjectMapper here, also see
FasterXML/jackson-databind#2153.

Signed-off-by: Sebastian Schuberth <[email protected]>
sschuberth added a commit to oss-review-toolkit/ort that referenced this issue Oct 2, 2019
This is more speaking than using the generic ObjectMapper here, also see
FasterXML/jackson-databind#2153.

Signed-off-by: Sebastian Schuberth <[email protected]>
sschuberth added a commit to oss-review-toolkit/ort that referenced this issue Oct 7, 2019
This is more speaking than using the generic ObjectMapper here, also see
FasterXML/jackson-databind#2153.

Signed-off-by: Sebastian Schuberth <[email protected]>
@IEnoobong
Copy link

hi @cowtowncoder , i'm wondering if there's a straight forward way to copy an already configured ObjectMapper to JsonMapper?

Use case is I have an autoconfigured ObjectMappper - Spring, and I'll like to use a JsonMapper with the same settings but add a few new ones

@JooHyukKim
Copy link
Member

Try looking for some sort of copy method in ObjectMapper @IEnoobong

@IEnoobong
Copy link

@JooHyukKim thanks, there's one but it doesn't return a JsonMapper

@cowtowncoder
Copy link
Member Author

cowtowncoder commented Jun 14, 2024

Just cast it: return type of copy() in ObjectMapper has to be plain ObjectMapper, but if you call it on JsonMapper, it'll be copied as JsonMapper.

But also note that as things are, there is no additional functionality in JsonMapper over ObjectMapper (but there are other format-specific subtypes that obviously behave differently).

Also: if your reference is to JsonMapper, then calling copy() on it is declared to return JsonMapper.

If question is on creating JsonMapper from some other type, like CBORMapper or so; no, "format-bending" functionality does not exist, and -- although I thought about it -- won't. Seems difficult or impossible to reliable do that, considering all the things that can vary (esp. wrt modules registered).

@IEnoobong
Copy link

Thanks @cowtowncoder

I did this

  public JsonMapper httpObjectMapper() {
    var copy = (JsonMapper) objectMapper.copy();
    return copy.rebuild()
        .propertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE)
        .enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS)
        .enable(EnumFeature.WRITE_ENUMS_TO_LOWERCASE)
        .build();
  }

But getting this error

Caused by: java.lang.ClassCastException: class com.fasterxml.jackson.databind.ObjectMapper cannot be cast to class com.fasterxml.jackson.databind.json.JsonMapper (com.fasterxml.jackson.databind.ObjectMapper and com.fasterxml.jackson.databind.json.JsonMapper are in unnamed module of loader 'app')

@cowtowncoder
Copy link
Member Author

cowtowncoder commented Jun 14, 2024

Ok, please file an issue with full reproduction and I can have a look.
And make sure to include Jackson version you are using!!!

I suspect it is the rebuild() method.

Note, too, that you need NOT use copy() with rebuild() as it already makes a copy: so you'd do:

   return originalJsonMapper.rebuild()
         // make changes
        .build();

@cowtowncoder
Copy link
Member Author

Cannot reproduce the issue with 2.17, fwtw.

@IEnoobong
Copy link

IEnoobong commented Jun 19, 2024

@cowtowncoder thanks for your response, the issue rather is JsonMapper is a subclass of ObjectMapper not a superclass hence ObjectMapper cannot be cast to JsonMapper which is what I was trying to do.

@JooHyukKim
Copy link
Member

I see what your problem is now. I don't see direct way of copying.
Maybe we could add new JsonMapper(ObjectMapper) for such use case? 🤔
WDYT @cowtowncoder ?

@cowtowncoder
Copy link
Member Author

cowtowncoder commented Jun 19, 2024

@cowtowncoder thanks for your response, the issue rather is JsonMapper is a super class of ObjectMapper not a subclass hence ObjectMapper cannot be cast to JsonMapper which is what I was trying to do.

No. JsonMapper is a sub-class, ObjectMapper super class.
But correct, you cannot cast ObjectMapper that is not JsonMapper into JsonMapper.

Next time please include some code so we can actually evaluate what you are trying to do -- that makes misunderstandings less likely.

@cowtowncoder
Copy link
Member Author

@JooHyukKim No; I thought about that in the past, but things get rather complicated quickly in 2.x, wrt configuration. It would be very difficult to ensure everything is properly copied.

We could consider this for 3.0 where things are bit cleaner as ObjectMapper subtypes are immutable.

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

No branches or pull requests

3 participants