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/01 18:50:43 UTC
[19/20] renamed fleece to johnzon
http://git-wip-us.apache.org/repos/asf/incubator-fleece/blob/6e86a53e/fleece-core/src/main/java/org/apache/fleece/core/JsonInMemoryParser.java
----------------------------------------------------------------------
diff --git a/fleece-core/src/main/java/org/apache/fleece/core/JsonInMemoryParser.java b/fleece-core/src/main/java/org/apache/fleece/core/JsonInMemoryParser.java
deleted file mode 100644
index 038f3ae..0000000
--- a/fleece-core/src/main/java/org/apache/fleece/core/JsonInMemoryParser.java
+++ /dev/null
@@ -1,180 +0,0 @@
-/*
- * 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.fleece.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-fleece/blob/6e86a53e/fleece-core/src/main/java/org/apache/fleece/core/JsonLocationImpl.java
----------------------------------------------------------------------
diff --git a/fleece-core/src/main/java/org/apache/fleece/core/JsonLocationImpl.java b/fleece-core/src/main/java/org/apache/fleece/core/JsonLocationImpl.java
deleted file mode 100644
index 5c36d58..0000000
--- a/fleece-core/src/main/java/org/apache/fleece/core/JsonLocationImpl.java
+++ /dev/null
@@ -1,80 +0,0 @@
-/*
- * 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.fleece.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-fleece/blob/6e86a53e/fleece-core/src/main/java/org/apache/fleece/core/JsonLongImpl.java
----------------------------------------------------------------------
diff --git a/fleece-core/src/main/java/org/apache/fleece/core/JsonLongImpl.java b/fleece-core/src/main/java/org/apache/fleece/core/JsonLongImpl.java
deleted file mode 100644
index 9928a42..0000000
--- a/fleece-core/src/main/java/org/apache/fleece/core/JsonLongImpl.java
+++ /dev/null
@@ -1,97 +0,0 @@
-/*
- * 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.fleece.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-fleece/blob/6e86a53e/fleece-core/src/main/java/org/apache/fleece/core/JsonNumberImpl.java
----------------------------------------------------------------------
diff --git a/fleece-core/src/main/java/org/apache/fleece/core/JsonNumberImpl.java b/fleece-core/src/main/java/org/apache/fleece/core/JsonNumberImpl.java
deleted file mode 100644
index f1b388a..0000000
--- a/fleece-core/src/main/java/org/apache/fleece/core/JsonNumberImpl.java
+++ /dev/null
@@ -1,107 +0,0 @@
-/*
- * 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.fleece.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-fleece/blob/6e86a53e/fleece-core/src/main/java/org/apache/fleece/core/JsonObjectBuilderImpl.java
----------------------------------------------------------------------
diff --git a/fleece-core/src/main/java/org/apache/fleece/core/JsonObjectBuilderImpl.java b/fleece-core/src/main/java/org/apache/fleece/core/JsonObjectBuilderImpl.java
deleted file mode 100644
index a61c9a3..0000000
--- a/fleece-core/src/main/java/org/apache/fleece/core/JsonObjectBuilderImpl.java
+++ /dev/null
@@ -1,131 +0,0 @@
-/*
- * 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.fleece.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-fleece/blob/6e86a53e/fleece-core/src/main/java/org/apache/fleece/core/JsonObjectImpl.java
----------------------------------------------------------------------
diff --git a/fleece-core/src/main/java/org/apache/fleece/core/JsonObjectImpl.java b/fleece-core/src/main/java/org/apache/fleece/core/JsonObjectImpl.java
deleted file mode 100644
index a71e42c..0000000
--- a/fleece-core/src/main/java/org/apache/fleece/core/JsonObjectImpl.java
+++ /dev/null
@@ -1,167 +0,0 @@
-/*
- * 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.fleece.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-fleece/blob/6e86a53e/fleece-core/src/main/java/org/apache/fleece/core/JsonParserFactoryImpl.java
----------------------------------------------------------------------
diff --git a/fleece-core/src/main/java/org/apache/fleece/core/JsonParserFactoryImpl.java b/fleece-core/src/main/java/org/apache/fleece/core/JsonParserFactoryImpl.java
deleted file mode 100644
index f43ceee..0000000
--- a/fleece-core/src/main/java/org/apache/fleece/core/JsonParserFactoryImpl.java
+++ /dev/null
@@ -1,149 +0,0 @@
-/*
- * 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.fleece.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.fleece.buffer-strategy";
- public static final String MAX_STRING_LENGTH = "org.apache.fleece.max-string-length";
- public static final String BUFFER_LENGTH = "org.apache.fleece.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-fleece/blob/6e86a53e/fleece-core/src/main/java/org/apache/fleece/core/JsonPrettyGeneratorImpl.java
----------------------------------------------------------------------
diff --git a/fleece-core/src/main/java/org/apache/fleece/core/JsonPrettyGeneratorImpl.java b/fleece-core/src/main/java/org/apache/fleece/core/JsonPrettyGeneratorImpl.java
deleted file mode 100644
index 2e72f20..0000000
--- a/fleece-core/src/main/java/org/apache/fleece/core/JsonPrettyGeneratorImpl.java
+++ /dev/null
@@ -1,305 +0,0 @@
-/*
- * 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.fleece.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-fleece/blob/6e86a53e/fleece-core/src/main/java/org/apache/fleece/core/JsonProviderImpl.java
----------------------------------------------------------------------
diff --git a/fleece-core/src/main/java/org/apache/fleece/core/JsonProviderImpl.java b/fleece-core/src/main/java/org/apache/fleece/core/JsonProviderImpl.java
deleted file mode 100644
index 6245030..0000000
--- a/fleece-core/src/main/java/org/apache/fleece/core/JsonProviderImpl.java
+++ /dev/null
@@ -1,205 +0,0 @@
-/*
- * 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.fleece.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-fleece/blob/6e86a53e/fleece-core/src/main/java/org/apache/fleece/core/JsonReaderFactoryImpl.java
----------------------------------------------------------------------
diff --git a/fleece-core/src/main/java/org/apache/fleece/core/JsonReaderFactoryImpl.java b/fleece-core/src/main/java/org/apache/fleece/core/JsonReaderFactoryImpl.java
deleted file mode 100644
index 6f0e063..0000000
--- a/fleece-core/src/main/java/org/apache/fleece/core/JsonReaderFactoryImpl.java
+++ /dev/null
@@ -1,72 +0,0 @@
-/*
- * 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.fleece.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);
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-fleece/blob/6e86a53e/fleece-core/src/main/java/org/apache/fleece/core/JsonReaderImpl.java
----------------------------------------------------------------------
diff --git a/fleece-core/src/main/java/org/apache/fleece/core/JsonReaderImpl.java b/fleece-core/src/main/java/org/apache/fleece/core/JsonReaderImpl.java
deleted file mode 100644
index 3586bc5..0000000
--- a/fleece-core/src/main/java/org/apache/fleece/core/JsonReaderImpl.java
+++ /dev/null
@@ -1,208 +0,0 @@
-/*
- * 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.fleece.core;
-
-import javax.json.JsonArray;
-import javax.json.JsonArrayBuilder;
-import javax.json.JsonObject;
-import javax.json.JsonObjectBuilder;
-import javax.json.JsonReader;
-import javax.json.JsonStructure;
-import javax.json.stream.JsonParser;
-import javax.json.stream.JsonParsingException;
-
-class JsonReaderImpl implements JsonReader {
- private final JsonParser parser;
- private boolean closed = false;
-
- JsonReaderImpl(final JsonParser parser) {
- this.parser = parser;
- }
-
- @Override
- public JsonStructure read() {
-
- checkClosed();
-
- if (!parser.hasNext()) {
- throw new IllegalStateException("Nothing to read");
- }
- switch (parser.next()) {
- case START_OBJECT:
- final JsonObjectBuilder objectBuilder = new JsonObjectBuilderImpl();
- parseObject(objectBuilder);
- if (parser.hasNext()) {
- throw new JsonParsingException("Expected end of file", parser.getLocation());
- }
- close();
- return objectBuilder.build();
- case START_ARRAY:
- final JsonArrayBuilder arrayBuilder = new JsonArrayBuilderImpl();
- parseArray(arrayBuilder);
- if (parser.hasNext()) {
- throw new JsonParsingException("Expected end of file", parser.getLocation());
- }
- close();
- return arrayBuilder.build();
- default:
- close();
- throw new JsonParsingException("Unknown structure: " + parser.next(), parser.getLocation());
- }
-
- }
-
- @Override
- public JsonObject readObject() {
- return JsonObject.class.cast(read());
- }
-
- @Override
- public JsonArray readArray() {
- return JsonArray.class.cast(read());
- }
-
- @Override
- public void close() {
-
- if (!closed) {
- closed = true;
- parser.close();
- }
-
- }
-
- private void parseObject(final JsonObjectBuilder builder) {
- String key = null;
- while (parser.hasNext()) {
- final JsonParser.Event next = parser.next();
- switch (next) {
- case KEY_NAME:
- key = parser.getString();
- break;
-
- case VALUE_STRING:
- builder.add(key, new JsonStringImpl(parser.getString()));
- break;
-
- case START_OBJECT:
- JsonObjectBuilder subObject = null;
- parseObject(subObject = new JsonObjectBuilderImpl());
- builder.add(key, subObject);
- break;
-
- case START_ARRAY:
- JsonArrayBuilder subArray = null;
- parseArray(subArray = new JsonArrayBuilderImpl());
- builder.add(key, subArray);
- break;
-
- case VALUE_NUMBER:
- if (parser.isIntegralNumber()) {
- builder.add(key, new JsonLongImpl(parser.getLong()));
- } else {
- builder.add(key, new JsonNumberImpl(parser.getBigDecimal()));
- }
- break;
-
- case VALUE_NULL:
- builder.addNull(key);
- break;
-
- case VALUE_TRUE:
- builder.add(key, true);
- break;
-
- case VALUE_FALSE:
- builder.add(key, false);
- break;
-
- case END_OBJECT:
- return;
-
- case END_ARRAY:
- throw new JsonParsingException("']', shouldn't occur", parser.getLocation());
-
- default:
- throw new JsonParsingException(next.name() + ", shouldn't occur", parser.getLocation());
- }
- }
- }
-
- private void parseArray(final JsonArrayBuilder builder) {
- while (parser.hasNext()) {
- final JsonParser.Event next = parser.next();
- switch (next) {
- case VALUE_STRING:
- builder.add(new JsonStringImpl(parser.getString()));
- break;
-
- case VALUE_NUMBER:
- if (parser.isIntegralNumber()) {
- builder.add(new JsonLongImpl(parser.getLong()));
- } else {
- builder.add(new JsonNumberImpl(parser.getBigDecimal()));
- }
- break;
-
- case START_OBJECT:
- JsonObjectBuilder subObject = null;
- parseObject(subObject = new JsonObjectBuilderImpl());
- builder.add(subObject);
- break;
-
- case START_ARRAY:
- JsonArrayBuilder subArray = null;
- parseArray(subArray = new JsonArrayBuilderImpl());
- builder.add(subArray);
- break;
-
- case END_ARRAY:
- return;
-
- case VALUE_NULL:
- builder.addNull();
- break;
-
- case VALUE_TRUE:
- builder.add(true);
- break;
-
- case VALUE_FALSE:
- builder.add(false);
- break;
-
- case KEY_NAME:
- throw new JsonParsingException("array doesn't have keys", parser.getLocation());
-
- case END_OBJECT:
- throw new JsonParsingException("'}', shouldn't occur", parser.getLocation());
-
- default:
- throw new JsonParsingException(next.name() + ", shouldn't occur", parser.getLocation());
- }
- }
- }
-
- private void checkClosed() {
- if (closed) {
- throw new IllegalStateException("read(), readObject(), readArray() or close() method was already called");
- }
-
- }
-}