Skip to content

Commit 7f2ed19

Browse files
author
Tomáš Kraus
authored
Limit BigDecimal scale value for JsonNumber#bigIntegerValue() and JsonNumber#bigIntegerValueExact() (#78)
Signed-off-by: Tomáš Kraus <[email protected]>
1 parent 10b8d10 commit 7f2ed19

29 files changed

+756
-525
lines changed

impl/src/main/java/org/eclipse/parsson/JsonArrayBuilderImpl.java

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2021 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2023 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -16,8 +16,6 @@
1616

1717
package org.eclipse.parsson;
1818

19-
import org.eclipse.parsson.api.BufferPool;
20-
2119
import jakarta.json.*;
2220
import java.io.StringWriter;
2321
import java.math.BigDecimal;
@@ -35,23 +33,23 @@
3533
* @author Jitendra Kotamraju
3634
* @author Kin-man Chung
3735
*/
38-
3936
class JsonArrayBuilderImpl implements JsonArrayBuilder {
37+
4038
private ArrayList<JsonValue> valueList;
41-
private final BufferPool bufferPool;
39+
private final JsonContext jsonContext;
4240

43-
JsonArrayBuilderImpl(BufferPool bufferPool) {
44-
this.bufferPool = bufferPool;
41+
JsonArrayBuilderImpl(JsonContext jsonContext) {
42+
this.jsonContext = jsonContext;
4543
}
4644

47-
JsonArrayBuilderImpl(JsonArray array, BufferPool bufferPool) {
48-
this.bufferPool = bufferPool;
45+
JsonArrayBuilderImpl(JsonArray array, JsonContext jsonContext) {
46+
this(jsonContext);
4947
valueList = new ArrayList<>();
5048
valueList.addAll(array);
5149
}
5250

53-
JsonArrayBuilderImpl(Collection<?> collection, BufferPool bufferPool) {
54-
this.bufferPool = bufferPool;
51+
JsonArrayBuilderImpl(Collection<?> collection, JsonContext jsonContext) {
52+
this(jsonContext);
5553
valueList = new ArrayList<>();
5654
populate(collection);
5755
}
@@ -73,32 +71,32 @@ public JsonArrayBuilder add(String value) {
7371
@Override
7472
public JsonArrayBuilder add(BigDecimal value) {
7573
validateValue(value);
76-
addValueList(JsonNumberImpl.getJsonNumber(value));
74+
addValueList(JsonNumberImpl.getJsonNumber(value, jsonContext.bigIntegerScaleLimit()));
7775
return this;
7876
}
7977

8078
@Override
8179
public JsonArrayBuilder add(BigInteger value) {
8280
validateValue(value);
83-
addValueList(JsonNumberImpl.getJsonNumber(value));
81+
addValueList(JsonNumberImpl.getJsonNumber(value, jsonContext.bigIntegerScaleLimit()));
8482
return this;
8583
}
8684

8785
@Override
8886
public JsonArrayBuilder add(int value) {
89-
addValueList(JsonNumberImpl.getJsonNumber(value));
87+
addValueList(JsonNumberImpl.getJsonNumber(value, jsonContext.bigIntegerScaleLimit()));
9088
return this;
9189
}
9290

9391
@Override
9492
public JsonArrayBuilder add(long value) {
95-
addValueList(JsonNumberImpl.getJsonNumber(value));
93+
addValueList(JsonNumberImpl.getJsonNumber(value, jsonContext.bigIntegerScaleLimit()));
9694
return this;
9795
}
9896

9997
@Override
10098
public JsonArrayBuilder add(double value) {
101-
addValueList(JsonNumberImpl.getJsonNumber(value));
99+
addValueList(JsonNumberImpl.getJsonNumber(value, jsonContext.bigIntegerScaleLimit()));
102100
return this;
103101
}
104102

@@ -161,32 +159,32 @@ public JsonArrayBuilder add(int index, String value) {
161159
@Override
162160
public JsonArrayBuilder add(int index, BigDecimal value) {
163161
validateValue(value);
164-
addValueList(index, JsonNumberImpl.getJsonNumber(value));
162+
addValueList(index, JsonNumberImpl.getJsonNumber(value, jsonContext.bigIntegerScaleLimit()));
165163
return this;
166164
}
167165

168166
@Override
169167
public JsonArrayBuilder add(int index, BigInteger value) {
170168
validateValue(value);
171-
addValueList(index, JsonNumberImpl.getJsonNumber(value));
169+
addValueList(index, JsonNumberImpl.getJsonNumber(value, jsonContext.bigIntegerScaleLimit()));
172170
return this;
173171
}
174172

175173
@Override
176174
public JsonArrayBuilder add(int index, int value) {
177-
addValueList(index, JsonNumberImpl.getJsonNumber(value));
175+
addValueList(index, JsonNumberImpl.getJsonNumber(value, jsonContext.bigIntegerScaleLimit()));
178176
return this;
179177
}
180178

181179
@Override
182180
public JsonArrayBuilder add(int index, long value) {
183-
addValueList(index, JsonNumberImpl.getJsonNumber(value));
181+
addValueList(index, JsonNumberImpl.getJsonNumber(value, jsonContext.bigIntegerScaleLimit()));
184182
return this;
185183
}
186184

187185
@Override
188186
public JsonArrayBuilder add(int index, double value) {
189-
addValueList(index, JsonNumberImpl.getJsonNumber(value));
187+
addValueList(index, JsonNumberImpl.getJsonNumber(value, jsonContext.bigIntegerScaleLimit()));
190188
return this;
191189
}
192190

@@ -237,32 +235,32 @@ public JsonArrayBuilder set(int index, String value) {
237235
@Override
238236
public JsonArrayBuilder set(int index, BigDecimal value) {
239237
validateValue(value);
240-
setValueList(index, JsonNumberImpl.getJsonNumber(value));
238+
setValueList(index, JsonNumberImpl.getJsonNumber(value, jsonContext.bigIntegerScaleLimit()));
241239
return this;
242240
}
243241

244242
@Override
245243
public JsonArrayBuilder set(int index, BigInteger value) {
246244
validateValue(value);
247-
setValueList(index, JsonNumberImpl.getJsonNumber(value));
245+
setValueList(index, JsonNumberImpl.getJsonNumber(value, jsonContext.bigIntegerScaleLimit()));
248246
return this;
249247
}
250248

251249
@Override
252250
public JsonArrayBuilder set(int index, int value) {
253-
setValueList(index, JsonNumberImpl.getJsonNumber(value));
251+
setValueList(index, JsonNumberImpl.getJsonNumber(value, jsonContext.bigIntegerScaleLimit()));
254252
return this;
255253
}
256254

257255
@Override
258256
public JsonArrayBuilder set(int index, long value) {
259-
setValueList(index, JsonNumberImpl.getJsonNumber(value));
257+
setValueList(index, JsonNumberImpl.getJsonNumber(value, jsonContext.bigIntegerScaleLimit()));
260258
return this;
261259
}
262260

263261
@Override
264262
public JsonArrayBuilder set(int index, double value) {
265-
setValueList(index, JsonNumberImpl.getJsonNumber(value));
263+
setValueList(index, JsonNumberImpl.getJsonNumber(value, jsonContext.bigIntegerScaleLimit()));
266264
return this;
267265
}
268266

@@ -316,16 +314,16 @@ public JsonArray build() {
316314
snapshot = Collections.unmodifiableList(valueList);
317315
}
318316
valueList = null;
319-
return new JsonArrayImpl(snapshot, bufferPool);
317+
return new JsonArrayImpl(snapshot, jsonContext);
320318
}
321319

322320
private void populate(Collection<?> collection) {
323321
for (Object value : collection) {
324-
if (value != null && value instanceof Optional) {
322+
if (value instanceof Optional) {
325323
((Optional<?>) value).ifPresent(v ->
326-
this.valueList.add(MapUtil.handle(v, bufferPool)));
324+
this.valueList.add(MapUtil.handle(v, jsonContext)));
327325
} else {
328-
this.valueList.add(MapUtil.handle(value, bufferPool));
326+
this.valueList.add(MapUtil.handle(value, jsonContext));
329327
}
330328
}
331329
}
@@ -359,12 +357,12 @@ private void validateValue(Object value) {
359357

360358
private static final class JsonArrayImpl extends AbstractList<JsonValue> implements JsonArray {
361359
private final List<JsonValue> valueList; // Unmodifiable
362-
private final BufferPool bufferPool;
360+
private final JsonContext jsonContext;
363361
private int hashCode;
364362

365-
JsonArrayImpl(List<JsonValue> valueList, BufferPool bufferPool) {
363+
JsonArrayImpl(List<JsonValue> valueList, JsonContext jsonContext) {
366364
this.valueList = valueList;
367-
this.bufferPool = bufferPool;
365+
this.jsonContext = jsonContext;
368366
}
369367

370368
@Override
@@ -473,7 +471,7 @@ public int hashCode() {
473471
@Override
474472
public String toString() {
475473
StringWriter sw = new StringWriter();
476-
try (JsonWriter jw = new JsonWriterImpl(sw, bufferPool)) {
474+
try (JsonWriter jw = new JsonWriterImpl(sw, jsonContext)) {
477475
jw.write(this);
478476
}
479477
return sw.toString();
Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2021 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2023 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -17,62 +17,58 @@
1717
package org.eclipse.parsson;
1818

1919
import java.util.Collection;
20-
import org.eclipse.parsson.api.BufferPool;
2120

2221
import jakarta.json.JsonObject;
2322
import jakarta.json.JsonArray;
2423
import jakarta.json.JsonArrayBuilder;
2524
import jakarta.json.JsonBuilderFactory;
2625
import jakarta.json.JsonObjectBuilder;
27-
import java.util.Collections;
2826
import java.util.Map;
2927

3028
/**
3129
* @author Jitendra Kotamraju
3230
*/
3331
class JsonBuilderFactoryImpl implements JsonBuilderFactory {
34-
private final Map<String, ?> config;
35-
private final BufferPool bufferPool;
36-
private final boolean rejectDuplicateKeys;
3732

38-
JsonBuilderFactoryImpl(BufferPool bufferPool, boolean rejectDuplicateKeys) {
39-
this.config = Collections.emptyMap();
40-
this.bufferPool = bufferPool;
41-
this.rejectDuplicateKeys = rejectDuplicateKeys;
33+
private final JsonContext jsonContext;
34+
35+
JsonBuilderFactoryImpl(JsonContext jsonContext) {
36+
this.jsonContext = jsonContext;
4237
}
4338

4439
@Override
4540
public JsonObjectBuilder createObjectBuilder() {
46-
return new JsonObjectBuilderImpl(bufferPool, rejectDuplicateKeys, Collections.emptyMap());
41+
return new JsonObjectBuilderImpl(jsonContext);
4742
}
4843

4944
@Override
5045
public JsonObjectBuilder createObjectBuilder(JsonObject object) {
51-
return new JsonObjectBuilderImpl(object, bufferPool, rejectDuplicateKeys, Collections.emptyMap());
46+
return new JsonObjectBuilderImpl(object, jsonContext);
5247
}
5348

5449
@Override
5550
public JsonObjectBuilder createObjectBuilder(Map<String, Object> object) {
56-
return new JsonObjectBuilderImpl(object, bufferPool, rejectDuplicateKeys, Collections.emptyMap());
51+
return new JsonObjectBuilderImpl(object, jsonContext);
5752
}
5853

5954
@Override
6055
public JsonArrayBuilder createArrayBuilder() {
61-
return new JsonArrayBuilderImpl(bufferPool);
56+
return new JsonArrayBuilderImpl(jsonContext);
6257
}
6358

6459
@Override
6560
public JsonArrayBuilder createArrayBuilder(JsonArray array) {
66-
return new JsonArrayBuilderImpl(array, bufferPool);
61+
return new JsonArrayBuilderImpl(array, jsonContext);
6762
}
6863

6964
@Override
7065
public JsonArrayBuilder createArrayBuilder(Collection<?> collection) {
71-
return new JsonArrayBuilderImpl(collection, bufferPool);
66+
return new JsonArrayBuilderImpl(collection, jsonContext);
7267
}
7368

7469
@Override
7570
public Map<String, ?> getConfigInUse() {
76-
return config;
71+
return jsonContext.config();
7772
}
73+
7874
}

0 commit comments

Comments
 (0)