Repository: johnzon Updated Branches: refs/heads/master 02993157e -> 24cbca333
JOHNZON-96 implement Json.createValue methods Project: http://git-wip-us.apache.org/repos/asf/johnzon/repo Commit: http://git-wip-us.apache.org/repos/asf/johnzon/commit/24cbca33 Tree: http://git-wip-us.apache.org/repos/asf/johnzon/tree/24cbca33 Diff: http://git-wip-us.apache.org/repos/asf/johnzon/diff/24cbca33 Branch: refs/heads/master Commit: 24cbca3339977ff505d3f0d8a6cd6a2887621250 Parents: 0299315 Author: Mark Struberg <[email protected]> Authored: Fri Feb 24 16:29:37 2017 +0100 Committer: Mark Struberg <[email protected]> Committed: Fri Feb 24 16:29:37 2017 +0100 ---------------------------------------------------------------------- .../apache/johnzon/core/JsonProviderImpl.java | 30 +++++++ .../apache/johnzon/core/JsonProviderTest.java | 86 ++++++++++++++++++++ 2 files changed, 116 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/johnzon/blob/24cbca33/johnzon-core/src/main/java/org/apache/johnzon/core/JsonProviderImpl.java ---------------------------------------------------------------------- diff --git a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonProviderImpl.java b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonProviderImpl.java index 5fd5074..189caad 100644 --- a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonProviderImpl.java +++ b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonProviderImpl.java @@ -311,6 +311,36 @@ public class JsonProviderImpl extends JsonProvider implements Serializable { } @Override + public JsonString createValue(String value) { + return new JsonStringImpl(value); + } + + @Override + public JsonNumber createValue(int value) { + return new JsonLongImpl(value); + } + + @Override + public JsonNumber createValue(long value) { + return new JsonLongImpl(value); + } + + @Override + public JsonNumber createValue(double value) { + return new JsonDoubleImpl(value); + } + + @Override + public JsonNumber createValue(BigDecimal value) { + return new JsonNumberImpl(value); + } + + @Override + public JsonNumber createValue(BigInteger value) { + return new JsonLongImpl(value.longValue()); + } + + @Override public JsonBuilderFactory createBuilderFactory(final Map<String, ?> config) { return (config == null || config.isEmpty()) ? builderFactory : new JsonBuilderFactoryImpl(config); } http://git-wip-us.apache.org/repos/asf/johnzon/blob/24cbca33/johnzon-core/src/test/java/org/apache/johnzon/core/JsonProviderTest.java ---------------------------------------------------------------------- diff --git a/johnzon-core/src/test/java/org/apache/johnzon/core/JsonProviderTest.java b/johnzon-core/src/test/java/org/apache/johnzon/core/JsonProviderTest.java new file mode 100644 index 0000000..6d48c54 --- /dev/null +++ b/johnzon-core/src/test/java/org/apache/johnzon/core/JsonProviderTest.java @@ -0,0 +1,86 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.johnzon.core; + +import java.math.BigDecimal; +import java.math.BigInteger; + +import javax.json.Json; +import javax.json.JsonNumber; +import javax.json.JsonValue; + +import org.junit.Assert; +import org.junit.Test; + +public class JsonProviderTest { + + @Test + public void testJsonCreateValueString() { + JsonValue val = Json.createValue("hello"); + Assert.assertNotNull(val); + Assert.assertEquals(JsonValue.ValueType.STRING, val.getValueType()); + Assert.assertEquals("\"hello\"", val.toString()); + } + + @Test + public void testJsonCreateValueInt() { + JsonValue val = Json.createValue(4711); + Assert.assertNotNull(val); + Assert.assertEquals(JsonValue.ValueType.NUMBER, val.getValueType()); + Assert.assertEquals("4711", val.toString()); + } + + @Test + public void testJsonCreateValueLong() { + JsonValue val = Json.createValue(1234567890L); + Assert.assertNotNull(val); + Assert.assertEquals(JsonValue.ValueType.NUMBER, val.getValueType()); + Assert.assertEquals("1234567890", val.toString()); + } + + @Test + public void testJsonCreateValueDouble() { + double d = 1234567890.12345d; + JsonNumber val = Json.createValue(d); + Assert.assertNotNull(val); + Assert.assertEquals(JsonValue.ValueType.NUMBER, val.getValueType()); + + Assert.assertEquals(d, val.doubleValue(), 0.01f); + } + + @Test + public void testJsonCreateValueBigDecimal() { + BigDecimal bd = new BigDecimal(1234567890.12345d); + JsonNumber val = Json.createValue(bd); + Assert.assertNotNull(val); + Assert.assertEquals(JsonValue.ValueType.NUMBER, val.getValueType()); + + Assert.assertEquals(bd, val.bigDecimalValue()); + } + + @Test + public void testJsonCreateValueBigInteger() { + BigInteger bi = BigInteger.valueOf(1234567890L); + JsonNumber val = Json.createValue(bi); + Assert.assertNotNull(val); + Assert.assertEquals(JsonValue.ValueType.NUMBER, val.getValueType()); + + Assert.assertEquals(bi, val.bigIntegerValue()); + } +}
