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
1616
1717package org .eclipse .parsson ;
1818
19- import org .eclipse .parsson .api .BufferPool ;
20-
2119import jakarta .json .*;
2220import java .io .StringWriter ;
2321import java .math .BigDecimal ;
3533 * @author Jitendra Kotamraju
3634 * @author Kin-man Chung
3735 */
38-
3936class 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 ();
0 commit comments