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

Issue #35 - printToString doesn't encode to bytes #36

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

tonicsoft
Copy link

For any AbstractCharBasedFormatter, printToString can print directly to
a String and not have to convert to bytes and back again. This avoids
call to ByteArrayOutputStream.toString() at ProtobufFormatter:91 which
uses JVM default encoding instead of ProtobufFormatter.defaultCharset.
(I discovered this issue when trying to create JSON with a broad range
of unicode characters on a machine where the default Charset did not
cover the full Unicode range.)

@scr
Copy link
Collaborator

scr commented Feb 23, 2017

Please update https://github.com/bivas/protobuf-java-format/blob/master/RELEASE-NOTES.md for this fix in 1.5.

@scr
Copy link
Collaborator

scr commented Feb 23, 2017

Would you mind fixing the issue you pointed out in Issue #35 ?

https://github.com/bivas/protobuf-java-format/blob/master/src/main/java/com/googlecode/protobuf/format/ProtobufFormatter.java#L91

Basically,

Convert this to

return new String(out.getBytes(), defaultCharset);

@tonicsoft
Copy link
Author

Sure - shall I create a new issue/PR etc or just tag onto this one?

@tonicsoft
Copy link
Author

Done - just added change to this issue, found another bug too.

@scr
Copy link
Collaborator

scr commented Feb 28, 2017

So sorry, but recent RELEASE-NOTES change for #37 collide with yours… please resolve the conflicts and upload.

protected JsonGenerator createGenerator(OutputStream output) throws IOException {
JsonGenerator generator = jsonFactory.createJsonGenerator(output, JsonEncoding.UTF8);
return createGenerator(output, Charset.forName(JsonEncoding.UTF8.getJavaName()));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm… If you know you want UTF8, can you just use StandardCharsets.html.UTF_8?
https://docs.oracle.com/javase/8/docs/api/java/nio/charset/StandardCharsets.html#UTF_8

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fair enough - fixed!

For any AbstractCharBasedFormatter, printToString can print directly to
a String and not have to convert to bytes and back again. This avoids
call to ByteArrayOutputStream.toString() at ProtobufFormatter:91 which
uses JVM default encoding instead of ProtobufFormatter.defaultCharset.
(I discovered this issue when trying to create JSON with a broad range
of unicode characters on a machine where the default Charset did not
cover the full Unicode range.
 - use the same char set when converting from characters to bytes and back again.
 - Remove usage of deprecated jsonFactory API in order to respect charset when printing to an OutputStream using the JsonJacksonFormat.
 - Added test class hierarchy to match production code hierarchy so tests do not need to be duplicated for each ProtobufFormatter.
@tonicsoft
Copy link
Author

Conflicts resolved. Should be ready for merging now.

@onesuper
Copy link

onesuper commented Feb 1, 2018

Is this PR still in progress now? Without this PR, We have to invoke print() method and convert a OutputStream to String manually

@luigi-riefolo
Copy link

any update on this?

@tonicsoft
Copy link
Author

I am no longer working on this, apologies.

Please feel free to take it up.

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

Successfully merging this pull request may close these issues.

None yet

4 participants