#ignite-961: add ignite-json module.
Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/e5a33c1d Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/e5a33c1d Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/e5a33c1d Branch: refs/heads/ignite-1121 Commit: e5a33c1d69c258e2f4cd5f75e8a868e7dc01befe Parents: 8616eeb Author: ivasilinets <ivasilin...@gridgain.com> Authored: Thu Jul 16 12:48:56 2015 +0300 Committer: ivasilinets <ivasilin...@gridgain.com> Committed: Thu Jul 16 12:48:56 2015 +0300 ---------------------------------------------------------------------- modules/json/pom.xml | 88 ++++ .../org/apache/ignite/JsonArrayBuilderImpl.java | 130 +++++ .../java/org/apache/ignite/JsonArrayImpl.java | 131 +++++ .../org/apache/ignite/JsonGeneratorImpl.java | 500 +++++++++++++++++++ .../org/apache/ignite/JsonLocationImpl.java | 60 +++ .../java/org/apache/ignite/JsonNumberImpl.java | 116 +++++ .../apache/ignite/JsonObjectBuilderImpl.java | 141 ++++++ .../java/org/apache/ignite/JsonObjectImpl.java | 116 +++++ .../org/apache/ignite/JsonProviderImpl.java | 104 ++++ .../java/org/apache/ignite/JsonStringImpl.java | 71 +++ modules/nodejs/pom.xml | 2 +- pom.xml | 1 + 12 files changed, 1459 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/e5a33c1d/modules/json/pom.xml ---------------------------------------------------------------------- diff --git a/modules/json/pom.xml b/modules/json/pom.xml new file mode 100644 index 0000000..88c300e --- /dev/null +++ b/modules/json/pom.xml @@ -0,0 +1,88 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<!-- + 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. +--> + +<!-- + POM file. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.ignite</groupId> + <artifactId>ignite-parent</artifactId> + <version>1</version> + <relativePath>../../parent</relativePath> + </parent> + + <artifactId>ignite-json</artifactId> + <version>1.4.1-SNAPSHOT</version> + + <dependencies> + <dependency> + <groupId>javax.json</groupId> + <artifactId>javax.json-api</artifactId> + <version>1.0</version> + </dependency> + + <dependency> + <groupId>org.apache.ignite</groupId> + <artifactId>ignite-core</artifactId> + <version>${project.version}</version> + </dependency> + + <dependency> + <groupId>org.apache.ignite</groupId> + <artifactId>ignite-spring</artifactId> + <version>${project.version}</version> + </dependency> + + <dependency> + <groupId>org.apache.ignite</groupId> + <artifactId>ignite-log4j</artifactId> + <version>${project.version}</version> + </dependency> + + <dependency> + <groupId>org.apache.ignite</groupId> + <artifactId>ignite-indexing</artifactId> + <version>${project.version}</version> + </dependency> + + <dependency> + <groupId>org.apache.ignite</groupId> + <artifactId>ignite-rest-http</artifactId> + <version>${project.version}</version> + </dependency> + + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>4.11</version> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>org.apache.ignite</groupId> + <artifactId>ignite-core</artifactId> + <version>${project.version}</version> + <type>test-jar</type> + <scope>test</scope> + </dependency> + </dependencies> +</project> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/e5a33c1d/modules/json/src/main/java/org/apache/ignite/JsonArrayBuilderImpl.java ---------------------------------------------------------------------- diff --git a/modules/json/src/main/java/org/apache/ignite/JsonArrayBuilderImpl.java b/modules/json/src/main/java/org/apache/ignite/JsonArrayBuilderImpl.java new file mode 100644 index 0000000..97b3e9a --- /dev/null +++ b/modules/json/src/main/java/org/apache/ignite/JsonArrayBuilderImpl.java @@ -0,0 +1,130 @@ +/* + * 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.ignite; + +import org.apache.ignite.internal.util.typedef.internal.*; + +import javax.json.*; +import java.math.*; +import java.util.*; + +/** + * Json array builder. + */ +public class JsonArrayBuilderImpl implements JsonArrayBuilder { + /** Json array list. */ + private List<JsonValue> jsonList = new ArrayList<>(); + + /** {@inheritDoc} */ + @Override public JsonArrayBuilder add(JsonValue val) { + A.notNull(val, "value"); + + jsonList.add(val); + + return this; + } + + /** {@inheritDoc} */ + @Override public JsonArrayBuilder add(String val) { + A.notNull(val, "value"); + + jsonList.add(new JsonStringImpl(val)); + + return this; + } + + /** {@inheritDoc} */ + @Override public JsonArrayBuilder add(BigDecimal val) { + A.notNull(val, "value"); + + jsonList.add(new JsonNumberImpl(val)); + + return this; + } + + /** {@inheritDoc} */ + @Override public JsonArrayBuilder add(BigInteger val) { + A.notNull(val, "value"); + + //TODO: optimize for value + jsonList.add(new JsonNumberImpl(new BigDecimal(val))); + + return this; + } + + /** {@inheritDoc} */ + @Override public JsonArrayBuilder add(int val) { + //TODO: optimize for value + jsonList.add(new JsonNumberImpl(new BigDecimal(val))); + + return this; + } + + /** {@inheritDoc} */ + @Override public JsonArrayBuilder add(long val) { + //TODO: optimize for value + jsonList.add(new JsonNumberImpl(new BigDecimal(val))); + + return this; + } + + /** {@inheritDoc} */ + @Override public JsonArrayBuilder add(double val) { + //TODO: optimize for value + jsonList.add(new JsonNumberImpl(new BigDecimal(val))); + + return this; + } + + /** {@inheritDoc} */ + @Override public JsonArrayBuilder add(boolean val) { + jsonList.add(val ? JsonValue.TRUE : JsonValue.FALSE); + + return this; + } + + /** {@inheritDoc} */ + @Override public JsonArrayBuilder addNull() { + jsonList.add(JsonValue.NULL); + + return this; + } + + /** {@inheritDoc} */ + @Override public JsonArrayBuilder add(JsonObjectBuilder bld) { + A.notNull(bld, "value"); + + jsonList.add(bld.build()); + + return this; + } + + /** {@inheritDoc} */ + @Override public JsonArrayBuilder add(JsonArrayBuilder bld) { + A.notNull(bld, "value"); + + jsonList.add(bld.build()); + + return this; + } + + /** {@inheritDoc} */ + @Override public JsonArray build() { + return new JsonArrayImpl(jsonList); + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/e5a33c1d/modules/json/src/main/java/org/apache/ignite/JsonArrayImpl.java ---------------------------------------------------------------------- diff --git a/modules/json/src/main/java/org/apache/ignite/JsonArrayImpl.java b/modules/json/src/main/java/org/apache/ignite/JsonArrayImpl.java new file mode 100644 index 0000000..9005c22 --- /dev/null +++ b/modules/json/src/main/java/org/apache/ignite/JsonArrayImpl.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.ignite; + +import javax.json.*; +import java.util.*; + +/** + * Implementation of JsonArray + */ +public class JsonArrayImpl extends ArrayList<JsonValue> implements JsonArray { + /** Values for getValueAs. */ + private List<JsonValue> val; + + /** + * @param val List json values. + */ + public JsonArrayImpl(List<JsonValue> val) { + super(val); + } + + /** {@inheritDoc} */ + @Override public JsonObject getJsonObject(int idx) { + return (JsonObject)get(idx); + } + + /** {@inheritDoc} */ + @Override public JsonArray getJsonArray(int idx) { + return (JsonArray)get(idx); + } + + /** {@inheritDoc} */ + @Override public JsonNumber getJsonNumber(int idx) { + return (JsonNumber)get(idx); + } + + /** {@inheritDoc} */ + @Override public JsonString getJsonString(int idx) { + return (JsonString)get(idx); + } + + /** {@inheritDoc} */ + @SuppressWarnings("unchecked") + @Override public <T extends JsonValue> List<T> getValuesAs(Class<T> clazz) { + if (val == null) { + val = new ArrayList(this.size()); + + for (int i = 0; i < size(); ++i) + val.add(get(i)); + + val = Collections.unmodifiableList(val); + } + return (List<T>) val; + } + + /** {@inheritDoc} */ + @Override public String getString(int idx) { + return getJsonString(idx).getString(); + } + + /** {@inheritDoc} */ + @Override public String getString(int idx, String dfltVal) { + try { + return getString(idx); + } + catch (Exception e) { + return dfltVal; + } + } + + /** {@inheritDoc} */ + @Override public int getInt(int idx) { + return getJsonNumber(idx).intValue(); + } + + /** {@inheritDoc} */ + @Override public int getInt(int idx, int dfltVal) { + try { + return getInt(idx); + } catch (Exception e) { + return dfltVal; + } + } + + /** {@inheritDoc} */ + @Override public boolean getBoolean(int idx) { + JsonValue val = get(idx); + + if (val.equals(JsonValue.TRUE)) + return true; + + if (val.equals(JsonValue.FALSE)) + return false; + + throw new ClassCastException(); + } + + /** {@inheritDoc} */ + @Override public boolean getBoolean(int idx, boolean dfltVal) { + try { + return getBoolean(idx); + } catch (Exception e) { + return dfltVal; + } + } + + /** {@inheritDoc} */ + @Override public boolean isNull(int idx) { + return get(idx).equals(JsonValue.NULL); + } + + /** {@inheritDoc} */ + @Override public ValueType getValueType() { + return ValueType.ARRAY; + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/e5a33c1d/modules/json/src/main/java/org/apache/ignite/JsonGeneratorImpl.java ---------------------------------------------------------------------- diff --git a/modules/json/src/main/java/org/apache/ignite/JsonGeneratorImpl.java b/modules/json/src/main/java/org/apache/ignite/JsonGeneratorImpl.java new file mode 100644 index 0000000..95b336f --- /dev/null +++ b/modules/json/src/main/java/org/apache/ignite/JsonGeneratorImpl.java @@ -0,0 +1,500 @@ +/* + * 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.ignite; + +import javax.json.*; +import javax.json.stream.*; +import java.io.*; +import java.math.*; +import java.util.*; + +/** + * Json generator implementation. + */ +public class JsonGeneratorImpl implements JsonGenerator { + /** Writer. */ + private final BufferedWriter writer; + + private LinkedList<Element> ctx = new LinkedList(); + + /** + * @param writer Writer. + */ + public JsonGeneratorImpl(Writer writer) { + this.writer = new BufferedWriter(writer); + + ctx.push(new Element(Context.NONE, true)); + } + + /** {@inheritDoc} */ + @Override public JsonGenerator writeStartObject() { + try { + if (ctx.getLast().context() == Context.OBJECT || + (ctx.getLast().context() == Context.NONE && !ctx.getLast().isFirst())) + throw new JsonGenerationException("No name for object field."); + + writeComma(); + writer.write('{'); + + ctx.addLast(new Element(Context.OBJECT, true)); + + return this; + } + catch (IOException e) { + throw new JsonException("Writer fails.", e); + } + } + + /** {@inheritDoc} */ + @Override public JsonGenerator writeStartObject(String name) { + try { + if (ctx.getLast().context() != Context.OBJECT) + throw new JsonGenerationException("Object with name in not object scope."); + + writeComma(); + writeString(name); + writer.write(":"); + writer.write('{'); + + ctx.addLast(new Element(Context.OBJECT, true)); + + return this; + } + catch (IOException e) { + throw new JsonException("Writer fails.", e); + } + } + + /** {@inheritDoc} */ + @Override public JsonGenerator writeStartArray() { + try { + if (ctx.getLast().context() == Context.OBJECT || + (ctx.getLast().context() == Context.NONE && !ctx.getLast().isFirst())) + throw new JsonGenerationException("Array in object scope."); + + writeComma(); + writer.write("["); + + ctx.addLast(new Element(Context.ARRAY, true)); + + return this; + } + catch (IOException e) { + throw new JsonException("Writer fails.", e); + } + } + + /** {@inheritDoc} */ + @Override public JsonGenerator writeStartArray(String name) { + try { + if (ctx.getLast().context() != Context.OBJECT) + throw new JsonGenerationException("Array with name in not object scope."); + + writeComma(); + writeString(name); + writer.write(":"); + writer.write('['); + + ctx.addLast(new Element(Context.ARRAY, true)); + + return this; + } + catch (IOException e) { + throw new JsonException("Writer fails.", e); + } + } + + /** {@inheritDoc} */ + @Override public JsonGenerator write(String name, JsonValue val) { + if (ctx.getLast().context() != Context.OBJECT) + throw new JsonGenerationException("Json value with name in not object scope."); + + try { + switch (val.getValueType()) { + case ARRAY: { + JsonArray arr = (JsonArray) val; + + writeStartArray(name); + + for (JsonValue el : arr) + write(el); + + writeEnd(); + + break; + } + + case OBJECT: { + JsonObject o = (JsonObject) val; + + writeStartObject(name); + + for (Map.Entry<String, JsonValue> member : o.entrySet()) + write(member.getKey(), member.getValue()); + + writeEnd(); + + break; + } + + case STRING: { + JsonString str = (JsonString) val; + + write(name, str.getString()); + + break; + } + + case NUMBER: { + JsonNumber n = (JsonNumber) val; + + writeComma(); + writeString(name); + writer.write(":"); + writeString(n.toString()); + + break; + } + case TRUE: { + write(name, true); + + break; + } + + case FALSE: { + write(name, false); + + break; + } + + case NULL: { + writeNull(name); + + break; + } + } + return this; + } + catch (IOException e) { + throw new JsonException("Writer fails.", e); + } + } + + /** {@inheritDoc} */ + @Override public JsonGenerator write(String name, String val) { + return writeSimpleField(name, val); + } + + /** {@inheritDoc} */ + @Override public JsonGenerator write(String name, BigInteger val) { + return writeSimpleField(name, String.valueOf(val)); + } + + /** {@inheritDoc} */ + @Override public JsonGenerator write(String name, BigDecimal val) { + return writeSimpleField(name, String.valueOf(val)); + } + + /** {@inheritDoc} */ + @Override public JsonGenerator write(String name, int val) { + return writeSimpleField(name, String.valueOf(val)); + } + + /** {@inheritDoc} */ + @Override public JsonGenerator write(String name, long val) { + return writeSimpleField(name, String.valueOf(val)); + } + + /** {@inheritDoc} */ + @Override public JsonGenerator write(String name, double val) { + return writeSimpleField(name, String.valueOf(val)); + } + + /** {@inheritDoc} */ + @Override public JsonGenerator write(String name, boolean val) { + return writeSimpleField(name, String.valueOf(val)); + } + + /** {@inheritDoc} */ + @Override public JsonGenerator writeNull(String name) { + return writeSimpleField(name, "null"); + } + + /** {@inheritDoc} */ + @Override public JsonGenerator writeEnd() { + if (ctx.getLast().context() == Context.NONE) + throw new JsonGenerationException("Cannot call writeEnd in none context."); + + try { + if (ctx.getLast().context() == Context.ARRAY) + writer.write("]"); + + if (ctx.getLast().context() == Context.OBJECT) + writer.write("}"); + + ctx.removeLast(); + + return this; + } + catch(IOException e) { + throw new JsonException("Writer fails.", e); + } + } + + /** {@inheritDoc} */ + @Override public JsonGenerator write(JsonValue val) { + if (ctx.getLast().context() != Context.ARRAY) + throw new JsonGenerationException("Json value without name in not array scope."); + + try { + switch (val.getValueType()) { + case ARRAY: { + JsonArray arr = (JsonArray) val; + + writeStartArray(); + + for (JsonValue el : arr) + write(el); + + writeEnd(); + + break; + } + + case OBJECT: { + JsonObject o = (JsonObject) val; + + writeStartObject(); + + for (Map.Entry<String, JsonValue> member : o.entrySet()) + write(member.getKey(), member.getValue()); + + writeEnd(); + + break; + } + + case STRING: { + JsonString str = (JsonString) val; + + write(str.getString()); + + break; + } + + case NUMBER: { + JsonNumber n = (JsonNumber) val; + + writeComma(); + writeString(n.toString()); + + break; + } + case TRUE: { + write(true); + + break; + } + + case FALSE: { + write(false); + + break; + } + + case NULL: { + writeNull(); + + break; + } + } + return this; + } + catch (IOException e) { + throw new JsonException("Writer fails.", e); + } + } + + /** {@inheritDoc} */ + @Override public JsonGenerator write(String val) { + return writeSimpleArrayElement(val); + } + + /** {@inheritDoc} */ + @Override public JsonGenerator write(BigDecimal val) { + return writeSimpleArrayElement(String.valueOf(val)); + } + + /** {@inheritDoc} */ + @Override public JsonGenerator write(BigInteger val) { + return writeSimpleArrayElement(String.valueOf(val)); + } + + /** {@inheritDoc} */ + @Override public JsonGenerator write(int val) { + return writeSimpleArrayElement(String.valueOf(val)); + } + + /** {@inheritDoc} */ + @Override public JsonGenerator write(long val) { + return writeSimpleArrayElement(String.valueOf(val)); + } + + /** {@inheritDoc} */ + @Override public JsonGenerator write(double val) { + return writeSimpleArrayElement(String.valueOf(val)); + } + + /** {@inheritDoc} */ + @Override public JsonGenerator write(boolean val) { + return writeSimpleArrayElement(String.valueOf(val)); + } + + /** {@inheritDoc} */ + @Override public JsonGenerator writeNull() { + return writeSimpleArrayElement("null"); + } + + /** {@inheritDoc} */ + @Override public void close() { + try { + writer.close(); + } + catch (IOException e) { + throw new JsonException("Could not close writer.", e); + } + } + + /** {@inheritDoc} */ + @Override public void flush() { + try { + writer.flush(); + } + catch (IOException e) { + throw new JsonException("Could not flush buffer to writer.", e); + } + } + + /** + * Write comma if object is not first. + * + * @throws IOException If failed. + */ + private void writeComma() throws IOException{ + if (!ctx.getLast().isFirst()) + writer.write(","); + + ctx.getLast().isFirst = false; + } + + /** + * @param name Field name. + * @param val Field value. + */ + private JsonGenerator writeSimpleField(String name, String val) { + if (ctx.getLast().context() != Context.OBJECT) + throw new JsonGenerationException("String with name in not object scope."); + + try { + writeComma(); + writeString(name); + writer.write(":"); + writeString(val); + + return this; + } + catch (IOException e) { + throw new JsonException("Writer fails.", e); + } + } + + + /** + * @param val Field value. + */ + private JsonGenerator writeSimpleArrayElement(String val) { + if (ctx.getLast().context() != Context.ARRAY) + throw new JsonGenerationException("String without name in not array scope."); + + try { + writeComma(); + writeString(val); + + return this; + } + catch (IOException e) { + throw new JsonException("Writer fails.", e); + } + } + + /** + * @param str String to write. + * @throws IOException If failed. + * //TODO: escape string. + */ + private void writeString(String str) throws IOException { + writer.write(str); + } + + /** + * Generator element. + */ + private static class Element { + /** Context. */ + private Context ctx; + + /** First element flag. */ + private boolean isFirst; + + /** + * @param ctx Context. + * @param isFirst First element flag. + */ + public Element(Context ctx, boolean isFirst) { + this.ctx = ctx; + this.isFirst = isFirst; + } + + /** + * @return First element flag. + */ + public boolean isFirst() { + return isFirst; + } + + /** + * @return Context. + */ + public Context context() { + return ctx; + } + } + /** + * Context for writer. + */ + private enum Context { + /** Writing object. */ + OBJECT, + + /** Writing array. */ + ARRAY, + + /** Not in object or in array. */ + NONE + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/e5a33c1d/modules/json/src/main/java/org/apache/ignite/JsonLocationImpl.java ---------------------------------------------------------------------- diff --git a/modules/json/src/main/java/org/apache/ignite/JsonLocationImpl.java b/modules/json/src/main/java/org/apache/ignite/JsonLocationImpl.java new file mode 100644 index 0000000..078c16f --- /dev/null +++ b/modules/json/src/main/java/org/apache/ignite/JsonLocationImpl.java @@ -0,0 +1,60 @@ +/* + * 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.ignite; + +import javax.json.stream.*; + +/** + * Json location implementation. + */ +public class JsonLocationImpl implements JsonLocation { + /** Column number. */ + private final long col; + + /** Line number. */ + private final long line; + + /** Stream offset. */ + private final long off; + + /** + * @param line Line number. + * @param col Column number. + * @param streamOff Stream offset. + */ + JsonLocationImpl(long line, long col, long streamOff) { + this.line = line; + this.col = col; + this.off = streamOff; + } + + /** {@inheritDoc} */ + @Override public long getLineNumber() { + return line; + } + + /** {@inheritDoc} */ + @Override public long getColumnNumber() { + return col; + } + + /** {@inheritDoc} */ + @Override public long getStreamOffset() { + return off; + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/e5a33c1d/modules/json/src/main/java/org/apache/ignite/JsonNumberImpl.java ---------------------------------------------------------------------- diff --git a/modules/json/src/main/java/org/apache/ignite/JsonNumberImpl.java b/modules/json/src/main/java/org/apache/ignite/JsonNumberImpl.java new file mode 100644 index 0000000..7cd0a68 --- /dev/null +++ b/modules/json/src/main/java/org/apache/ignite/JsonNumberImpl.java @@ -0,0 +1,116 @@ +/* + * 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.ignite; + +import javax.json.*; +import java.math.*; + +/** + * Json number implementation. + * //TODO: optimize for int, long, double... + */ +public class JsonNumberImpl implements JsonNumber { + /** Value. */ + private final BigDecimal val; + + /** + * @param val Value. + */ + public JsonNumberImpl(BigDecimal val){ + this.val = val; + } + + /** {@inheritDoc} */ + @Override public boolean isIntegral() { + if (val == null) + return false; + + return val.scale() == 0; + } + + /** {@inheritDoc} */ + @Override public int intValue() { + return val.intValue(); + } + + /** {@inheritDoc} */ + @Override public int intValueExact() { + return val.intValueExact(); + } + + /** {@inheritDoc} */ + @Override public long longValue() { + return val.longValue(); + } + + /** {@inheritDoc} */ + @Override public long longValueExact() { + return val.longValueExact(); + } + + /** {@inheritDoc} */ + @Override public BigInteger bigIntegerValue() { + return val.toBigInteger(); + } + + /** {@inheritDoc} */ + @Override public BigInteger bigIntegerValueExact() { + return val.toBigIntegerExact(); + } + + /** {@inheritDoc} */ + @Override public double doubleValue() { + return val.doubleValue(); + } + + /** {@inheritDoc} */ + @Override public BigDecimal bigDecimalValue() { + return val; + } + + /** {@inheritDoc} */ + @Override public ValueType getValueType() { + return ValueType.NUMBER; + } + + /** {@inheritDoc} */ + @Override public String toString() { + return val.toString(); + } + + /** {@inheritDoc} */ + @Override public int hashCode() { + if (val == null) + return 0; + + return val.hashCode(); + } + + /** {@inheritDoc} */ + @Override public boolean equals(Object obj) { + if (obj == null || !(obj instanceof JsonNumberImpl)) + return false; + + BigDecimal val0 = ((JsonNumberImpl)obj).val; + + if (val == null) + return val0 == null; + + return val.equals(val0); + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/e5a33c1d/modules/json/src/main/java/org/apache/ignite/JsonObjectBuilderImpl.java ---------------------------------------------------------------------- diff --git a/modules/json/src/main/java/org/apache/ignite/JsonObjectBuilderImpl.java b/modules/json/src/main/java/org/apache/ignite/JsonObjectBuilderImpl.java new file mode 100644 index 0000000..6246794 --- /dev/null +++ b/modules/json/src/main/java/org/apache/ignite/JsonObjectBuilderImpl.java @@ -0,0 +1,141 @@ +/* + * 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.ignite; + +import org.apache.ignite.internal.util.typedef.internal.*; + +import javax.json.*; +import java.math.*; +import java.util.*; + +/** + * Json object builder implementation. + */ +public class JsonObjectBuilderImpl implements JsonObjectBuilder { + /** Json object map. */ + private Map<String, JsonValue> jsonMap = new HashMap<>(); + + /** {@inheritDoc} */ + @Override public JsonObjectBuilder add(String name, JsonValue val) { + A.notNull(name, "key", val, "value"); + + jsonMap.put(name, val); + + return this; + } + + /** {@inheritDoc} */ + @Override public JsonObjectBuilder add(String name, String val) { + A.notNull(name, "key", val, "value"); + + jsonMap.put(name, new JsonStringImpl(val)); + + return this; + } + + /** {@inheritDoc} */ + @Override public JsonObjectBuilder add(String name, BigInteger val) { + A.notNull(name, "key", val, "value"); + + //TODO: optimize for value + jsonMap.put(name, new JsonNumberImpl(new BigDecimal(val))); + + return this; + } + + /** {@inheritDoc} */ + @Override public JsonObjectBuilder add(String name, BigDecimal val) { + A.notNull(name, "key", val, "value"); + + //TODO: optimize for value + jsonMap.put(name, new JsonNumberImpl(val)); + + return this; + } + + /** {@inheritDoc} */ + @Override public JsonObjectBuilder add(String name, int val) { + A.notNull(name, "key"); + + //TODO: optimize for value + jsonMap.put(name, new JsonNumberImpl(new BigDecimal(val))); + + return this; + } + + /** {@inheritDoc} */ + @Override public JsonObjectBuilder add(String name, long val) { + A.notNull(name, "key"); + + //TODO: optimize for value + jsonMap.put(name, new JsonNumberImpl(new BigDecimal(val))); + + return this; + } + + /** {@inheritDoc} */ + @Override public JsonObjectBuilder add(String name, double val) { + A.notNull(name, "key"); + + //TODO: optimize for value + jsonMap.put(name, new JsonNumberImpl(new BigDecimal(val))); + + return this; + } + + /** {@inheritDoc} */ + @Override public JsonObjectBuilder add(String name, boolean val) { + A.notNull(name, "key"); + + jsonMap.put(name, val ? JsonValue.TRUE : JsonValue.FALSE); + + return this; + } + + /** {@inheritDoc} */ + @Override public JsonObjectBuilder addNull(String name) { + A.notNull(name, "key"); + + jsonMap.put(name, JsonValue.NULL); + + return this; + } + + /** {@inheritDoc} */ + @Override public JsonObjectBuilder add(String name, JsonObjectBuilder bld) { + A.notNull(name, "key", bld, "value"); + + jsonMap.put(name, bld.build()); + + return this; + } + + /** {@inheritDoc} */ + @Override public JsonObjectBuilder add(String name, JsonArrayBuilder bld) { + A.notNull(name, "key", bld, "value"); + + jsonMap.put(name, bld.build()); + + return this; + } + + /** {@inheritDoc} */ + @Override public JsonObject build() { + return new JsonObjectImpl(jsonMap); + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/e5a33c1d/modules/json/src/main/java/org/apache/ignite/JsonObjectImpl.java ---------------------------------------------------------------------- diff --git a/modules/json/src/main/java/org/apache/ignite/JsonObjectImpl.java b/modules/json/src/main/java/org/apache/ignite/JsonObjectImpl.java new file mode 100644 index 0000000..8cb7637 --- /dev/null +++ b/modules/json/src/main/java/org/apache/ignite/JsonObjectImpl.java @@ -0,0 +1,116 @@ +/* + * 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.ignite; + +import javax.json.*; +import java.util.*; + +/** + * JsonObject implementation. + */ +public class JsonObjectImpl extends HashMap<String, JsonValue> implements JsonObject { + /** + * @param val Map to store. + */ + public JsonObjectImpl(Map<String, JsonValue> val) { + super(val); + } + + /** {@inheritDoc} */ + @Override public JsonArray getJsonArray(String name) { + return (JsonArray)get(name); + } + + /** {@inheritDoc} */ + @Override public JsonObject getJsonObject(String name) { + return (JsonObject)get(name); + } + + /** {@inheritDoc} */ + @Override public JsonNumber getJsonNumber(String name) { + return (JsonNumber)get(name); + } + + /** {@inheritDoc} */ + @Override public JsonString getJsonString(String name) { + return (JsonString)get(name); + } + + /** {@inheritDoc} */ + @Override public String getString(String name) { + return getJsonString(name).getString(); + } + + /** {@inheritDoc} */ + @Override public String getString(String name, String dfltVal) { + try { + return getString(name); + } + catch (Exception e) { + return dfltVal; + } + } + + /** {@inheritDoc} */ + @Override public int getInt(String name) { + return getJsonNumber(name).intValue(); + } + + /** {@inheritDoc} */ + @Override public int getInt(String name, int dfltVal) { + try { + return getInt(name); + } + catch (Exception e) { + return dfltVal; + } + } + + /** {@inheritDoc} */ + @Override public boolean getBoolean(String name) { + JsonValue val = get(name); + + if (val.equals(JsonValue.TRUE)) + return true; + + if (val.equals(JsonValue.FALSE)) + return false; + + throw new ClassCastException(); + } + + /** {@inheritDoc} */ + @Override public boolean getBoolean(String name, boolean dfltVal) { + try { + return getBoolean(name); + } + catch (Exception e) { + return dfltVal; + } + } + + /** {@inheritDoc} */ + @Override public boolean isNull(String name) { + return get(name).equals(JsonValue.NULL); + } + + /** {@inheritDoc} */ + @Override public ValueType getValueType() { + return ValueType.OBJECT; + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/e5a33c1d/modules/json/src/main/java/org/apache/ignite/JsonProviderImpl.java ---------------------------------------------------------------------- diff --git a/modules/json/src/main/java/org/apache/ignite/JsonProviderImpl.java b/modules/json/src/main/java/org/apache/ignite/JsonProviderImpl.java new file mode 100644 index 0000000..9bb6305 --- /dev/null +++ b/modules/json/src/main/java/org/apache/ignite/JsonProviderImpl.java @@ -0,0 +1,104 @@ +/* + * 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.ignite; + +import javax.json.*; +import javax.json.spi.*; +import javax.json.stream.*; +import java.io.*; +import java.util.*; + +/** + * Json provider implementation. + */ +public class JsonProviderImpl extends JsonProvider { + /** {@inheritDoc} */ + @Override public JsonParser createParser(Reader reader) { + return null; + } + + /** {@inheritDoc} */ + @Override public JsonParser createParser(InputStream in) { + return null; + } + + /** {@inheritDoc} */ + @Override public JsonParserFactory createParserFactory(Map<String, ?> config) { + return null; + } + + /** {@inheritDoc} */ + @Override public JsonGenerator createGenerator(Writer writer) { + return new JsonGeneratorImpl(writer); + } + + /** {@inheritDoc} */ + @Override public JsonGenerator createGenerator(OutputStream out) { + return null; + } + + /** {@inheritDoc} */ + @Override public JsonGeneratorFactory createGeneratorFactory(Map<String, ?> config) { + return null; + } + + /** {@inheritDoc} */ + @Override public JsonReader createReader(Reader reader) { + return null; + } + + /** {@inheritDoc} */ + @Override public JsonReader createReader(InputStream in) { + return null; + } + + /** {@inheritDoc} */ + @Override public JsonWriter createWriter(Writer writer) { + return null; + } + + /** {@inheritDoc} */ + @Override public JsonWriter createWriter(OutputStream out) { + return null; + } + + /** {@inheritDoc} */ + @Override public JsonWriterFactory createWriterFactory(Map<String, ?> config) { + return null; + } + + /** {@inheritDoc} */ + @Override public JsonReaderFactory createReaderFactory(Map<String, ?> config) { + return null; + } + + /** {@inheritDoc} */ + @Override public JsonObjectBuilder createObjectBuilder() { + return new JsonObjectBuilderImpl(); + } + + /** {@inheritDoc} */ + @Override public JsonArrayBuilder createArrayBuilder() { + return new JsonArrayBuilderImpl(); + } + + /** {@inheritDoc} */ + @Override public JsonBuilderFactory createBuilderFactory(Map<String, ?> config) { + return null; + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/e5a33c1d/modules/json/src/main/java/org/apache/ignite/JsonStringImpl.java ---------------------------------------------------------------------- diff --git a/modules/json/src/main/java/org/apache/ignite/JsonStringImpl.java b/modules/json/src/main/java/org/apache/ignite/JsonStringImpl.java new file mode 100644 index 0000000..6314826 --- /dev/null +++ b/modules/json/src/main/java/org/apache/ignite/JsonStringImpl.java @@ -0,0 +1,71 @@ +/* + * 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.ignite; + +import javax.json.*; + +/** + * Json string implementation. + */ +public class JsonStringImpl implements JsonString { + /** Value. */ + private final String val; + + /** + * @param val Value. + */ + public JsonStringImpl(String val) { + this.val = val; + } + + /** {@inheritDoc} */ + @Override public String getString() { + return val; + } + + /** {@inheritDoc} */ + @Override public CharSequence getChars() { + return val; + } + + /** {@inheritDoc} */ + @Override public ValueType getValueType() { + return ValueType.STRING; + } + + /** {@inheritDoc} */ + @Override public int hashCode() { + if (val == null) + return 0; + + return val.hashCode(); + } + + /** {@inheritDoc} */ + @Override public boolean equals(Object obj) { + if (obj == null || !(obj instanceof JsonString)) + return false; + + JsonString other = (JsonString)obj; + + if (val == null) + return other.getString() == null; + + return val.equals(other.getString()); + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/e5a33c1d/modules/nodejs/pom.xml ---------------------------------------------------------------------- diff --git a/modules/nodejs/pom.xml b/modules/nodejs/pom.xml index 6897511..aa649f1 100644 --- a/modules/nodejs/pom.xml +++ b/modules/nodejs/pom.xml @@ -31,7 +31,7 @@ </parent> <artifactId>ignite-nodejs</artifactId> - <version>1.2.1-SNAPSHOT</version> + <version>1.4.1-SNAPSHOT</version> <dependencies> <dependency> http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/e5a33c1d/pom.xml ---------------------------------------------------------------------- diff --git a/pom.xml b/pom.xml index 771689b..777285e 100644 --- a/pom.xml +++ b/pom.xml @@ -74,6 +74,7 @@ <module>modules/nodejs</module> <module>modules/kafka</module> <module>modules/yarn</module> + <module>modules/json</module> </modules> <profiles>