Skip to content

Commit

Permalink
Limit BigDecimal scale value for JsonNumber#bigIntegerValue() and Jso…
Browse files Browse the repository at this point in the history
…nNumber#bigIntegerValueExact() (#78)

Signed-off-by: Tomáš Kraus <tomas.kraus@oracle.com>
  • Loading branch information
Tomas-Kraus committed Apr 21, 2023
1 parent 10b8d10 commit 7f2ed19
Show file tree
Hide file tree
Showing 29 changed files with 756 additions and 525 deletions.
66 changes: 32 additions & 34 deletions impl/src/main/java/org/eclipse/parsson/JsonArrayBuilderImpl.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -16,8 +16,6 @@

package org.eclipse.parsson;

import org.eclipse.parsson.api.BufferPool;

import jakarta.json.*;
import java.io.StringWriter;
import java.math.BigDecimal;
Expand All @@ -35,23 +33,23 @@
* @author Jitendra Kotamraju
* @author Kin-man Chung
*/

class JsonArrayBuilderImpl implements JsonArrayBuilder {

private ArrayList<JsonValue> valueList;
private final BufferPool bufferPool;
private final JsonContext jsonContext;

JsonArrayBuilderImpl(BufferPool bufferPool) {
this.bufferPool = bufferPool;
JsonArrayBuilderImpl(JsonContext jsonContext) {
this.jsonContext = jsonContext;
}

JsonArrayBuilderImpl(JsonArray array, BufferPool bufferPool) {
this.bufferPool = bufferPool;
JsonArrayBuilderImpl(JsonArray array, JsonContext jsonContext) {
this(jsonContext);
valueList = new ArrayList<>();
valueList.addAll(array);
}

JsonArrayBuilderImpl(Collection<?> collection, BufferPool bufferPool) {
this.bufferPool = bufferPool;
JsonArrayBuilderImpl(Collection<?> collection, JsonContext jsonContext) {
this(jsonContext);
valueList = new ArrayList<>();
populate(collection);
}
Expand All @@ -73,32 +71,32 @@ public JsonArrayBuilder add(String value) {
@Override
public JsonArrayBuilder add(BigDecimal value) {
validateValue(value);
addValueList(JsonNumberImpl.getJsonNumber(value));
addValueList(JsonNumberImpl.getJsonNumber(value, jsonContext.bigIntegerScaleLimit()));
return this;
}

@Override
public JsonArrayBuilder add(BigInteger value) {
validateValue(value);
addValueList(JsonNumberImpl.getJsonNumber(value));
addValueList(JsonNumberImpl.getJsonNumber(value, jsonContext.bigIntegerScaleLimit()));
return this;
}

@Override
public JsonArrayBuilder add(int value) {
addValueList(JsonNumberImpl.getJsonNumber(value));
addValueList(JsonNumberImpl.getJsonNumber(value, jsonContext.bigIntegerScaleLimit()));
return this;
}

@Override
public JsonArrayBuilder add(long value) {
addValueList(JsonNumberImpl.getJsonNumber(value));
addValueList(JsonNumberImpl.getJsonNumber(value, jsonContext.bigIntegerScaleLimit()));
return this;
}

@Override
public JsonArrayBuilder add(double value) {
addValueList(JsonNumberImpl.getJsonNumber(value));
addValueList(JsonNumberImpl.getJsonNumber(value, jsonContext.bigIntegerScaleLimit()));
return this;
}

Expand Down Expand Up @@ -161,32 +159,32 @@ public JsonArrayBuilder add(int index, String value) {
@Override
public JsonArrayBuilder add(int index, BigDecimal value) {
validateValue(value);
addValueList(index, JsonNumberImpl.getJsonNumber(value));
addValueList(index, JsonNumberImpl.getJsonNumber(value, jsonContext.bigIntegerScaleLimit()));
return this;
}

@Override
public JsonArrayBuilder add(int index, BigInteger value) {
validateValue(value);
addValueList(index, JsonNumberImpl.getJsonNumber(value));
addValueList(index, JsonNumberImpl.getJsonNumber(value, jsonContext.bigIntegerScaleLimit()));
return this;
}

@Override
public JsonArrayBuilder add(int index, int value) {
addValueList(index, JsonNumberImpl.getJsonNumber(value));
addValueList(index, JsonNumberImpl.getJsonNumber(value, jsonContext.bigIntegerScaleLimit()));
return this;
}

@Override
public JsonArrayBuilder add(int index, long value) {
addValueList(index, JsonNumberImpl.getJsonNumber(value));
addValueList(index, JsonNumberImpl.getJsonNumber(value, jsonContext.bigIntegerScaleLimit()));
return this;
}

@Override
public JsonArrayBuilder add(int index, double value) {
addValueList(index, JsonNumberImpl.getJsonNumber(value));
addValueList(index, JsonNumberImpl.getJsonNumber(value, jsonContext.bigIntegerScaleLimit()));
return this;
}

Expand Down Expand Up @@ -237,32 +235,32 @@ public JsonArrayBuilder set(int index, String value) {
@Override
public JsonArrayBuilder set(int index, BigDecimal value) {
validateValue(value);
setValueList(index, JsonNumberImpl.getJsonNumber(value));
setValueList(index, JsonNumberImpl.getJsonNumber(value, jsonContext.bigIntegerScaleLimit()));
return this;
}

@Override
public JsonArrayBuilder set(int index, BigInteger value) {
validateValue(value);
setValueList(index, JsonNumberImpl.getJsonNumber(value));
setValueList(index, JsonNumberImpl.getJsonNumber(value, jsonContext.bigIntegerScaleLimit()));
return this;
}

@Override
public JsonArrayBuilder set(int index, int value) {
setValueList(index, JsonNumberImpl.getJsonNumber(value));
setValueList(index, JsonNumberImpl.getJsonNumber(value, jsonContext.bigIntegerScaleLimit()));
return this;
}

@Override
public JsonArrayBuilder set(int index, long value) {
setValueList(index, JsonNumberImpl.getJsonNumber(value));
setValueList(index, JsonNumberImpl.getJsonNumber(value, jsonContext.bigIntegerScaleLimit()));
return this;
}

@Override
public JsonArrayBuilder set(int index, double value) {
setValueList(index, JsonNumberImpl.getJsonNumber(value));
setValueList(index, JsonNumberImpl.getJsonNumber(value, jsonContext.bigIntegerScaleLimit()));
return this;
}

Expand Down Expand Up @@ -316,16 +314,16 @@ public JsonArray build() {
snapshot = Collections.unmodifiableList(valueList);
}
valueList = null;
return new JsonArrayImpl(snapshot, bufferPool);
return new JsonArrayImpl(snapshot, jsonContext);
}

private void populate(Collection<?> collection) {
for (Object value : collection) {
if (value != null && value instanceof Optional) {
if (value instanceof Optional) {
((Optional<?>) value).ifPresent(v ->
this.valueList.add(MapUtil.handle(v, bufferPool)));
this.valueList.add(MapUtil.handle(v, jsonContext)));
} else {
this.valueList.add(MapUtil.handle(value, bufferPool));
this.valueList.add(MapUtil.handle(value, jsonContext));
}
}
}
Expand Down Expand Up @@ -359,12 +357,12 @@ private void validateValue(Object value) {

private static final class JsonArrayImpl extends AbstractList<JsonValue> implements JsonArray {
private final List<JsonValue> valueList; // Unmodifiable
private final BufferPool bufferPool;
private final JsonContext jsonContext;
private int hashCode;

JsonArrayImpl(List<JsonValue> valueList, BufferPool bufferPool) {
JsonArrayImpl(List<JsonValue> valueList, JsonContext jsonContext) {
this.valueList = valueList;
this.bufferPool = bufferPool;
this.jsonContext = jsonContext;
}

@Override
Expand Down Expand Up @@ -473,7 +471,7 @@ public int hashCode() {
@Override
public String toString() {
StringWriter sw = new StringWriter();
try (JsonWriter jw = new JsonWriterImpl(sw, bufferPool)) {
try (JsonWriter jw = new JsonWriterImpl(sw, jsonContext)) {
jw.write(this);
}
return sw.toString();
Expand Down
30 changes: 13 additions & 17 deletions impl/src/main/java/org/eclipse/parsson/JsonBuilderFactoryImpl.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -17,62 +17,58 @@
package org.eclipse.parsson;

import java.util.Collection;
import org.eclipse.parsson.api.BufferPool;

import jakarta.json.JsonObject;
import jakarta.json.JsonArray;
import jakarta.json.JsonArrayBuilder;
import jakarta.json.JsonBuilderFactory;
import jakarta.json.JsonObjectBuilder;
import java.util.Collections;
import java.util.Map;

/**
* @author Jitendra Kotamraju
*/
class JsonBuilderFactoryImpl implements JsonBuilderFactory {
private final Map<String, ?> config;
private final BufferPool bufferPool;
private final boolean rejectDuplicateKeys;

JsonBuilderFactoryImpl(BufferPool bufferPool, boolean rejectDuplicateKeys) {
this.config = Collections.emptyMap();
this.bufferPool = bufferPool;
this.rejectDuplicateKeys = rejectDuplicateKeys;
private final JsonContext jsonContext;

JsonBuilderFactoryImpl(JsonContext jsonContext) {
this.jsonContext = jsonContext;
}

@Override
public JsonObjectBuilder createObjectBuilder() {
return new JsonObjectBuilderImpl(bufferPool, rejectDuplicateKeys, Collections.emptyMap());
return new JsonObjectBuilderImpl(jsonContext);
}

@Override
public JsonObjectBuilder createObjectBuilder(JsonObject object) {
return new JsonObjectBuilderImpl(object, bufferPool, rejectDuplicateKeys, Collections.emptyMap());
return new JsonObjectBuilderImpl(object, jsonContext);
}

@Override
public JsonObjectBuilder createObjectBuilder(Map<String, Object> object) {
return new JsonObjectBuilderImpl(object, bufferPool, rejectDuplicateKeys, Collections.emptyMap());
return new JsonObjectBuilderImpl(object, jsonContext);
}

@Override
public JsonArrayBuilder createArrayBuilder() {
return new JsonArrayBuilderImpl(bufferPool);
return new JsonArrayBuilderImpl(jsonContext);
}

@Override
public JsonArrayBuilder createArrayBuilder(JsonArray array) {
return new JsonArrayBuilderImpl(array, bufferPool);
return new JsonArrayBuilderImpl(array, jsonContext);
}

@Override
public JsonArrayBuilder createArrayBuilder(Collection<?> collection) {
return new JsonArrayBuilderImpl(collection, bufferPool);
return new JsonArrayBuilderImpl(collection, jsonContext);
}

@Override
public Map<String, ?> getConfigInUse() {
return config;
return jsonContext.config();
}

}

0 comments on commit 7f2ed19

Please sign in to comment.