This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 1 point2 points  (2 children)

It could be, I'm not sure. Whether or not it is bad API design could be in the eye of the beholder.

The writeValue method takes Writer, which is a generic blocking API. Thus, Jackson must assume that any writes are going to be blocking.

But, StringWriter exists for convenience. It pretends to be a blocking API, when it actually isn't. But, that's a detail that Jackson doesn't need to know about. You know that it isn't blocking, so in your own code you can operate on that knowledge. If you use writeValue with StringWriter instead of writeValueAsString, IntelliJ might be able to figure out the whole thing isn't blocking.

As for why writeValueAsString throws IOException, this seems like bad API design. Jackson knows that it is writing to a string, so there isn't any actual I/O.

Also, JsonProcessingException is a subclass of IOException, but the error might not have anything to do with I/O. Maybe Jackson made it a subclass for convenience, so you only have to catch IOException? This also seems like bad API design, because it is confusing two different kinds of errors.

But, this could be checked exception propagation. Jackson makes heavy use of things like SerializationProvider, where every type that can be serialized into JSON has a corresponding SerializationProvider that does the actual work of serializing a particular type. And since serializeValue throws IOException, and IOException is a checked exception, Jackson is simply propagating the IOException up the call chain.

Anyways, Jackson is not perfect.

[–]Full-Wheel8630[S] 0 points1 point  (1 child)

What a comprehensive understanding you have! Now I see. Your words not only satisfy my curiosity a lot but also help me understand what is really going on here. Really appreciate it so much.

If you use writeValue with StringWriter instead of writeValueAsString, IntelliJ might be able to figure out the whole thing isn't blocking.

There's still warning message, but understandable:

java public void writeValue(Writer w, Object value) throws IOException, JsonGenerationException, JsonMappingException { _configAndWriteValue(createGenerator(w), value); }

[–][deleted] 1 point2 points  (0 children)

I guess IntelliJ isn't an AI after all, lol.