diff --git a/src/main/java/com/googlecode/protobuf/format/XmlFormat.java b/src/main/java/com/googlecode/protobuf/format/XmlFormat.java index 336b92a..bdc441b 100644 --- a/src/main/java/com/googlecode/protobuf/format/XmlFormat.java +++ b/src/main/java/com/googlecode/protobuf/format/XmlFormat.java @@ -31,6 +31,9 @@ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT import java.io.IOException; import java.math.BigInteger; +import java.text.DecimalFormat; +import java.text.Format; +import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.Map; @@ -38,11 +41,7 @@ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT import java.util.regex.Pattern; import java.nio.CharBuffer; -import com.google.protobuf.ByteString; -import com.google.protobuf.Descriptors; -import com.google.protobuf.ExtensionRegistry; -import com.google.protobuf.Message; -import com.google.protobuf.UnknownFieldSet; +import com.google.protobuf.*; import com.google.protobuf.Descriptors.EnumValueDescriptor; import com.google.protobuf.Descriptors.FieldDescriptor; import static com.googlecode.protobuf.format.util.TextUtils.*; @@ -62,6 +61,7 @@ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT */ public final class XmlFormat extends AbstractCharBasedFormatter { + public Map formatter = new HashMap(); /** * Outputs a textual representation of the Protocol Message supplied into the parameter output. * (This representation is the new version of the classic "ProtocolPrinter" output from the @@ -164,7 +164,10 @@ private void printFieldValue(FieldDescriptor field, Object value, XmlGenerator g case DOUBLE: case BOOL: // Good old toString() does what we want for these types. - generator.print(value.toString()); + if(formatter.containsKey(field.getType())) + generator.print(formatter.get(field.getType()).format(value)); + else + generator.print(value.toString()); break; case UINT32: diff --git a/src/test/java/com/googlecode/protobuf/format/XmlFormatTest.java b/src/test/java/com/googlecode/protobuf/format/XmlFormatTest.java new file mode 100644 index 0000000..a13a126 --- /dev/null +++ b/src/test/java/com/googlecode/protobuf/format/XmlFormatTest.java @@ -0,0 +1,37 @@ +package com.googlecode.protobuf.format; + +import com.google.protobuf.Descriptors; +import com.googlecode.protobuf.format.issue23.Issue23; +import org.testng.annotations.Test; +import org.testng.reporters.Files; + +import java.io.IOException; +import java.text.DecimalFormat; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +/** + * Created by kyakkala on 3/22/2018. + */ +public class XmlFormatTest { + + @Test + public void testDecimalFormat() throws IOException { + + Issue23.MsgWithUnknownFields msg = Issue23.MsgWithUnknownFields.newBuilder() + .setLeaf4(12345670678.01245) + .build(); + XmlFormat xmlFormat = new XmlFormat(); + + DecimalFormat decimalFormat = new DecimalFormat("#"); + decimalFormat.setMaximumFractionDigits(4); + xmlFormat.formatter.put(Descriptors.FieldDescriptor.Type.DOUBLE, decimalFormat); + + assertThat(xmlFormat.printToString(msg).toString(), + is(Files.readFile(XmlFormatTest.class + .getResourceAsStream( + "/expectations/xmlFormatTest/xml_format_without_exponent_notation.txt")).trim())); + + } +} diff --git a/src/test/resources/expectations/xmlFormatTest/xml_format_without_exponent_notation.txt b/src/test/resources/expectations/xmlFormatTest/xml_format_without_exponent_notation.txt new file mode 100644 index 0000000..ad83508 --- /dev/null +++ b/src/test/resources/expectations/xmlFormatTest/xml_format_without_exponent_notation.txt @@ -0,0 +1 @@ +12345670678.0124 \ No newline at end of file diff --git a/src/test/resources/proto/issue23.proto b/src/test/resources/proto/issue23.proto index 422bb59..f9d5428 100644 --- a/src/test/resources/proto/issue23.proto +++ b/src/test/resources/proto/issue23.proto @@ -8,4 +8,5 @@ message MsgWithUnknownFields { optional string leaf1 = 1; optional int32 leaf2 = 2; repeated int32 leaf3 = 3; + optional double leaf4 = 4; }