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
Unnecessary JsonProcessingException Catch clause in ObjectMapper.writeValueAsString(Object value) #1694
Comments
The intent is to avoid unnecessary wrapping of |
The comments by @hperperidis are pointing out that he cannot see any code path that produces a |
Creating a
|
@cowtowncoder - Please reopen this. By rethrowing the exception (instead of wrapping it with another exception), is that I can't see why i'm getting "Current context not Object but root". Yes i'm probably doing something wrong to get this error, but with the Ideally, just |
@ruckc the rethrowing behavior will not replace the stack trace. you will still have the old stack trace available. |
@ruckc If you want a change, please file a new issue, explaining exact issue you have. It does not sound the original issue and I think it is confusing to reopen issues after the fact in such cases. But I would specifically need a reproduction of your problem, not just general description. |
In
ObjectMapper
class, methodwriteValueAsString(Object value)
includes athrow
declaration of typeJsonProcessingException
.Current Method code (minus comments) - Latest release
The
_configAndWriteValue()
call in the snippet above (surrounded by thetry - catch
block, is only throwing anIOException
, which is caught and handled in the seccond catch clause.So the only other part of this
try - catch
block that could potentially throw theJsonProcessingException
is the_jsonFactory.createGenerator(sw)
call, wheresw
is theSegmentedStringWriter
which extendsWriter
.Looking at how the JsonFactory instance is populated in this class, we can see in the Object mapper constructor:
which calls:
This constructor initializes the
JsonFactory
by default, as an instance of itsMappingJsonFactory
implementing subclass.Looking at this class, we can see it does not actually overide the
createGenerator(Writer w)
method fromcom.fasterxml.jackson.core.JsonFactory
.And from
com.fasterxml.jackson.core.JsonFactory
we can see that all overloaded instances of thecreateGenerator
method are not throwing aJsonProcessingException
but rather again anIOException
(See link https://github.com/FasterXML/jackson-core/blob/master/src/main/java/com/fasterxml/jackson/core/JsonFactory.java).So the catch clause for
JsonProcessingException
can be completely removed fromwriteValueAsString
inObjectMapper
.Recomended Change
Unless I have missed something, I think the above refactor could be made without causing serious issues.
The text was updated successfully, but these errors were encountered: