bivas / protobuf-java-format

Provide serialization and de-serialization of different formats based on Google’s protobuf Message. Enables overriding the default (byte array) output to text based formats such as XML, JSON and HTML.
BSD 3-Clause "New" or "Revised" License
152 stars 97 forks source link

JsonFormat.printToString() does not respect JsonFormat.setDefaultCharset() #35

Open tonicsoft opened 8 years ago

tonicsoft commented 8 years ago

Implementation of JsonFormat.printToString() (which is in ProtobufFormatter.java) is to print to a byte array using the Charset specified by JsonFormat.setDefaultCharset(), but converts the byte array back to a string using ByteArrayOutputStream.toString(), which uses Charset.defaultCharset().

Conversion from the ByteArrayOutputStream back to a string, https://github.com/bivas/protobuf-java-format/blob/master/src/main/java/com/googlecode/protobuf/format/ProtobufFormatter.java#L91, should also use the specified Charset instead of JVM default.

It is worth pointing out that conversion to bytes and back to string is not actually required if all we want to do is serialize JSON to a java.lang.String.