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

Number serialization of java types outside of IEEE754 double precisio… #176

Closed
wants to merge 1 commit into from
Closed
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
32 changes: 15 additions & 17 deletions impl/src/main/java/org/glassfish/json/JsonArrayBuilderImpl.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2017 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2019 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.glassfish.json;

import org.glassfish.json.api.BufferPool;

import javax.json.*;
import java.io.StringWriter;
import java.math.BigDecimal;
Expand All @@ -38,20 +36,20 @@

class JsonArrayBuilderImpl implements JsonArrayBuilder {
private ArrayList<JsonValue> valueList;
private final BufferPool bufferPool;
private final JsonConfig config;

JsonArrayBuilderImpl(BufferPool bufferPool) {
this.bufferPool = bufferPool;
JsonArrayBuilderImpl(JsonConfig config) {
this.config = config;
}

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

JsonArrayBuilderImpl(Collection<?> collection, BufferPool bufferPool) {
this.bufferPool = bufferPool;
JsonArrayBuilderImpl(Collection<?> collection, JsonConfig config) {
this.config = config;
valueList = new ArrayList<>();
populate(collection);
}
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, config);
}

private void populate(Collection<?> collection) {
for (Object value : collection) {
if (value != null && value instanceof Optional) {
((Optional<?>) value).ifPresent(v ->
this.valueList.add(MapUtil.handle(v, bufferPool)));
this.valueList.add(MapUtil.handle(v, config)));
} else {
this.valueList.add(MapUtil.handle(value, bufferPool));
this.valueList.add(MapUtil.handle(value, config));
}
}
}
Expand Down Expand Up @@ -359,11 +357,11 @@ 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 JsonConfig config;

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

