Skip to content

Commit 9383fa9

Browse files
authored
Use string constructor of BigDecimal to avoid bad decimals in output. (helidon-io#9074)
Added method to set float. Updated test
1 parent a00e732 commit 9383fa9

4 files changed

Lines changed: 38 additions & 5 deletions

File tree

metadata/hson/src/main/java/io/helidon/metadata/hson/Hson.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,15 @@ interface Builder extends io.helidon.common.Builder<Builder, Object> {
339339
*/
340340
Builder set(String key, double value);
341341

342+
/**
343+
* Set a float value.
344+
*
345+
* @param key key to set
346+
* @param value value to assign to the key
347+
* @return updated instance (this instance)
348+
*/
349+
Builder set(String key, float value);
350+
342351
/**
343352
* Set an int value.
344353
*

metadata/hson/src/main/java/io/helidon/metadata/hson/HsonArray.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,16 @@ static Hson.Array create(int... values) {
7777

7878
static Hson.Array create(double... values) {
7979
List<BigDecimal> collect = DoubleStream.of(values)
80-
.mapToObj(BigDecimal::new)
80+
.mapToObj(String::valueOf)
81+
.map(BigDecimal::new)
8182
.collect(Collectors.toUnmodifiableList());
8283
return Hson.Array.createNumbers(collect);
8384
}
8485

8586
static Hson.Array create(float... values) {
8687
List<BigDecimal> list = new ArrayList<>(values.length);
8788
for (float value : values) {
88-
list.add(new BigDecimal(value));
89+
list.add(new BigDecimal(String.valueOf(value)));
8990
}
9091

9192
return Hson.Array.createNumbers(list);

metadata/hson/src/main/java/io/helidon/metadata/hson/HsonObject.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,12 +259,18 @@ public Builder set(String key, boolean value) {
259259
return this;
260260
}
261261

262+
@Override
263+
public Builder set(String key, float value) {
264+
Objects.requireNonNull(key, "key cannot be null");
265+
266+
return set(key, new BigDecimal(String.valueOf(value)));
267+
}
268+
262269
@Override
263270
public Builder set(String key, double value) {
264271
Objects.requireNonNull(key, "key cannot be null");
265272

266-
values.put(key, HsonValues.NumberValue.create(new BigDecimal(value)));
267-
return this;
273+
return set(key, new BigDecimal(String.valueOf(value)));
268274
}
269275

270276
@Override

metadata/hson/src/test/java/io/helidon/metadata/hson/HsonTest.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ void testWriteObjectArray() {
212212
String string = sw.toString();
213213
String expected = "[{\"string\":\"value\","
214214
+ "\"long\":4,"
215-
+ "\"double\":4,"
215+
+ "\"double\":4.0,"
216216
+ "\"boolean\":true,"
217217
+ "\"strings\":[\"a\",\"b\"],"
218218
+ "\"longs\":[1,2],"
@@ -233,6 +233,7 @@ void testWriteObjectArray() {
233233
assertThat(readSecond, is(second));
234234
}
235235

236+
@Test
236237
void testSetObjects() {
237238
Hson.Object first = Hson.objectBuilder()
238239
.set("key", "value1")
@@ -268,6 +269,22 @@ void testUnset() {
268269
assertThat(object.value("null-key"), optionalEmpty());
269270
}
270271

272+
@Test
273+
void testFunnyDoubles() {
274+
StringWriter stringWriter = new StringWriter();
275+
try (PrintWriter pw = new PrintWriter(stringWriter)) {
276+
Hson.objectBuilder()
277+
.set("first", 1.1d)
278+
.set("second", 1.2f)
279+
.set("third", 1.3)
280+
.set("fourth", 4)
281+
.set("fifth", 5L)
282+
.build()
283+
.write(pw);
284+
}
285+
assertThat(stringWriter.toString(), is("{\"first\":1.1,\"second\":1.2,\"third\":1.3,\"fourth\":4,\"fifth\":5}"));
286+
}
287+
271288
@Test
272289
void testFeatures() {
273290
Hson.Value<?> parsedValue = Hson.parse(HsonTest.class.getResourceAsStream("/json-features.json"));

0 commit comments

Comments
 (0)