-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support writing longs as strings (opt-in)
`jackson-datatype-protobuf` is a missing link in the Jackson ecosystem, but it doesn't currently provide a canonical representation of long integers, which poses a problem for strict parsers given the limitation of the maximum value to [2^53 - 1](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER) in JavaScript, whereas this value is for instance [2^63 - 1](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html) in Java. The change proposed here is to provide users who need it with an option to activate this canonical representation, without altering the existing default behavior.
- Loading branch information
1 parent
ea45ad7
commit b797d1f
Showing
13 changed files
with
131 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/test/java/com/hubspot/jackson/datatype/protobuf/WriteLongsAsStringsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.hubspot.jackson.datatype.protobuf; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.io.IOException; | ||
|
||
import org.junit.Test; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.hubspot.jackson.datatype.protobuf.util.TestProtobuf.AllFields; | ||
|
||
public class WriteLongsAsStringsTest { | ||
|
||
@Test | ||
public void itDoesntWriteLongsAsStringsByDefault() throws IOException { | ||
ProtobufJacksonConfig config = ProtobufJacksonConfig.builder().build(); | ||
|
||
assertThat(writeSomeInt64s(config)).isEqualTo("{\"int64\":-42,\"uint64\":42}"); | ||
} | ||
|
||
@Test | ||
public void itDoesntWriteLongsAsStringsWhenDisabled() throws IOException { | ||
ProtobufJacksonConfig config = ProtobufJacksonConfig.builder().writeLongsAsStrings(false).build(); | ||
|
||
assertThat(writeSomeInt64s(config)).isEqualTo("{\"int64\":-42,\"uint64\":42}"); | ||
} | ||
|
||
@Test | ||
public void itDoesntWriteLongsAsStringsWhenEnabledThenDisabled() throws IOException { | ||
ProtobufJacksonConfig config = ProtobufJacksonConfig.builder() | ||
.writeLongsAsStrings(true) | ||
.writeLongsAsStrings(false) | ||
.build(); | ||
|
||
assertThat(writeSomeInt64s(config)).isEqualTo("{\"int64\":-42,\"uint64\":42}"); | ||
} | ||
|
||
@Test | ||
public void itWritesLongsAsStringsWhenEnabled() throws IOException { | ||
ProtobufJacksonConfig config = ProtobufJacksonConfig.builder().writeLongsAsStrings(true).build(); | ||
|
||
assertThat(writeSomeInt64s(config)).isEqualTo("{\"int64\":\"-42\",\"uint64\":\"42\"}"); | ||
} | ||
|
||
@Test | ||
public void itWritesLongsAsStringsWhenDisabledThenEnabled() throws IOException { | ||
ProtobufJacksonConfig config = ProtobufJacksonConfig.builder() | ||
.writeLongsAsStrings(false) | ||
.writeLongsAsStrings(true) | ||
.build(); | ||
|
||
assertThat(writeSomeInt64s(config)).isEqualTo("{\"int64\":\"-42\",\"uint64\":\"42\"}"); | ||
} | ||
|
||
private static String writeSomeInt64s(final ProtobufJacksonConfig config) throws IOException { | ||
ObjectMapper mapper = new ObjectMapper().registerModules(new ProtobufModule(config)); | ||
AllFields someInt64s = AllFields.newBuilder().setInt64(-42).setUint64(42).build(); | ||
return mapper.writeValueAsString(someInt64s); | ||
} | ||
} |