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

4.x: Use string constructor of BigDecimal to avoid bad decimals in output. #9074

Merged
merged 1 commit into from
Aug 1, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,15 @@ interface Builder extends io.helidon.common.Builder<Builder, Object> {
*/
Builder set(String key, double value);

/**
* Set a float value.
*
* @param key key to set
* @param value value to assign to the key
* @return updated instance (this instance)
*/
Builder set(String key, float value);

/**
* Set an int value.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,16 @@ static Hson.Array create(int... values) {

static Hson.Array create(double... values) {
List<BigDecimal> collect = DoubleStream.of(values)
.mapToObj(BigDecimal::new)
.mapToObj(String::valueOf)
.map(BigDecimal::new)
.collect(Collectors.toUnmodifiableList());
return Hson.Array.createNumbers(collect);
}

static Hson.Array create(float... values) {
List<BigDecimal> list = new ArrayList<>(values.length);
for (float value : values) {
list.add(new BigDecimal(value));
list.add(new BigDecimal(String.valueOf(value)));
}

return Hson.Array.createNumbers(list);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,12 +259,18 @@ public Builder set(String key, boolean value) {
return this;
}

@Override
public Builder set(String key, float value) {
Objects.requireNonNull(key, "key cannot be null");

return set(key, new BigDecimal(String.valueOf(value)));
}

@Override
public Builder set(String key, double value) {
Objects.requireNonNull(key, "key cannot be null");

values.put(key, HsonValues.NumberValue.create(new BigDecimal(value)));
return this;
return set(key, new BigDecimal(String.valueOf(value)));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ void testWriteObjectArray() {
String string = sw.toString();
String expected = "[{\"string\":\"value\","
+ "\"long\":4,"
+ "\"double\":4,"
+ "\"double\":4.0,"
+ "\"boolean\":true,"
+ "\"strings\":[\"a\",\"b\"],"
+ "\"longs\":[1,2],"
Expand All @@ -233,6 +233,7 @@ void testWriteObjectArray() {
assertThat(readSecond, is(second));
}

@Test
void testSetObjects() {
Hson.Object first = Hson.objectBuilder()
.set("key", "value1")
Expand Down Expand Up @@ -268,6 +269,22 @@ void testUnset() {
assertThat(object.value("null-key"), optionalEmpty());
}

@Test
void testFunnyDoubles() {
StringWriter stringWriter = new StringWriter();
try (PrintWriter pw = new PrintWriter(stringWriter)) {
Hson.objectBuilder()
.set("first", 1.1d)
.set("second", 1.2f)
.set("third", 1.3)
.set("fourth", 4)
.set("fifth", 5L)
.build()
.write(pw);
}
assertThat(stringWriter.toString(), is("{\"first\":1.1,\"second\":1.2,\"third\":1.3,\"fourth\":4,\"fifth\":5}"));
}

@Test
void testFeatures() {
Hson.Value<?> parsedValue = Hson.parse(HsonTest.class.getResourceAsStream("/json-features.json"));
Expand Down