You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@johnzon.apache.org by sa...@apache.org on 2014/09/11 20:29:26 UTC
[10/22] renamed fleece to johnzon
http://git-wip-us.apache.org/repos/asf/incubator-johnzon/blob/6e86a53e/johnzon-core/src/main/java/org/apache/johnzon/core/JsonDoubleImpl.java
----------------------------------------------------------------------
diff --git a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonDoubleImpl.java b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonDoubleImpl.java
new file mode 100644
index 0000000..9ddfd58
--- /dev/null
+++ b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonDoubleImpl.java
@@ -0,0 +1,102 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.johnzon.core;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+
+import javax.json.JsonNumber;
+
+final class JsonDoubleImpl implements JsonNumber {
+ private final double value;
+
+ JsonDoubleImpl(final double value) {
+
+ if(Double.isInfinite(value) || Double.isNaN(value)) {
+ throw new NumberFormatException("double value must to be NaN or Infinite");
+ }
+
+ this.value = value;
+ }
+
+ @Override
+ public boolean isIntegral() {
+ return false;
+ }
+
+ @Override
+ public int intValue() {
+ return (int) value;
+ }
+
+ @Override
+ public int intValueExact() {
+ return intValue();
+ }
+
+ @Override
+ public long longValue() {
+ return (long) value;
+ }
+
+ @Override
+ public long longValueExact() {
+ return (long) value;
+ }
+
+ @Override
+ public BigInteger bigIntegerValue() {
+ return new BigDecimal(toString()).toBigInteger();
+ }
+
+ @Override
+ public BigInteger bigIntegerValueExact() {
+ return new BigDecimal(toString()).toBigIntegerExact();
+ }
+
+ @Override
+ public double doubleValue() {
+ return value;
+ }
+
+ @Override
+ public BigDecimal bigDecimalValue() {
+ return new BigDecimal(toString());
+ }
+
+ @Override
+ public ValueType getValueType() {
+ return ValueType.NUMBER;
+ }
+
+ @Override
+ public String toString() {
+ return Double.toString(value);
+ }
+
+ @Override
+ public int hashCode() {
+ return Double.valueOf(value).hashCode();
+ }
+
+ @Override
+ public boolean equals(final Object obj) {
+ return JsonNumber.class.isInstance(obj) && JsonNumber.class.cast(obj).doubleValue() == value;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-johnzon/blob/6e86a53e/johnzon-core/src/main/java/org/apache/johnzon/core/JsonGeneratorFactoryImpl.java
----------------------------------------------------------------------
diff --git a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonGeneratorFactoryImpl.java b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonGeneratorFactoryImpl.java
new file mode 100644
index 0000000..bdc19fb
--- /dev/null
+++ b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonGeneratorFactoryImpl.java
@@ -0,0 +1,121 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.johnzon.core;
+
+import java.io.OutputStream;
+import java.io.Serializable;
+import java.io.Writer;
+import java.nio.charset.Charset;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Locale;
+import java.util.Map;
+import java.util.concurrent.ConcurrentMap;
+
+import javax.json.stream.JsonGenerator;
+import javax.json.stream.JsonGeneratorFactory;
+
+public class JsonGeneratorFactoryImpl implements JsonGeneratorFactory, Serializable {
+ public static final String BUFFER_LENGTH = "org.apache.johnzon.default-char-buffer-generator";
+ public static final int DEFAULT_BUFFER_LENGTH = Integer.getInteger(BUFFER_LENGTH, 1024); //TODO check default string length/buffer size
+ private final Map<String, Object> internalConfig = new HashMap<String, Object>();
+ private static final String[] SUPPORTED_CONFIG_KEYS = new String[] {
+
+ JsonGenerator.PRETTY_PRINTING, BUFFER_LENGTH, JsonParserFactoryImpl.BUFFER_STRATEGY
+
+ };
+ //key caching currently disabled
+ private final ConcurrentMap<String, String> cache = null;//new ConcurrentHashMap<String, String>();
+ private final boolean pretty;
+ private final BufferStrategy.BufferProvider<char[]> bufferProvider;
+
+ public JsonGeneratorFactoryImpl(final Map<String, ?> config) {
+
+ if(config != null) {
+
+ for (String configKey : SUPPORTED_CONFIG_KEYS) {
+ if(config.containsKey(configKey)) {
+ internalConfig.put(configKey, config.get(configKey));
+ }
+ }
+ }
+
+ if(internalConfig.containsKey(JsonGenerator.PRETTY_PRINTING)) {
+ this.pretty = Boolean.TRUE.equals(internalConfig.get(JsonGenerator.PRETTY_PRINTING)) || "true".equals(internalConfig.get(JsonGenerator.PRETTY_PRINTING));
+ } else {
+ this.pretty = false;
+ }
+
+ final int bufferSize = getInt(BUFFER_LENGTH);
+ if (bufferSize <= 0) {
+ throw new IllegalArgumentException("buffer length must be greater than zero");
+ }
+
+ this.bufferProvider = getBufferProvider().newCharProvider(bufferSize);
+ }
+
+ private BufferStrategy getBufferProvider() {
+ final Object name = internalConfig.get(JsonParserFactoryImpl.BUFFER_STRATEGY);
+ if (name != null) {
+ return BufferStrategy.valueOf(name.toString().toUpperCase(Locale.ENGLISH));
+ }
+ return BufferStrategy.QUEUE;
+ }
+
+ private int getInt(final String key) {
+ final Object maxStringSize = internalConfig.get(key);
+ if (maxStringSize == null) {
+ return DEFAULT_BUFFER_LENGTH;
+ } else if (Number.class.isInstance(maxStringSize)) {
+ return Number.class.cast(maxStringSize).intValue();
+ }
+ return Integer.parseInt(maxStringSize.toString());
+ }
+
+ @Override
+ public JsonGenerator createGenerator(final Writer writer) {
+ if (pretty) {
+ return new JsonPrettyGeneratorImpl(writer, bufferProvider, cache);
+ }
+ return new JsonGeneratorImpl(writer, bufferProvider, cache);
+ }
+
+
+
+ @Override
+ public JsonGenerator createGenerator(final OutputStream out) {
+ if (pretty) {
+ return new JsonPrettyGeneratorImpl(out, bufferProvider, cache);
+ }
+ return new JsonGeneratorImpl(out, bufferProvider, cache);
+ }
+
+ @Override
+ public JsonGenerator createGenerator(final OutputStream out, final Charset charset) {
+ if (pretty) {
+ return new JsonPrettyGeneratorImpl(out,charset, bufferProvider, cache);
+ }
+ return new JsonGeneratorImpl(out,charset, bufferProvider, cache);
+ }
+
+ @Override
+ public Map<String, ?> getConfigInUse() {
+ return Collections.unmodifiableMap(internalConfig);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-johnzon/blob/6e86a53e/johnzon-core/src/main/java/org/apache/johnzon/core/JsonGeneratorImpl.java
----------------------------------------------------------------------
diff --git a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonGeneratorImpl.java b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonGeneratorImpl.java
new file mode 100644
index 0000000..6b539ad
--- /dev/null
+++ b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonGeneratorImpl.java
@@ -0,0 +1,870 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.johnzon.core;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.Serializable;
+import java.io.Writer;
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.nio.charset.Charset;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.concurrent.ConcurrentMap;
+
+import javax.json.JsonArray;
+import javax.json.JsonException;
+import javax.json.JsonNumber;
+import javax.json.JsonObject;
+import javax.json.JsonString;
+import javax.json.JsonStructure;
+import javax.json.JsonValue;
+import javax.json.stream.JsonGenerationException;
+import javax.json.stream.JsonGenerator;
+
+class JsonGeneratorImpl implements JsonGenerator, JsonChars, Serializable {
+ private static final Charset UTF8_CHARSET = Charset.forName("UTF-8");
+
+ private final Writer writer;
+ private final BufferStrategy.BufferProvider<char[]> bufferProvider;
+ private final char[] buffer;
+ private int bufferPos = 0;
+ //private final ConcurrentMap<String, String> cache;
+ protected boolean needComma = false;
+
+ private StructureElement currentStructureElement = null;
+ private boolean valid = false;
+ protected int depth = 0;
+
+ //minimal stack implementation
+ private static final class StructureElement {
+ final StructureElement previous;
+ final boolean isArray;
+
+ StructureElement(final StructureElement previous, final boolean isArray) {
+ super();
+ this.previous = previous;
+ this.isArray = isArray;
+ }
+ }
+
+ JsonGeneratorImpl(final Writer writer, final BufferStrategy.BufferProvider<char[]> bufferProvider,
+ final ConcurrentMap<String, String> cache) {
+ this.writer = writer;
+ //this.cache = cache;
+ this.buffer = bufferProvider.newBuffer();
+ this.bufferProvider = bufferProvider;
+ }
+
+ JsonGeneratorImpl(final OutputStream out, final BufferStrategy.BufferProvider<char[]> bufferProvider,
+ final ConcurrentMap<String, String> cache) {
+ this(new OutputStreamWriter(out, UTF8_CHARSET), bufferProvider, cache);
+ }
+
+ JsonGeneratorImpl(final OutputStream out, final Charset encoding, final BufferStrategy.BufferProvider<char[]> bufferProvider,
+ final ConcurrentMap<String, String> cache) {
+ this(new OutputStreamWriter(out, encoding), bufferProvider, cache);
+ }
+
+ protected void addCommaIfNeeded() {
+ if (needComma) {
+ justWrite(COMMA_CHAR);
+ needComma = false;
+ }
+
+ }
+
+ //caching currently disabled
+ //two problems:
+ // 1) not easy to get the escaped value efficiently wen its streamed and the buffer is full and needs to be flushed
+ // 2) we have to use a kind of bounded threadsafe map to let the cache not grow indefinitely
+ private void writeCachedOrEscape(final String name) {
+ /* String k = cache.get(name);
+
+ if (k == null) {
+
+ justWrite(QUOTE_CHAR);
+ int start = bufferPos;
+ writeEscaped0(name);
+ int end = bufferPos;
+ String escaped= get from buffer
+ ---
+ //FIXME if buffer is flushed this will not work here
+ cache.putIfAbsent(name, escaped);
+ justWrite(QUOTE_CHAR);
+ justWrite(KEY_SEPARATOR);
+ }else*/
+ {
+ justWrite(QUOTE_CHAR);
+ writeEscaped0(name);
+ justWrite(QUOTE_CHAR);
+ justWrite(KEY_SEPARATOR);
+ }
+
+ }
+
+ @Override
+ public JsonGenerator writeStartObject() {
+
+ if (currentStructureElement == null && valid) {
+ throw new JsonGenerationException("Method must not be called more than once in no context");
+ }
+
+ if (currentStructureElement != null && !currentStructureElement.isArray) {
+ throw new JsonGenerationException("Method must not be called within an object context");
+ }
+
+ //push upon the stack
+ if (currentStructureElement == null) {
+ currentStructureElement = new StructureElement(null, false);
+ } else {
+ final StructureElement localStructureElement = new StructureElement(currentStructureElement, false);
+ currentStructureElement = localStructureElement;
+ }
+
+ if (!valid) {
+ valid = true;
+ }
+
+ noCheckWrite(START_OBJECT_CHAR);
+ depth++;
+ return this;
+ }
+
+ @Override
+ public JsonGenerator writeStartObject(final String name) {
+ if (currentStructureElement == null || currentStructureElement.isArray) {
+ throw new JsonGenerationException("Method must not be called within an array context");
+ }
+
+ //push upon the stack
+ if (currentStructureElement == null) {
+ currentStructureElement = new StructureElement(null, false);
+ } else {
+ final StructureElement localStructureElement = new StructureElement(currentStructureElement, false);
+ currentStructureElement = localStructureElement;
+ }
+
+ addCommaIfNeeded();
+ writeCachedOrEscape(name);
+ noCheckWrite(START_OBJECT_CHAR);
+ depth++;
+ return this;
+ }
+
+ @Override
+ public JsonGenerator writeStartArray() {
+ if (currentStructureElement == null && valid) {
+ throw new JsonGenerationException("Method must not be called more than once in no context");
+ }
+
+ if (currentStructureElement != null && !currentStructureElement.isArray) {
+ throw new JsonGenerationException("Method must not be called within an object context");
+ }
+
+ //push upon the stack
+ if (currentStructureElement == null) {
+ currentStructureElement = new StructureElement(null, true);
+ } else {
+ final StructureElement localStructureElement = new StructureElement(currentStructureElement, true);
+ currentStructureElement = localStructureElement;
+ }
+
+ if (!valid) {
+ valid = true;
+ }
+
+ noCheckWrite(START_ARRAY_CHAR);
+ depth++;
+ return this;
+ }
+
+ @Override
+ public JsonGenerator writeStartArray(final String name) {
+ if (currentStructureElement == null || currentStructureElement.isArray) {
+ throw new JsonGenerationException("Method must not be called within an array context");
+ }
+
+ //push upon the stack
+ if (currentStructureElement == null) {
+ currentStructureElement = new StructureElement(null, true);
+ } else {
+ final StructureElement localStructureElement = new StructureElement(currentStructureElement, true);
+ currentStructureElement = localStructureElement;
+ }
+
+ addCommaIfNeeded();
+ writeCachedOrEscape(name);
+ noCheckWrite(START_ARRAY_CHAR);
+ depth++;
+ return this;
+ }
+
+ private void writeJsonValue(final String name, final JsonValue value) {
+ if (currentStructureElement != null) {
+ checkObject();
+ }
+ //TODO check null handling
+ switch (value.getValueType()) {
+ case ARRAY:
+ writeStartArray(name);
+ final JsonArray array = JsonArray.class.cast(value);
+ final Iterator<JsonValue> ait = array.iterator();
+ while (ait.hasNext()) {
+ write(ait.next());
+ }
+ writeEnd();
+
+ break;
+ case OBJECT:
+ writeStartObject(name);
+ final JsonObject object = JsonObject.class.cast(value);
+ final Iterator<Map.Entry<String, JsonValue>> oit = object.entrySet().iterator();
+ while (oit.hasNext()) {
+ final Map.Entry<String, JsonValue> keyval = oit.next();
+ write(keyval.getKey(), keyval.getValue());
+ }
+ writeEnd();
+
+ break;
+ case STRING:
+ write(name, JsonString.class.cast(value).getString());
+ break;
+ case NUMBER:
+ //TODO optimize
+ final JsonNumber number = JsonNumber.class.cast(value);
+ if (number.isIntegral()) {
+ write(name, number.longValueExact());
+ } else {
+ write(name, number.bigDecimalValue());
+ }
+ break;
+ case TRUE:
+ write(name, true);
+ break;
+ case FALSE:
+ write(name, false);
+ break;
+ case NULL:
+ writeNull(name);
+ break;
+ default:
+ throw new JsonGenerationException("Unknown JsonValue type");
+ }
+ }
+
+ private void writeJsonValue(final JsonValue value) {
+ if (currentStructureElement != null) {
+ checkArray();
+ }
+ //TODO check null handling
+ switch (value.getValueType()) {
+ case ARRAY:
+ writeStartArray();
+ final JsonArray array = JsonArray.class.cast(value);
+ final Iterator<JsonValue> ait = array.iterator();
+ while (ait.hasNext()) {
+ write(ait.next());
+ }
+ writeEnd();
+
+ break;
+ case OBJECT:
+ writeStartObject();
+ final JsonObject object = JsonObject.class.cast(value);
+ final Iterator<Map.Entry<String, JsonValue>> oit = object.entrySet().iterator();
+ while (oit.hasNext()) {
+ final Map.Entry<String, JsonValue> keyval = oit.next();
+ write(keyval.getKey(), keyval.getValue());
+ }
+ writeEnd();
+
+ break;
+ case STRING:
+ write(JsonString.class.cast(value).getString());
+ break;
+ case NUMBER:
+ //TODO optimize
+ final JsonNumber number = JsonNumber.class.cast(value);
+ if (number.isIntegral()) {
+ write(number.longValueExact());
+ } else {
+ write(number.bigDecimalValue());
+ }
+ break;
+ case TRUE:
+ write(true);
+ break;
+ case FALSE:
+ write(false);
+ break;
+ case NULL:
+ writeNull();
+ break;
+ default:
+ throw new JsonGenerationException("Unknown JsonValue type");
+ }
+ }
+
+ @Override
+ public JsonGenerator write(final String name, final JsonValue value) {
+ writeJsonValue(name, value);
+ return this;
+ }
+
+
+ @Override
+ public JsonGenerator write(final String name, final String value) {
+ checkObject();
+
+ addCommaIfNeeded();
+ writeCachedOrEscape(name);
+
+ addCommaIfNeeded();
+ justWrite(QUOTE_CHAR);
+ writeEscaped0(value);
+ justWrite(QUOTE_CHAR);
+ needComma = true;
+ return this;
+ }
+
+ @Override
+ public JsonGenerator write(final String name, final BigInteger value) {
+ checkObject();
+ addCommaIfNeeded();
+ writeCachedOrEscape(name);
+ noCheckWriteAndForceComma(String.valueOf(value));
+ return this;
+ }
+
+ @Override
+ public JsonGenerator write(final String name, final BigDecimal value) {
+ checkObject();
+ addCommaIfNeeded();
+ writeCachedOrEscape(name);
+ noCheckWriteAndForceComma(String.valueOf(value));
+ return this;
+ }
+
+ @Override
+ public JsonGenerator write(final String name, final int value) {
+ checkObject();
+ addCommaIfNeeded();
+ writeCachedOrEscape(name);
+ addCommaIfNeeded();
+ writeInt0(value);
+ needComma = true;
+ return this;
+ }
+
+ @Override
+ public JsonGenerator write(final String name, final long value) {
+ checkObject();
+ addCommaIfNeeded();
+ writeCachedOrEscape(name);
+ addCommaIfNeeded();
+ writeLong0(value);
+ needComma = true;
+ return this;
+ }
+
+ @Override
+ public JsonGenerator write(final String name, final double value) {
+ checkObject();
+ checkDoubleRange(value);
+ addCommaIfNeeded();
+ writeCachedOrEscape(name);
+ noCheckWriteAndForceComma(String.valueOf(value));
+ return this;
+ }
+
+ @Override
+ public JsonGenerator write(final String name, final boolean value) {
+ checkObject();
+ addCommaIfNeeded();
+ writeCachedOrEscape(name);
+ noCheckWriteAndForceComma(String.valueOf(value));
+ return this;
+ }
+
+ @Override
+ public JsonGenerator writeNull(final String name) {
+ checkObject();
+ addCommaIfNeeded();
+ writeCachedOrEscape(name);
+ noCheckWriteAndForceComma(NULL);
+ return this;
+ }
+
+ @Override
+ public JsonGenerator writeEnd() {
+ if (currentStructureElement == null) {
+ throw new JsonGenerationException("Method must not be called in no context");
+ }
+
+ writeEnd(currentStructureElement.isArray ? END_ARRAY_CHAR : END_OBJECT_CHAR);
+
+ //pop from stack
+ currentStructureElement = currentStructureElement.previous;
+ depth--;
+
+ return this;
+ }
+
+ @Override
+ public JsonGenerator write(final JsonValue value) {
+ writeJsonValue(value);
+
+ if (JsonStructure.class.isInstance(value)) {
+ valid = true;
+ }
+ return this;
+ }
+
+ @Override
+ public JsonGenerator write(final String value) {
+ checkArray();
+ addCommaIfNeeded();
+ justWrite(QUOTE_CHAR);
+ writeEscaped0(value);
+ justWrite(QUOTE_CHAR);
+ needComma = true;
+ return this;
+ }
+
+
+ @Override
+ public JsonGenerator write(final BigDecimal value) {
+ checkArray();
+ noCheckWrite(String.valueOf(value));
+ needComma = true;
+ return this;
+ }
+
+ @Override
+ public JsonGenerator write(final BigInteger value) {
+ checkArray();
+ noCheckWrite(String.valueOf(value));
+ needComma = true;
+ return this;
+ }
+
+ @Override
+ public JsonGenerator write(final int value) {
+ checkArray();
+ addCommaIfNeeded();
+ writeInt0(value);
+ needComma = true;
+ return this;
+ }
+
+ @Override
+ public JsonGenerator write(final long value) {
+ checkArray();
+ addCommaIfNeeded();
+ writeLong0(value);
+ needComma = true;
+ return this;
+ }
+
+ @Override
+ public JsonGenerator write(final double value) {
+ checkArray();
+ checkDoubleRange(value);
+ noCheckWrite(Double.toString(value));
+ needComma = true;
+ return this;
+ }
+
+ @Override
+ public JsonGenerator write(final boolean value) {
+ checkArray();
+ noCheckWrite(Boolean.toString(value));
+ needComma = true;
+ return this;
+ }
+
+ @Override
+ public JsonGenerator writeNull() {
+ checkArray();
+ noCheckWriteAndForceComma(NULL);
+ needComma = true;
+ return this;
+ }
+
+ @Override
+ public void close() {
+
+ try {
+ if (currentStructureElement != null || !valid) {
+
+ throw new JsonGenerationException("Invalid json " + currentStructureElement + " " + valid);
+ }
+ } finally {
+
+ flushBuffer();
+
+ try {
+ writer.close();
+ } catch (final IOException e) {
+ throw new JsonException(e.getMessage(), e);
+ }
+
+ bufferProvider.release(buffer);
+ }
+ }
+
+ @Override
+ public void flush() {
+
+ flushBuffer();
+
+ try {
+ writer.flush();
+ } catch (final IOException e) {
+ throw new JsonException(e.getMessage(), e);
+ }
+ }
+
+ private JsonGenerator noCheckWriteAndForceComma(final String value) {
+ noCheckWrite(value);
+ needComma = true;
+ return this;
+ }
+
+ protected JsonGenerator writeEnd(final char value) {
+ justWrite(value);
+ needComma = true;
+ return this;
+ }
+
+ protected void noCheckWrite(final String value) {
+ addCommaIfNeeded();
+ justWrite(value);
+ }
+
+ protected void noCheckWrite(final char value) {
+ addCommaIfNeeded();
+ justWrite(value);
+ }
+
+ private void flushBuffer() {
+
+ if (bufferPos > 0) {
+
+ try {
+ writer.write(buffer, 0, bufferPos);
+ bufferPos = 0;
+ } catch (final IOException e) {
+ throw new JsonException(e.getMessage(), e);
+ }
+
+ }
+ }
+
+ private void writeEscaped0(final String value) {
+ int len = 0;
+ if (value == null || (len = value.length()) == 0) {
+ return;
+ }
+
+ for (int i = 0; i < value.length(); i++) {
+ char c = value.charAt(i);
+
+ while (c != ESCAPE_CHAR && c != QUOTE_CHAR && c >= SPACE) {
+
+ //read fast
+ justWrite(c);
+
+ if (i >= len - 1) {
+ return;
+ }
+
+ i++;
+ c = value.charAt(i);
+ }
+
+ switch (c) {
+ case QUOTE_CHAR:
+ case ESCAPE_CHAR:
+ justWrite(ESCAPE_CHAR);
+ justWrite(c);
+ break;
+ default:
+ if (c < SPACE) {
+ switch (c) {
+ case EOL:
+ justWrite("\\n");
+ break;
+ case '\r':
+ justWrite("\\r");
+ break;
+ case '\t':
+ justWrite("\\t");
+ break;
+ case '\b':
+ justWrite("\\b");
+ break;
+ case '\f':
+ justWrite("\\f");
+ break;
+ default:
+ justWrite(toUnicode(c));
+ }
+ } else if ((c >= '\u0080' && c < '\u00a0') || (c >= '\u2000' && c < '\u2100')) {
+ justWrite(toUnicode(c));
+ } else {
+ justWrite(c);
+ }
+ }
+ }
+
+ }
+
+ private static final String UNICODE_PREFIX = "\\u";
+ private static final String UNICODE_PREFIX_HELPER = "000";
+
+ private static String toUnicode(final char c) {
+
+ final String hex = UNICODE_PREFIX_HELPER + Integer.toHexString(c);
+ final String s = UNICODE_PREFIX + hex.substring(hex.length() - 4);
+
+ return s;
+ }
+
+ protected void justWrite(final char[] chars) {
+
+ if (bufferPos + chars.length >= buffer.length) {
+
+ int start = 0;
+ int len = buffer.length - bufferPos;
+
+ while (true) {
+ int end = start + len;
+ if (end > chars.length) {
+ end = chars.length;
+ }
+
+ System.arraycopy(chars, start, buffer, bufferPos, end - start);
+
+ bufferPos += (end - start);
+ start += (len);
+
+ if (start >= chars.length) {
+ return;
+ }
+
+ if (bufferPos >= buffer.length) {
+ flushBuffer();
+ len = buffer.length;
+ }
+
+ }
+
+ } else {
+ //fits completely into the buffer
+ System.arraycopy(chars, 0, buffer, bufferPos, chars.length);
+ bufferPos += chars.length;
+ }
+
+ }
+
+ protected void justWrite(final String value) {
+ final int valueLength = value.length();
+
+ if (bufferPos + valueLength >= buffer.length) {
+
+ int start = 0;
+ int len = buffer.length - bufferPos;
+
+ while (true) {
+ int end = start + len;
+ if (end > valueLength) {
+ end = valueLength;
+ }
+
+ value.getChars(start, end, buffer, bufferPos);
+
+ bufferPos += (end - start);
+ start += (len);
+
+ if (start >= valueLength) {
+ return;
+ }
+
+ if (bufferPos >= buffer.length) {
+ flushBuffer();
+ len = buffer.length;
+ }
+
+ }
+
+ } else {
+ //fits completely into the buffer
+ value.getChars(0, valueLength, buffer, bufferPos);
+ bufferPos += valueLength;
+ }
+
+ }
+
+ protected void justWrite(final char value) {
+
+ if (bufferPos >= buffer.length) {
+ flushBuffer();
+ }
+
+ buffer[bufferPos++] = value;
+
+ }
+
+ private void checkObject() {
+ if (currentStructureElement == null || currentStructureElement.isArray) {
+ throw new JsonGenerationException("write(name, param) is only valid in objects");
+ }
+ }
+
+ private void checkArray() {
+ if (currentStructureElement == null || !currentStructureElement.isArray) {
+ throw new JsonGenerationException("write(param) is only valid in arrays");
+ }
+ }
+
+ private static void checkDoubleRange(final double value) {
+ if (Double.isInfinite(value) || Double.isNaN(value)) {
+ throw new NumberFormatException("double can't be infinite or NaN");
+ }
+ }
+
+
+ //unopitimized, see below
+ private void writeLong0(final long i) {
+
+ justWrite(String.valueOf(i));
+ }
+
+ //unopitimized, see below
+ private void writeInt0(final int i) {
+
+ justWrite(String.valueOf(i));
+ }
+
+ //optimized number optimizations
+/*
+ private void writeLong0(final long i) {
+ if (i == Long.MIN_VALUE) {
+ justWrite("-9223372036854775808");
+ return;
+ }
+ final int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
+ final char[] buf = new char[size];
+ getChars(i, size, buf);
+ justWrite(buf);
+ }
+
+ private void writeInt0(final int i) {
+ if (i == Integer.MIN_VALUE) {
+ justWrite("-2147483648");
+ return;
+ }
+ final int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
+ final char[] buf = new char[size];
+ getChars(i, size, buf);
+ justWrite(buf);
+ }
+
+ private final static char[] DIGIT_TENS = { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '1', '1', '1', '1', '1', '1',
+ '1', '1', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '4', '4', '4',
+ '4', '4', '4', '4', '4', '4', '4', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '6', '6', '6', '6', '6', '6', '6', '6',
+ '6', '6', '7', '7', '7', '7', '7', '7', '7', '7', '7', '7', '8', '8', '8', '8', '8', '8', '8', '8', '8', '8', '9', '9', '9',
+ '9', '9', '9', '9', '9', '9', '9', };
+
+ private final static char[] DIGIT_ONES = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7',
+ '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2',
+ '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7',
+ '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2',
+ '3', '4', '5', '6', '7', '8', '9', };
+
+ private final static char[] DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
+ 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
+
+ // Requires positive x
+ private static int stringSize(final long x) {
+ long p = 10;
+ for (int i = 1; i < 19; i++) {
+ if (x < p) {
+ return i;
+ }
+ p = 10 * p;
+ }
+ return 19;
+ }
+
+ private static void getChars(long i, final int index, final char[] buf) {
+ long q;
+ int r;
+ int charPos = index;
+ char sign = 0;
+
+ if (i < 0) {
+ sign = '-';
+ i = -i;
+ }
+
+ // Get 2 digits/iteration using longs until quotient fits into an int
+ while (i > Integer.MAX_VALUE) {
+ q = i / 100;
+ // really: r = i - (q * 100);
+ r = (int) (i - ((q << 6) + (q << 5) + (q << 2)));
+ i = q;
+ buf[--charPos] = DIGIT_ONES[r];
+ buf[--charPos] = DIGIT_TENS[r];
+ }
+
+ // Get 2 digits/iteration using ints
+ int q2;
+ int i2 = (int) i;
+ while (i2 >= 65536) {
+ q2 = i2 / 100;
+ // really: r = i2 - (q * 100);
+ r = i2 - ((q2 << 6) + (q2 << 5) + (q2 << 2));
+ i2 = q2;
+ buf[--charPos] = DIGIT_ONES[r];
+ buf[--charPos] = DIGIT_TENS[r];
+ }
+
+ // Fall thru to fast mode for smaller numbers
+ // assert(i2 <= 65536, i2);
+ for (;;) {
+ q2 = (i2 * 52429) >>> (16 + 3);
+ r = i2 - ((q2 << 3) + (q2 << 1)); // r = i2-(q2*10) ...
+ buf[--charPos] = DIGITS[r];
+ i2 = q2;
+ if (i2 == 0) {
+ break;
+ }
+ }
+ if (sign != 0) {
+ buf[--charPos] = sign;
+ }
+ }
+*/
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-johnzon/blob/6e86a53e/johnzon-core/src/main/java/org/apache/johnzon/core/JsonInMemoryParser.java
----------------------------------------------------------------------
diff --git a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonInMemoryParser.java b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonInMemoryParser.java
new file mode 100644
index 0000000..b3709a8
--- /dev/null
+++ b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonInMemoryParser.java
@@ -0,0 +1,180 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.johnzon.core;
+
+import java.math.BigDecimal;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+
+import javax.json.JsonArray;
+import javax.json.JsonNumber;
+import javax.json.JsonObject;
+import javax.json.JsonString;
+import javax.json.JsonValue;
+import javax.json.stream.JsonLocation;
+import javax.json.stream.JsonParser;
+
+// we don't use visitor pattern to ensure we work with other impl of JsonObject and JsonArray
+class JsonInMemoryParser implements JsonParser {
+ private final Iterator<Entry> iterator;
+
+ private Entry next = null;
+
+ JsonInMemoryParser(final JsonObject object) {
+ final List<Entry> events = new LinkedList<Entry>();
+ generateObjectEvents(events, object);
+ iterator = events.iterator();
+ }
+
+ JsonInMemoryParser(final JsonArray array) {
+ final List<Entry> events = new LinkedList<Entry>();
+ generateArrayEvents(events, array);
+ iterator = events.iterator();
+ }
+
+ private static void generateObjectEvents(final List<Entry> events, final JsonObject object) {
+ events.add(Entry.START_OBJECT_ENTRY);
+ for (final Map.Entry<String, JsonValue> entry : object.entrySet()) {
+ events.add(new Entry(Event.KEY_NAME, new JsonStringImpl(entry.getKey())));
+ final JsonValue value = entry.getValue();
+ addValueEvents(events, value);
+ }
+ events.add(Entry.END_OBJECT_ENTRY);
+ }
+
+ private static void generateArrayEvents(final List<Entry> events, final JsonArray array) {
+ events.add(Entry.START_ARRAY_ENTRY);
+ for (final JsonValue value : array) {
+ addValueEvents(events, value);
+ }
+ events.add(Entry.END_ARRAY_ENTRY);
+ }
+
+ private static void addValueEvents(final List<Entry> events, final JsonValue value) {
+
+ switch(value.getValueType()) {
+ case ARRAY:
+ generateArrayEvents(events, JsonArray.class.cast(value));
+ break;
+ case OBJECT:
+ generateObjectEvents(events, JsonObject.class.cast(value));
+ break;
+ case NUMBER:
+ events.add(new Entry(Event.VALUE_NUMBER, value));
+ break;
+ case STRING:
+ events.add(new Entry(Event.VALUE_STRING, value));
+ break;
+ case FALSE:
+ events.add(Entry.VALUE_FALSE_ENTRY);
+ break;
+ case NULL:
+ events.add(Entry.VALUE_NULL_ENTRY);
+ break;
+ case TRUE:
+ events.add(Entry.VALUE_TRUE_ENTRY);
+ break;
+ default: throw new IllegalArgumentException(value + " not supported");
+
+ }
+
+ }
+
+ @Override
+ public boolean hasNext() {
+ return iterator.hasNext();
+ }
+
+ @Override
+ public Event next() {
+ next = iterator.next();
+ return next.event;
+ }
+
+ @Override
+ public String getString() {
+ if (next.event != Event.KEY_NAME && next.event != Event.VALUE_STRING) {
+ throw new IllegalStateException("String is for numbers and strings");
+ }
+ return JsonString.class.cast(next.value).getString();
+ }
+
+ @Override
+ public boolean isIntegralNumber() {
+ if (next.event != Event.VALUE_NUMBER) {
+ throw new IllegalStateException("isIntegralNumber is for numbers");
+ }
+ return JsonNumber.class.cast(next.value).isIntegral();
+ }
+
+ @Override
+ public int getInt() {
+ if (next.event != Event.VALUE_NUMBER) {
+ throw new IllegalStateException("getInt is for numbers");
+ }
+ return JsonNumber.class.cast(next.value).intValue();
+ }
+
+ @Override
+ public long getLong() {
+ if (next.event != Event.VALUE_NUMBER) {
+ throw new IllegalStateException("getLong is for numbers");
+ }
+ return JsonNumber.class.cast(next.value).longValue();
+ }
+
+ @Override
+ public BigDecimal getBigDecimal() {
+ if (next.event != Event.VALUE_NUMBER) {
+ throw new IllegalStateException("getBigDecimal is for numbers");
+ }
+ return JsonNumber.class.cast(next.value).bigDecimalValue();
+ }
+
+ @Override
+ public JsonLocation getLocation() { // no location for in memory parsers
+ return JsonLocationImpl.UNKNOW_LOCATION;
+ }
+
+ @Override
+ public void close() {
+ // no-op
+ }
+
+ private static class Entry {
+
+ static final Entry VALUE_FALSE_ENTRY = new Entry(Event.VALUE_FALSE, null);
+ static final Entry VALUE_TRUE_ENTRY = new Entry(Event.VALUE_TRUE, null);
+ static final Entry VALUE_NULL_ENTRY = new Entry(Event.VALUE_NULL, null);
+ static final Entry START_OBJECT_ENTRY = new Entry(Event.START_OBJECT, null);
+ static final Entry END_OBJECT_ENTRY = new Entry(Event.END_OBJECT, null);
+ static final Entry START_ARRAY_ENTRY = new Entry(Event.START_ARRAY, null);
+ static final Entry END_ARRAY_ENTRY = new Entry(Event.END_ARRAY, null);
+
+ final Event event;
+ final JsonValue value;
+
+ Entry(final Event event, final JsonValue value) {
+ this.event = event;
+ this.value = value;
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-johnzon/blob/6e86a53e/johnzon-core/src/main/java/org/apache/johnzon/core/JsonLocationImpl.java
----------------------------------------------------------------------
diff --git a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonLocationImpl.java b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonLocationImpl.java
new file mode 100644
index 0000000..0c9e37b
--- /dev/null
+++ b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonLocationImpl.java
@@ -0,0 +1,80 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.johnzon.core;
+
+import java.io.Serializable;
+
+import javax.json.stream.JsonLocation;
+
+final class JsonLocationImpl implements JsonLocation, Serializable {
+
+ public static final JsonLocation UNKNOW_LOCATION = new JsonLocationImpl(-1, -1, -1);
+
+ private final long lineNumber;
+ private final long columnNumber;
+ private final long streamOffset;
+
+ JsonLocationImpl(final long lineNumber, final long columnNumber, final long streamOffset) {
+ this.lineNumber = lineNumber;
+ this.columnNumber = columnNumber;
+ this.streamOffset = streamOffset;
+ }
+
+ @Override
+ public long getLineNumber() {
+ return lineNumber;
+ }
+
+ @Override
+ public long getColumnNumber() {
+ return columnNumber;
+ }
+
+ @Override
+ public long getStreamOffset() {
+ return streamOffset;
+ }
+
+ @Override
+ public boolean equals(final Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+
+ final JsonLocationImpl that = JsonLocationImpl.class.cast(o);
+ return columnNumber == that.columnNumber && lineNumber == that.lineNumber && streamOffset == that.streamOffset;
+
+ }
+
+ @Override
+ public int hashCode() {
+ int result = (int) (lineNumber ^ (lineNumber >>> 32));
+ result = 31 * result + (int) (columnNumber ^ (columnNumber >>> 32));
+ result = 31 * result + (int) (streamOffset ^ (streamOffset >>> 32));
+ return result;
+ }
+
+ @Override
+ public String toString() {
+ return "[lineNumber=" + lineNumber + ", columnNumber=" + columnNumber + ", streamOffset=" + streamOffset + "]";
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-johnzon/blob/6e86a53e/johnzon-core/src/main/java/org/apache/johnzon/core/JsonLongImpl.java
----------------------------------------------------------------------
diff --git a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonLongImpl.java b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonLongImpl.java
new file mode 100644
index 0000000..2f013f7
--- /dev/null
+++ b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonLongImpl.java
@@ -0,0 +1,97 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.johnzon.core;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+
+import javax.json.JsonNumber;
+
+final class JsonLongImpl implements JsonNumber {
+ private final long value;
+
+ JsonLongImpl(final long value) {
+ this.value = value;
+ }
+
+ @Override
+ public boolean isIntegral() {
+ return true;
+ }
+
+ @Override
+ public int intValue() {
+ return (int) value;
+ }
+
+ @Override
+ public int intValueExact() {
+ return intValue();
+ }
+
+ @Override
+ public long longValue() {
+ return value;
+ }
+
+ @Override
+ public long longValueExact() {
+ return value;
+ }
+
+ @Override
+ public BigInteger bigIntegerValue() {
+ return new BigInteger(toString());
+ }
+
+ @Override
+ public BigInteger bigIntegerValueExact() {
+ return bigIntegerValue();
+ }
+
+ @Override
+ public double doubleValue() {
+ return value;
+ }
+
+ @Override
+ public BigDecimal bigDecimalValue() {
+ return new BigDecimal(toString());
+ }
+
+ @Override
+ public ValueType getValueType() {
+ return ValueType.NUMBER;
+ }
+
+ @Override
+ public String toString() {
+ return Long.toString(value);
+ }
+
+ @Override
+ public int hashCode() {
+ return (int) value;
+ }
+
+ @Override
+ public boolean equals(final Object obj) {
+ return JsonNumber.class.isInstance(obj) && JsonNumber.class.cast(obj).longValue() == value;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-johnzon/blob/6e86a53e/johnzon-core/src/main/java/org/apache/johnzon/core/JsonNumberImpl.java
----------------------------------------------------------------------
diff --git a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonNumberImpl.java b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonNumberImpl.java
new file mode 100644
index 0000000..8b560ed
--- /dev/null
+++ b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonNumberImpl.java
@@ -0,0 +1,107 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.johnzon.core;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+
+import javax.json.JsonNumber;
+
+final class JsonNumberImpl implements JsonNumber {
+ private final BigDecimal value;
+ private Integer hashCode = null;
+
+ JsonNumberImpl(final BigDecimal decimal) {
+ if(decimal == null) {
+ throw new NullPointerException("decimal must not be null");
+ }
+
+ this.value = decimal;
+ }
+
+ @Override
+ public boolean isIntegral() {
+ return value.scale() == 0;
+ }
+
+ @Override
+ public int intValue() {
+ return value.intValue();
+ }
+
+ @Override
+ public int intValueExact() {
+ return value.intValueExact();
+ }
+
+ @Override
+ public long longValue() {
+ return value.longValue();
+ }
+
+ @Override
+ public long longValueExact() {
+ return value.longValueExact();
+ }
+
+ @Override
+ public BigInteger bigIntegerValue() {
+ return value.toBigInteger();
+ }
+
+ @Override
+ public BigInteger bigIntegerValueExact() {
+ return value.toBigIntegerExact();
+ }
+
+ @Override
+ public double doubleValue() {
+ return value.doubleValue();
+ }
+
+ @Override
+ public BigDecimal bigDecimalValue() {
+ return value;
+ }
+
+ @Override
+ public ValueType getValueType() {
+ return ValueType.NUMBER;
+ }
+
+ @Override
+ public String toString() {
+ return value.toString();
+ }
+
+ @Override
+ public int hashCode() {
+ Integer h=hashCode;
+ if (h == null) {
+ h = value.hashCode();
+ hashCode=h;
+ }
+ return h;
+ }
+
+ @Override
+ public boolean equals(final Object obj) {
+ return JsonNumber.class.isInstance(obj) && JsonNumber.class.cast(obj).bigDecimalValue().equals(value);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-johnzon/blob/6e86a53e/johnzon-core/src/main/java/org/apache/johnzon/core/JsonObjectBuilderImpl.java
----------------------------------------------------------------------
diff --git a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonObjectBuilderImpl.java b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonObjectBuilderImpl.java
new file mode 100644
index 0000000..ea15a17
--- /dev/null
+++ b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonObjectBuilderImpl.java
@@ -0,0 +1,131 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.johnzon.core;
+
+import java.io.Serializable;
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+import javax.json.JsonArrayBuilder;
+import javax.json.JsonObject;
+import javax.json.JsonObjectBuilder;
+import javax.json.JsonValue;
+
+class JsonObjectBuilderImpl implements JsonObjectBuilder, Serializable {
+ private Map<String, JsonValue> tmpMap;
+
+ @Override
+ public JsonObjectBuilder add(final String name, final JsonValue value) {
+ putValue(name, value);
+ return this;
+ }
+
+ @Override
+ public JsonObjectBuilder add(final String name, final String value) {
+ putValue(name, new JsonStringImpl(value));
+ return this;
+ }
+
+ @Override
+ public JsonObjectBuilder add(final String name, final BigInteger value) {
+ putValue(name, new JsonNumberImpl(new BigDecimal(value)));
+ return this;
+ }
+
+ @Override
+ public JsonObjectBuilder add(final String name, final BigDecimal value) {
+ putValue(name, new JsonNumberImpl(value));
+ return this;
+ }
+
+ @Override
+ public JsonObjectBuilder add(final String name, final int value) {
+ putValue(name, new JsonLongImpl(value));
+ return this;
+ }
+
+ @Override
+ public JsonObjectBuilder add(final String name, final long value) {
+ putValue(name, new JsonLongImpl(value));
+ return this;
+ }
+
+ @Override
+ public JsonObjectBuilder add(final String name, final double value) {
+ putValue(name, new JsonDoubleImpl(value));
+ return this;
+ }
+
+ @Override
+ public JsonObjectBuilder add(final String name, final boolean value) {
+ putValue(name, value ? JsonValue.TRUE : JsonValue.FALSE);
+ return this;
+ }
+
+ @Override
+ public JsonObjectBuilder addNull(final String name) {
+ putValue(name, JsonValue.NULL);
+ return this;
+ }
+
+ @Override
+ public JsonObjectBuilder add(final String name, final JsonObjectBuilder builder) {
+ putValue(name, builder.build());
+ return this;
+ }
+
+ @Override
+ public JsonObjectBuilder add(final String name, final JsonArrayBuilder builder) {
+ putValue(name, builder.build());
+ return this;
+ }
+
+ private void putValue(String name, JsonValue value){
+ if(name == null || value == null) {
+ throw npe();
+ }
+
+ if(tmpMap==null){
+ tmpMap=new LinkedHashMap<String, JsonValue>();
+ }
+
+ tmpMap.put(name, value);
+ }
+
+ private static NullPointerException npe() {
+ return new NullPointerException("name or value/builder must not be null");
+ }
+
+ @Override
+ public JsonObject build() {
+
+ if(tmpMap==null) {
+ return new JsonObjectImpl(Collections.EMPTY_MAP);
+ } else {
+ Map<String, JsonValue> dump = (Collections.unmodifiableMap(tmpMap));
+ tmpMap=null;
+ return new JsonObjectImpl(dump);
+ }
+
+
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-johnzon/blob/6e86a53e/johnzon-core/src/main/java/org/apache/johnzon/core/JsonObjectImpl.java
----------------------------------------------------------------------
diff --git a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonObjectImpl.java b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonObjectImpl.java
new file mode 100644
index 0000000..863f173
--- /dev/null
+++ b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonObjectImpl.java
@@ -0,0 +1,167 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.johnzon.core;
+
+import java.io.Serializable;
+import java.util.AbstractMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
+
+import javax.json.JsonArray;
+import javax.json.JsonNumber;
+import javax.json.JsonObject;
+import javax.json.JsonString;
+import javax.json.JsonValue;
+
+public final class JsonObjectImpl extends AbstractMap<String, JsonValue> implements JsonObject, Serializable {
+ private Integer hashCode = null;
+ private final Map<String, JsonValue> unmodifieableBackingMap;
+
+ private <T> T value(final String name, final Class<T> clazz) {
+ final Object v = unmodifieableBackingMap.get(name);
+ if (v != null) {
+ return clazz.cast(v);
+ }
+ throw new NullPointerException("no mapping for " + name);
+ }
+
+ public JsonObjectImpl(Map<String, JsonValue> backingMap) {
+ super();
+ this.unmodifieableBackingMap = backingMap;
+ }
+
+
+ @Override
+ public JsonArray getJsonArray(final String name) {
+ return value(name, JsonArray.class);
+ }
+
+ @Override
+ public JsonObject getJsonObject(final String name) {
+ return value(name, JsonObject.class);
+ }
+
+ @Override
+ public JsonNumber getJsonNumber(final String name) {
+ return value(name, JsonNumber.class);
+ }
+
+ @Override
+ public JsonString getJsonString(final String name) {
+ return value(name, JsonString.class);
+ }
+
+ @Override
+ public String getString(final String name) {
+ final JsonString str = getJsonString(name);
+ return str != null ? str.getString() : null;
+ }
+
+ @Override
+ public String getString(final String name, final String defaultValue) {
+ try {
+ return getJsonString(name).getString();
+ } catch (final NullPointerException npe) {
+ return defaultValue;
+ }
+ }
+
+ @Override
+ public int getInt(final String name) {
+ return getJsonNumber(name).intValue();
+ }
+
+ @Override
+ public int getInt(final String name, final int defaultValue) {
+ try {
+ return getJsonNumber(name).intValue();
+ } catch (final NullPointerException npe) {
+ return defaultValue;
+ }
+ }
+
+ @Override
+ public boolean getBoolean(final String name) {
+ return value(name, JsonValue.class) == JsonValue.TRUE;
+ }
+
+ @Override
+ public boolean getBoolean(final String name, final boolean defaultValue) {
+ try {
+ return getBoolean(name);
+ } catch (final NullPointerException npe) {
+ return defaultValue;
+ }
+ }
+
+ @Override
+ public boolean isNull(final String name) {
+ return value(name, JsonValue.class) == JsonValue.NULL;
+ }
+
+ @Override
+ public ValueType getValueType() {
+ return ValueType.OBJECT;
+ }
+
+ @Override
+ public String toString() {
+ final StringBuilder builder = new StringBuilder("{");
+ final Iterator<Map.Entry<String, JsonValue>> it = unmodifieableBackingMap.entrySet().iterator();
+ boolean hasNext = it.hasNext();
+ while (hasNext) {
+ final Map.Entry<String, JsonValue> entry = it.next();
+
+ builder.append('"').append(entry.getKey()).append("\":");
+
+ final JsonValue value = entry.getValue();
+ if (JsonString.class.isInstance(value)) {
+ builder.append(value.toString());
+ } else {
+ builder.append(value != JsonValue.NULL ? value.toString() : JsonChars.NULL);
+ }
+
+ hasNext = it.hasNext();
+ if (hasNext) {
+ builder.append(",");
+ }
+ }
+ return builder.append('}').toString();
+ }
+
+ @Override
+ public boolean equals(final Object obj) {
+ return JsonObjectImpl.class.isInstance(obj) && unmodifieableBackingMap.equals(JsonObjectImpl.class.cast(obj).unmodifieableBackingMap);
+ }
+
+
+ @Override
+ public int hashCode() {
+ if (hashCode == null) {
+ hashCode = unmodifieableBackingMap.hashCode();
+ }
+ return hashCode;
+ }
+
+ @Override
+ public Set<java.util.Map.Entry<String, JsonValue>> entrySet() {
+ return unmodifieableBackingMap.entrySet();
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-johnzon/blob/6e86a53e/johnzon-core/src/main/java/org/apache/johnzon/core/JsonParserFactoryImpl.java
----------------------------------------------------------------------
diff --git a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonParserFactoryImpl.java b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonParserFactoryImpl.java
new file mode 100644
index 0000000..85d09d2
--- /dev/null
+++ b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonParserFactoryImpl.java
@@ -0,0 +1,149 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.johnzon.core;
+
+import java.io.InputStream;
+import java.io.Reader;
+import java.io.Serializable;
+import java.nio.charset.Charset;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Locale;
+import java.util.Map;
+
+import javax.json.JsonArray;
+import javax.json.JsonObject;
+import javax.json.stream.JsonParser;
+import javax.json.stream.JsonParserFactory;
+
+class JsonParserFactoryImpl implements JsonParserFactory, Serializable {
+ public static final String BUFFER_STRATEGY = "org.apache.johnzon.buffer-strategy";
+ public static final String MAX_STRING_LENGTH = "org.apache.johnzon.max-string-length";
+ public static final String BUFFER_LENGTH = "org.apache.johnzon.default-char-buffer";
+ public static final int DEFAULT_MAX_SIZE = Integer.getInteger(MAX_STRING_LENGTH, 8192*32); //TODO check default string length/buffer size
+
+ private final Map<String, Object> internalConfig = new HashMap<String, Object>();
+ private static final String[] SUPPORTED_CONFIG_KEYS = new String[] {
+
+ BUFFER_STRATEGY, MAX_STRING_LENGTH, BUFFER_LENGTH
+
+ };
+
+ private final int maxSize;
+ private final BufferStrategy.BufferProvider<char[]> bufferProvider;
+ private final BufferStrategy.BufferProvider<char[]> valueBufferProvider;
+
+ JsonParserFactoryImpl(final Map<String, ?> config) {
+
+
+ if(config != null) {
+
+ for (String configKey : SUPPORTED_CONFIG_KEYS) {
+ if(config.containsKey(configKey)) {
+ internalConfig.put(configKey, config.get(configKey));
+ }
+ }
+ }
+
+
+ final int bufferSize = getInt(BUFFER_LENGTH);
+ if (bufferSize <= 0) {
+ throw new IllegalArgumentException("buffer length must be greater than zero");
+ }
+
+ this.maxSize = getInt(MAX_STRING_LENGTH);
+ this.bufferProvider = getBufferProvider().newCharProvider(bufferSize);
+ this.valueBufferProvider = getBufferProvider().newCharProvider(maxSize);
+ }
+
+ private BufferStrategy getBufferProvider() {
+ final Object name = internalConfig.get(BUFFER_STRATEGY);
+ if (name != null) {
+ return BufferStrategy.valueOf(name.toString().toUpperCase(Locale.ENGLISH));
+ }
+ return BufferStrategy.QUEUE;
+ }
+
+ private int getInt(final String key) {
+ final Object maxStringSize = internalConfig.get(key);
+ if (maxStringSize == null) {
+ return DEFAULT_MAX_SIZE;
+ } else if (Number.class.isInstance(maxStringSize)) {
+ return Number.class.cast(maxStringSize).intValue();
+ }
+ return Integer.parseInt(maxStringSize.toString());
+ }
+
+ private JsonParser getDefaultJsonParserImpl(final InputStream in) {
+ //UTF Auto detection RFC 4627
+ return new JsonStreamParserImpl(in, maxSize, bufferProvider, valueBufferProvider);
+ }
+
+ private JsonParser getDefaultJsonParserImpl(final InputStream in, final Charset charset) {
+ //use provided charset
+ return new JsonStreamParserImpl(in, charset, maxSize, bufferProvider, valueBufferProvider);
+ }
+
+ private JsonParser getDefaultJsonParserImpl(final Reader in) {
+ //no charset necessary
+ return new JsonStreamParserImpl(in, maxSize, bufferProvider, valueBufferProvider);
+ }
+
+ @Override
+ public JsonParser createParser(final Reader reader) {
+ return getDefaultJsonParserImpl(reader);
+ }
+
+ @Override
+ public JsonParser createParser(final InputStream in) {
+ return getDefaultJsonParserImpl(in);
+ }
+
+ @Override
+ public JsonParser createParser(final InputStream in, final Charset charset) {
+ return getDefaultJsonParserImpl(in, charset);
+ }
+
+ @Override
+ public JsonParser createParser(final JsonObject obj) {
+ return new JsonInMemoryParser(obj);
+ }
+
+ @Override
+ public JsonParser createParser(final JsonArray array) {
+ return new JsonInMemoryParser(array);
+ }
+
+ @Override
+ public Map<String, ?> getConfigInUse() {
+ return Collections.unmodifiableMap(internalConfig);
+ }
+
+ public JsonParser createInternalParser(final InputStream in) {
+ return getDefaultJsonParserImpl(in);
+ }
+
+ public JsonParser createInternalParser(final InputStream in, final Charset charset) {
+ return getDefaultJsonParserImpl(in, charset);
+ }
+
+ public JsonParser createInternalParser(final Reader reader) {
+ return getDefaultJsonParserImpl(reader);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-johnzon/blob/6e86a53e/johnzon-core/src/main/java/org/apache/johnzon/core/JsonPrettyGeneratorImpl.java
----------------------------------------------------------------------
diff --git a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonPrettyGeneratorImpl.java b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonPrettyGeneratorImpl.java
new file mode 100644
index 0000000..9ec038e
--- /dev/null
+++ b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonPrettyGeneratorImpl.java
@@ -0,0 +1,305 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.johnzon.core;
+
+import java.io.OutputStream;
+import java.io.Writer;
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.nio.charset.Charset;
+import java.util.concurrent.ConcurrentMap;
+
+import javax.json.JsonValue;
+import javax.json.stream.JsonGenerator;
+
+final class JsonPrettyGeneratorImpl extends JsonGeneratorImpl {
+ private static final String DEFAULT_INDENTATION = " ";
+ private final String indent;
+
+ public JsonPrettyGeneratorImpl(final Writer writer, final BufferStrategy.BufferProvider<char[]> bufferProvider,
+ final ConcurrentMap<String, String> cache) {
+ super(writer, bufferProvider, cache);
+ indent = DEFAULT_INDENTATION;
+ }
+
+ public JsonPrettyGeneratorImpl(final OutputStream out, final Charset encoding,
+ final BufferStrategy.BufferProvider<char[]> bufferProvider, final ConcurrentMap<String, String> cache) {
+ super(out, encoding, bufferProvider, cache);
+ indent = DEFAULT_INDENTATION;
+ }
+
+ public JsonPrettyGeneratorImpl(final OutputStream out, final BufferStrategy.BufferProvider<char[]> bufferProvider,
+ final ConcurrentMap<String, String> cache) {
+ super(out, bufferProvider, cache);
+ indent = DEFAULT_INDENTATION;
+ }
+
+ private void writeEOL() {
+ justWrite(EOL);
+ }
+
+ private void writeIndent(final int correctionOffset) {
+ for (int i = 0; i < depth + correctionOffset; i++) {
+ justWrite(indent);
+ }
+ }
+
+ @Override
+ protected void addCommaIfNeeded() {
+ if (needComma) {
+ justWrite(COMMA_CHAR);
+ writeEOL();
+ writeIndent(0);
+ needComma = false;
+ }
+
+ }
+
+ @Override
+ public JsonGenerator writeStartObject() {
+ if (depth > 0 && !needComma) {
+ writeEOL();
+ writeIndent(0);
+ }
+ return super.writeStartObject();
+
+ }
+
+ @Override
+ public JsonGenerator writeStartObject(final String name) {
+ if (!needComma) {
+ writeEOL();
+ writeIndent(0);
+ }
+ return super.writeStartObject(name);
+
+ }
+
+ @Override
+ public JsonGenerator writeStartArray() {
+ if (depth > 0 && !needComma) {
+ writeEOL();
+ writeIndent(0);
+ }
+ return super.writeStartArray();
+
+ }
+
+ @Override
+ public JsonGenerator writeStartArray(final String name) {
+ if (!needComma) {
+ writeEOL();
+ writeIndent(0);
+ }
+ return super.writeStartArray(name);
+
+ }
+
+ //end
+
+ @Override
+ public JsonGenerator writeEnd() {
+ writeEOL();
+ writeIndent(-1);
+ return super.writeEnd();
+
+ }
+
+ //normal
+
+ @Override
+ public JsonGenerator write(final String name, final JsonValue value) {
+ if (!needComma) {
+ writeEOL();
+ writeIndent(0);
+ }
+
+ return super.write(name, value);
+
+ }
+
+ @Override
+ public JsonGenerator write(final String name, final String value) {
+ if (!needComma) {
+ writeEOL();
+ writeIndent(0);
+ }
+ return super.write(name, value);
+ }
+
+ @Override
+ public JsonGenerator write(final String name, final BigInteger value) {
+ if (!needComma) {
+ writeEOL();
+ writeIndent(0);
+ }
+ return super.write(name, value);
+
+ }
+
+ @Override
+ public JsonGenerator write(final String name, final BigDecimal value) {
+ if (!needComma) {
+ writeEOL();
+ writeIndent(0);
+ }
+ return super.write(name, value);
+
+ }
+
+ @Override
+ public JsonGenerator write(final String name, final int value) {
+ if (!needComma) {
+ writeEOL();
+ writeIndent(0);
+ }
+ return super.write(name, value);
+
+ }
+
+ @Override
+ public JsonGenerator write(final String name, final long value) {
+ if (!needComma) {
+ writeEOL();
+ writeIndent(0);
+ }
+ return super.write(name, value);
+
+ }
+
+ @Override
+ public JsonGenerator write(final String name, final double value) {
+ if (!needComma) {
+ writeEOL();
+ writeIndent(0);
+ }
+ return super.write(name, value);
+
+ }
+
+ @Override
+ public JsonGenerator write(final String name, final boolean value) {
+ if (!needComma) {
+ writeEOL();
+ writeIndent(0);
+ }
+ return super.write(name, value);
+
+ }
+
+ @Override
+ public JsonGenerator writeNull(final String name) {
+ if (!needComma) {
+ writeEOL();
+ writeIndent(0);
+ }
+ return super.writeNull(name);
+
+ }
+
+ @Override
+ public JsonGenerator write(final JsonValue value) {
+ if (!needComma) {
+ writeEOL();
+ writeIndent(0);
+ }
+ return super.write(value);
+
+ }
+
+ @Override
+ public JsonGenerator write(final String value) {
+ if (!needComma) {
+ writeEOL();
+ writeIndent(0);
+ }
+ return super.write(value);
+
+ }
+
+ @Override
+ public JsonGenerator write(final BigDecimal value) {
+ if (!needComma) {
+ writeEOL();
+ writeIndent(0);
+ }
+ return super.write(value);
+
+ }
+
+ @Override
+ public JsonGenerator write(final BigInteger value) {
+ if (!needComma) {
+ writeEOL();
+ writeIndent(0);
+ }
+ return super.write(value);
+
+ }
+
+ @Override
+ public JsonGenerator write(final int value) {
+ if (!needComma) {
+ writeEOL();
+ writeIndent(0);
+ }
+ return super.write(value);
+
+ }
+
+ @Override
+ public JsonGenerator write(final long value) {
+ if (!needComma) {
+ writeEOL();
+ writeIndent(0);
+ }
+ return super.write(value);
+
+ }
+
+ @Override
+ public JsonGenerator write(final double value) {
+ if (!needComma) {
+ writeEOL();
+ writeIndent(0);
+ }
+ return super.write(value);
+
+ }
+
+ @Override
+ public JsonGenerator write(final boolean value) {
+ if (!needComma) {
+ writeEOL();
+ writeIndent(0);
+ }
+ return super.write(value);
+
+ }
+
+ @Override
+ public JsonGenerator writeNull() {
+ if (!needComma) {
+ writeEOL();
+ writeIndent(0);
+ }
+ return super.writeNull();
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-johnzon/blob/6e86a53e/johnzon-core/src/main/java/org/apache/johnzon/core/JsonProviderImpl.java
----------------------------------------------------------------------
diff --git a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonProviderImpl.java b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonProviderImpl.java
new file mode 100644
index 0000000..273d0db
--- /dev/null
+++ b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonProviderImpl.java
@@ -0,0 +1,205 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.johnzon.core;
+
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.Reader;
+import java.io.Serializable;
+import java.io.Writer;
+import java.nio.charset.Charset;
+import java.util.Collections;
+import java.util.Map;
+
+import javax.json.JsonArrayBuilder;
+import javax.json.JsonBuilderFactory;
+import javax.json.JsonObjectBuilder;
+import javax.json.JsonReader;
+import javax.json.JsonReaderFactory;
+import javax.json.JsonWriter;
+import javax.json.JsonWriterFactory;
+import javax.json.spi.JsonProvider;
+import javax.json.stream.JsonGenerator;
+import javax.json.stream.JsonGeneratorFactory;
+import javax.json.stream.JsonParser;
+import javax.json.stream.JsonParserFactory;
+
+public class JsonProviderImpl extends JsonProvider implements Serializable {
+ private static final JsonProvider DELEGATE = new JsonProviderDelegate();
+
+
+ @Override
+ public JsonParser createParser(final Reader reader) {
+ return DELEGATE.createParser(reader);
+ }
+
+ @Override
+ public JsonParser createParser(final InputStream inputStream) {
+ return DELEGATE.createParser(inputStream);
+ }
+
+ @Override
+ public JsonParserFactory createParserFactory(final Map<String, ?> stringMap) {
+ return DELEGATE.createParserFactory(stringMap);
+ }
+
+ @Override
+ public JsonGenerator createGenerator(final Writer writer) {
+ return DELEGATE.createGenerator(writer);
+ }
+
+ @Override
+ public JsonGenerator createGenerator(final OutputStream outputStream) {
+ return DELEGATE.createGenerator(outputStream);
+ }
+
+ @Override
+ public JsonGeneratorFactory createGeneratorFactory(final Map<String, ?> stringMap) {
+ return DELEGATE.createGeneratorFactory(stringMap);
+ }
+
+ @Override
+ public JsonReader createReader(final Reader reader) {
+ return DELEGATE.createReader(reader);
+ }
+
+ @Override
+ public JsonReader createReader(final InputStream inputStream) {
+ return DELEGATE.createReader(inputStream);
+ }
+
+ @Override
+ public JsonWriter createWriter(final Writer writer) {
+ return DELEGATE.createWriter(writer);
+ }
+
+ @Override
+ public JsonWriter createWriter(final OutputStream outputStream) {
+ return DELEGATE.createWriter(outputStream);
+ }
+
+ @Override
+ public JsonWriterFactory createWriterFactory(final Map<String, ?> stringMap) {
+ return DELEGATE.createWriterFactory(stringMap);
+ }
+
+ @Override
+ public JsonReaderFactory createReaderFactory(final Map<String, ?> stringMap) {
+ return DELEGATE.createReaderFactory(stringMap);
+ }
+
+ @Override
+ public JsonObjectBuilder createObjectBuilder() {
+ return DELEGATE.createObjectBuilder();
+ }
+
+ @Override
+ public JsonArrayBuilder createArrayBuilder() {
+ return DELEGATE.createArrayBuilder();
+ }
+
+ @Override
+ public JsonBuilderFactory createBuilderFactory(Map<String, ?> stringMap) {
+ return DELEGATE.createBuilderFactory(stringMap);
+ }
+
+ static class JsonProviderDelegate extends JsonProvider {
+ private final JsonReaderFactory readerFactory = new JsonReaderFactoryImpl(Collections.<String, Object>emptyMap());
+ private final JsonParserFactory parserFactory = new JsonParserFactoryImpl(Collections.<String, Object>emptyMap());
+ private final JsonGeneratorFactory generatorFactory = new JsonGeneratorFactoryImpl(Collections.<String, Object>emptyMap());
+ private static final Charset UTF8_CHARSET = Charset.forName("UTF-8");
+
+ @Override
+ public JsonParser createParser(final InputStream in) {
+ return parserFactory.createParser(in);
+ }
+
+ @Override
+ public JsonParser createParser(final Reader reader) {
+ return parserFactory.createParser(reader);
+ }
+
+ @Override
+ public JsonReader createReader(final InputStream in) {
+ return readerFactory.createReader(in);
+ }
+
+ @Override
+ public JsonReader createReader(final Reader reader) {
+ return readerFactory.createReader(reader);
+ }
+
+ @Override
+ public JsonParserFactory createParserFactory(final Map<String, ?> config) {
+ return new JsonParserFactoryImpl(config);
+ }
+
+ @Override
+ public JsonReaderFactory createReaderFactory(final Map<String, ?> config) {
+ return new JsonReaderFactoryImpl(config);
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public JsonGenerator createGenerator(final Writer writer) {
+ return generatorFactory.createGenerator(writer);
+ }
+
+ @Override
+ public JsonGenerator createGenerator(final OutputStream out) {
+ return generatorFactory.createGenerator(out);
+ }
+
+ @Override
+ public JsonGeneratorFactory createGeneratorFactory(final Map<String, ?> config) {
+ return new JsonGeneratorFactoryImpl(config);
+ }
+
+ @Override
+ public JsonWriter createWriter(final Writer writer) {
+ return new JsonWriterImpl(createGenerator(writer));
+ }
+
+ @Override
+ public JsonWriter createWriter(final OutputStream out) {
+ return createWriter(new OutputStreamWriter(out, UTF8_CHARSET));
+ }
+
+ @Override
+ public JsonWriterFactory createWriterFactory(final Map<String, ?> config) {
+ return new JsonWriterFactoryImpl(config);
+ }
+
+ @Override
+ public JsonObjectBuilder createObjectBuilder() {
+ return new JsonObjectBuilderImpl();
+ }
+
+ @Override
+ public JsonArrayBuilder createArrayBuilder() {
+ return new JsonArrayBuilderImpl();
+ }
+
+ @Override
+ public JsonBuilderFactory createBuilderFactory(final Map<String, ?> config) {
+ return new JsonBuilderFactoryImpl(config);
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-johnzon/blob/6e86a53e/johnzon-core/src/main/java/org/apache/johnzon/core/JsonReaderFactoryImpl.java
----------------------------------------------------------------------
diff --git a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonReaderFactoryImpl.java b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonReaderFactoryImpl.java
new file mode 100644
index 0000000..65549ac
--- /dev/null
+++ b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonReaderFactoryImpl.java
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.johnzon.core;
+
+import java.io.InputStream;
+import java.io.Reader;
+import java.io.Serializable;
+import java.nio.charset.Charset;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.json.JsonReader;
+import javax.json.JsonReaderFactory;
+
+class JsonReaderFactoryImpl implements JsonReaderFactory, Serializable {
+ private final Map<String, Object> internalConfig = new HashMap<String, Object>();
+ private static final String[] SUPPORTED_CONFIG_KEYS = new String[] {
+ JsonParserFactoryImpl.BUFFER_STRATEGY, JsonParserFactoryImpl.MAX_STRING_LENGTH, JsonParserFactoryImpl.BUFFER_LENGTH
+ };
+ private final JsonParserFactoryImpl parserFactory;
+
+ JsonReaderFactoryImpl(final Map<String, ?> config) {
+
+ if(config != null) {
+
+ for (String configKey : SUPPORTED_CONFIG_KEYS) {
+ if(config.containsKey(configKey)) {
+ internalConfig.put(configKey, config.get(configKey));
+ }
+ }
+ }
+
+ this.parserFactory = new JsonParserFactoryImpl(internalConfig);
+ }
+
+ @Override
+ public JsonReader createReader(final Reader reader) {
+ return new JsonReaderImpl(parserFactory.createInternalParser(reader));
+ }
+
+ @Override
+ public JsonReader createReader(final InputStream in) {
+ return new JsonReaderImpl(parserFactory.createInternalParser(in));
+ }
+
+ @Override
+ public JsonReader createReader(final InputStream in, final Charset charset) {
+ return new JsonReaderImpl(parserFactory.createInternalParser(in, charset));
+ }
+
+ @Override
+ public Map<String, ?> getConfigInUse() {
+ return Collections.unmodifiableMap(internalConfig);
+ }
+}