From 58d4a45ba559ce5394eff47f7caf7709bd2e70cd Mon Sep 17 00:00:00 2001 From: Lukas Jungmann Date: Wed, 12 Jul 2023 23:26:24 +0200 Subject: [PATCH] #90: MapUtil.handle does not support Array objects Signed-off-by: Lukas Jungmann --- .../java/org/eclipse/parsson/MapUtil.java | 52 +++++++++++++++++++ .../eclipse/parsson/tests/JsonObjectTest.java | 21 +++++++- 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/impl/src/main/java/org/eclipse/parsson/MapUtil.java b/impl/src/main/java/org/eclipse/parsson/MapUtil.java index f1da3e1e..820fac9d 100644 --- a/impl/src/main/java/org/eclipse/parsson/MapUtil.java +++ b/impl/src/main/java/org/eclipse/parsson/MapUtil.java @@ -21,6 +21,7 @@ import jakarta.json.JsonValue; import java.math.BigDecimal; import java.math.BigInteger; +import java.util.Arrays; import java.util.Collection; import java.util.Map; @@ -67,6 +68,57 @@ static JsonValue handle(Object value, JsonContext jsonContext) { @SuppressWarnings("unchecked") JsonObjectBuilder object = new JsonObjectBuilderImpl((Map) value, jsonContext); return object.build(); + } else if (value instanceof Object[]) { + JsonArrayBuilder jsonArrayBuilder = new JsonArrayBuilderImpl(Arrays.asList((Object[]) value), jsonContext); + return jsonArrayBuilder.build(); + } else if (value instanceof int[]) { + JsonArrayBuilder jsonArrayBuilder = new JsonArrayBuilderImpl(jsonContext); + for (int d : (int[]) value) { + jsonArrayBuilder.add(d); + } + return jsonArrayBuilder.build(); + } else if (value instanceof long[]) { + JsonArrayBuilder jsonArrayBuilder = new JsonArrayBuilderImpl(jsonContext); + for (long d : (long[]) value) { + jsonArrayBuilder.add(d); + } + return jsonArrayBuilder.build(); + } else if (value instanceof double[]) { + JsonArrayBuilder jsonArrayBuilder = new JsonArrayBuilderImpl(jsonContext); + for (double d : (double[]) value) { + jsonArrayBuilder.add(d); + } + return jsonArrayBuilder.build(); + } else if (value instanceof boolean[]) { + JsonArrayBuilder jsonArrayBuilder = new JsonArrayBuilderImpl(jsonContext); + for (boolean d : (boolean[]) value) { + jsonArrayBuilder.add(d); + } + return jsonArrayBuilder.build(); + } else if (value instanceof char[]) { + JsonArrayBuilder jsonArrayBuilder = new JsonArrayBuilderImpl(jsonContext); + for (char d : (char[]) value) { + jsonArrayBuilder.add(d); + } + return jsonArrayBuilder.build(); + } else if (value instanceof float[]) { + JsonArrayBuilder jsonArrayBuilder = new JsonArrayBuilderImpl(jsonContext); + for (float d : (float[]) value) { + jsonArrayBuilder.add(d); + } + return jsonArrayBuilder.build(); + } else if (value instanceof byte[]) { + JsonArrayBuilder jsonArrayBuilder = new JsonArrayBuilderImpl(jsonContext); + for (byte d : (byte[]) value) { + jsonArrayBuilder.add(d); + } + return jsonArrayBuilder.build(); + } else if (value instanceof short[]) { + JsonArrayBuilder jsonArrayBuilder = new JsonArrayBuilderImpl(jsonContext); + for (short d : (short[]) value) { + jsonArrayBuilder.add(d); + } + return jsonArrayBuilder.build(); } throw new IllegalArgumentException(String.format("Type %s is not supported.", value.getClass())); diff --git a/impl/src/test/java/org/eclipse/parsson/tests/JsonObjectTest.java b/impl/src/test/java/org/eclipse/parsson/tests/JsonObjectTest.java index 59b3eaf7..7665dd2c 100644 --- a/impl/src/test/java/org/eclipse/parsson/tests/JsonObjectTest.java +++ b/impl/src/test/java/org/eclipse/parsson/tests/JsonObjectTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2021 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2023 Oracle and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0, which is available at @@ -229,4 +229,23 @@ public void testHashCode() { assertTrue(object3.hashCode() == object4.hashCode()); //equal instances have same hashCode } + public void testArrays() { + String[] stringArr = new String[] {"a", "b", "c"}; + boolean[] boolArr = new boolean[] {true, false, true}; + int[] intArr = new int[] {1, 2, 3}; + char[] charArr = new char[] {'a', 'b', 'c'}; + float[] floatArr = new float[] {1.0f, 2.0f, 3.0f}; + Map m = new HashMap<>(); + m.put("stringArray", stringArr); + m.put("booleanArray", boolArr); + m.put("intArray", intArr); + m.put("charArray", charArr); + m.put("floatArray", floatArr); + JsonObject object = Json.createObjectBuilder(m).build(); + assertEquals("b", object.get("stringArray").asJsonArray().getString(1)); + assertEquals(false, object.get("booleanArray").asJsonArray().getBoolean(1)); + assertEquals(2, object.get("intArray").asJsonArray().getInt(1)); + assertEquals('b', object.get("charArray").asJsonArray().getInt(1)); + assertEquals(2.0, object.get("floatArray").asJsonArray().getJsonNumber(1).doubleValue()); + } }