@Override
Expand Down Expand Up @@ -464,7 +462,7 @@ public JsonValue get(int index) {
@Override
public String toString() {
StringWriter sw = new StringWriter();
try (JsonWriter jw = new JsonWriterImpl(sw, bufferPool)) {
try (JsonWriter jw = new JsonWriterImpl(sw, config)) {
jw.write(this);
}
return sw.toString();
Expand Down
26 changes: 11 additions & 15 deletions impl/src/main/java/org/glassfish/json/JsonBuilderFactoryImpl.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2017 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2019 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,60 +17,56 @@
package org.glassfish.json;

import java.util.Collection;
import org.glassfish.json.api.BufferPool;

import javax.json.JsonObject;
import javax.json.JsonArray;
import javax.json.JsonArrayBuilder;
import javax.json.JsonBuilderFactory;
import javax.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 JsonConfig config;

JsonBuilderFactoryImpl(BufferPool bufferPool) {
this.config = Collections.emptyMap();
this.bufferPool = bufferPool;
JsonBuilderFactoryImpl(JsonConfig config) {
this.config = config;
}

@Override
public JsonObjectBuilder createObjectBuilder() {
return new JsonObjectBuilderImpl(bufferPool);
return new JsonObjectBuilderImpl(config);
}

@Override
public JsonObjectBuilder createObjectBuilder(JsonObject object) {
return new JsonObjectBuilderImpl(object, bufferPool);
return new JsonObjectBuilderImpl(object, config);
}

@Override
public JsonObjectBuilder createObjectBuilder(Map<String, Object> object) {
return new JsonObjectBuilderImpl(object, bufferPool);
return new JsonObjectBuilderImpl(object, config);
}

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

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

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

@Override
public Map<String, ?> getConfigInUse() {
return config;
return config.toConfigInUse();
}
}
122 changes: 122 additions & 0 deletions impl/src/main/java/org/glassfish/json/JsonConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* Copyright (c) 2019 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
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.json;

import org.glassfish.json.api.BufferPool;

import javax.json.stream.JsonGenerationException;
import javax.json.stream.JsonGenerator;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
* Parses and stores configuration supported by org.glassfish.json JSONP implementation.
*/
public class JsonConfig {

private final BufferPool bufferPool;
private final boolean prettyPrinting;
private final String bigNumberStrategy;

/**
* Create configuration from a String, Object property map.
*
* @param config properties to parse
*/
public JsonConfig(Map<String, ?> config) {
if (config == null) {
prettyPrinting = false;
bufferPool = new BufferPoolImpl();
bigNumberStrategy = NumberStrategy.JSON_NUMBER;
} else {
prettyPrinting = JsonProviderImpl.isPrettyPrintingEnabled(config);
Object bufferPool = config.get(BufferPool.class.getName());
if (bufferPool == null) {
this.bufferPool = new BufferPoolImpl();
} else {
if (!(bufferPool instanceof BufferPool)) {
throw new JsonGenerationException(BufferPool.class.getName()
+ " must be an instance of a " + BufferPool.class.getName());
}
this.bufferPool = (BufferPool) bufferPool;
}

Object bigNumberStrategy = config.get(NumberStrategy.class.getName());
if (bigNumberStrategy == null) {
this.bigNumberStrategy = NumberStrategy.JSON_NUMBER;
} else {
if (!(bigNumberStrategy instanceof String)) {
throw new JsonGenerationException(NumberStrategy.class.getName()
+ " must be an instance of a " + String.class.getName());
}
this.bigNumberStrategy = (String) bigNumberStrategy;
}
}
}

/**
* Creates configuration with default values:
*
* Pretty printing: false.
* BufferPool: org.glassfish.json.BufferPoolImpl.
* Big number strategy: JSON_NUMBER.
*/
public JsonConfig() {
this.prettyPrinting = false;
this.bufferPool = new BufferPoolImpl();
this.bigNumberStrategy = NumberStrategy.JSON_NUMBER;
}

/**
* Buffer pool to use.
*
* @return buffer pool.
*/
public BufferPool getBufferPool() {
return bufferPool;
}

/**
* Pretty printing enabled for json generation.
*
* @return true if enabled
*/
public boolean isPrettyPrinting() {
return prettyPrinting;
}

/**
* One of: {@link NumberStrategy} JSON_NUMBER, JSON_STRING.
*
* @return Number strategy.
*/
public String getBigNumberStrategy() {
return bigNumberStrategy;
}

/**
* All configuration properties currently used.
* @return used configuration.
*/
public Map<String, ?> toConfigInUse() {
Map<String, Object> configInUse = new HashMap<>();
configInUse.put(JsonGenerator.PRETTY_PRINTING, prettyPrinting);
configInUse.put(BufferPool.class.getName(), bufferPool);
configInUse.put(NumberStrategy.class.getName(), bigNumberStrategy);
return Collections.unmodifiableMap(configInUse);
}
}
34 changes: 14 additions & 20 deletions impl/src/main/java/org/glassfish/json/JsonGeneratorFactoryImpl.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2017 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2019 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.glassfish.json;

import org.glassfish.json.api.BufferPool;

import javax.json.stream.JsonGenerator;
import javax.json.stream.JsonGeneratorFactory;
import java.io.OutputStream;
Expand All @@ -30,41 +28,37 @@
*/
class JsonGeneratorFactoryImpl implements JsonGeneratorFactory {

private final boolean prettyPrinting;
private final Map<String, ?> config; // unmodifiable map
private final BufferPool bufferPool;
private JsonConfig config;


JsonGeneratorFactoryImpl(Map<String, ?> config, boolean prettyPrinting,
BufferPool bufferPool) {
JsonGeneratorFactoryImpl(JsonConfig config) {
this.config = config;
this.prettyPrinting = prettyPrinting;
this.bufferPool = bufferPool;
}

@Override
public JsonGenerator createGenerator(Writer writer) {
return prettyPrinting
? new JsonPrettyGeneratorImpl(writer, bufferPool)
: new JsonGeneratorImpl(writer, bufferPool);
return config.isPrettyPrinting()
? new JsonPrettyGeneratorImpl(writer, config)
: new JsonGeneratorImpl(writer, config);
}

@Override
public JsonGenerator createGenerator(OutputStream out) {
return prettyPrinting
? new JsonPrettyGeneratorImpl(out, bufferPool)
: new JsonGeneratorImpl(out, bufferPool);
return config.isPrettyPrinting()
? new JsonPrettyGeneratorImpl(out, config)
: new JsonGeneratorImpl(out, config);
}

@Override
public JsonGenerator createGenerator(OutputStream out, Charset charset) {
return prettyPrinting
? new JsonPrettyGeneratorImpl(out, charset, bufferPool)
: new JsonGeneratorImpl(out, charset, bufferPool);
return config.isPrettyPrinting()
? new JsonPrettyGeneratorImpl(out, charset, config)
: new JsonGeneratorImpl(out, charset, config);
}

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

}
Loading