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

SerializableString value can not directly render to Writer #480

Closed
marschall opened this issue Sep 20, 2018 · 4 comments
Closed

SerializableString value can not directly render to Writer #480

marschall opened this issue Sep 20, 2018 · 4 comments
Milestone

Comments

@marschall
Copy link
Contributor

We have a JsonGenerator on a Writer. We would like to write Java 8 Date Time values to JSON. We would like to do so by avoiding intermediate objects and garbage like the String object but instead directly render to the Writer. At first we thought we could achieve this as DateTimeFormatter allows appending to a Appendable and Writer implement Appendable. We thought that by implementing SerializableString we could achieve this. Unfortunately this does not work as WriterBasedJsonGenerator#writeString(SerializableString) always creates a char[] intermediate object. This is in contrast to UTF8JsonGenerator#writeString(SerializableString) which allows directly rendering to the output buffer without creating intermediate objects/garbage.

Ideally the SerializableString would be changed but in your case the intended outcome would be achieved if WriterBasedJsonGenerator#writeString would call #appendQuoted(char[], int) instead.

This is with Jackson 2.9.6.

@cowtowncoder
Copy link
Member

I am not sure I understand: there is no significant difference between operation modes for SerializerString byte[] and char[] options. In both cases intermediate representation is created once and reused. However there is no benefit of doing this unless same String is written more than once, with default implementation.

I guess this could be different for custom implementations that just wrap array(s)?

I think I could change writerBasedJsonGenerator() to use appendQuoted() for 2.10 -- that was the plan originally but somehow did not get done.
Does that make sense?

@marschall
Copy link
Contributor Author

Yeah, I did not explain very well.

  • UTF8JsonGenerator ends up calling#appendQuotedUTF8 which gives the the implementation direct access to the output byte[]. The implementation can write directly on that without the need to create an intermediate byte[]/char[] (there is still an additional copy but no intermediate garbage).
  • WriterBasedJsonGenerator ends up calling #asQuotedChars () which forces the implementation to create an intermediate byte[]/char[].

I think I could change writerBasedJsonGenerator() to use appendQuoted() for 2.10

Yes that would help and address our issue.

@marschall
Copy link
Contributor Author

I guess this could be different for custom implementations that just wrap array(s)?

We plan to implement an Appendable that just wraps the array passed to appendQuoted(). DateTimeFormatter then gets passed this Appendable and produces a string representation of a Date Time object directly into the char[] with the Appendable being the only intermediate object.

@cowtowncoder cowtowncoder added this to the 2.10.0 milestone Sep 25, 2018
@cowtowncoder cowtowncoder changed the title SerializableString can not directly render to Writer SerializableString value can not directly render to Writer Sep 25, 2018
@marschall
Copy link
Contributor Author

Awesome, thank you!

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

2 participants