Skip to content

double exponent notation to number #48

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/main/java/com/googlecode/protobuf/format/XmlFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,17 @@ 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;
import java.util.regex.Matcher;
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.*;
Expand All @@ -62,6 +61,7 @@ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
*/
public final class XmlFormat extends AbstractCharBasedFormatter {

public Map<FieldDescriptor.Type, Format> formatter = new HashMap<FieldDescriptor.Type, Format>();
/**
* 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
Expand Down Expand Up @@ -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:
Expand Down
37 changes: 37 additions & 0 deletions src/test/java/com/googlecode/protobuf/format/XmlFormatTest.java
Original file line number Diff line number Diff line change
@@ -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()));

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<MsgWithUnknownFields><leaf4>12345670678.0124</leaf4></MsgWithUnknownFields>
1 change: 1 addition & 0 deletions src/test/resources/proto/issue23.proto
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ message MsgWithUnknownFields {
optional string leaf1 = 1;
optional int32 leaf2 = 2;
repeated int32 leaf3 = 3;
optional double leaf4 = 4;
}