Skip to content

Commit

Permalink
eclipse-ee4j#90: MapUtil.handle does not support Array objects
Browse files Browse the repository at this point in the history
Signed-off-by: Lukas Jungmann <[email protected]>
  • Loading branch information
lukasj committed Jul 12, 2023
1 parent 72f5e1b commit 64e9f59
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
52 changes: 52 additions & 0 deletions impl/src/main/java/org/eclipse/parsson/MapUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -67,6 +68,57 @@ static JsonValue handle(Object value, JsonContext jsonContext) {
@SuppressWarnings("unchecked")
JsonObjectBuilder object = new JsonObjectBuilderImpl((Map<String, Object>) 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()));
Expand Down
21 changes: 20 additions & 1 deletion impl/src/test/java/org/eclipse/parsson/tests/JsonObjectTest.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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<String, Object> 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());
}
}

0 comments on commit 64e9f59

Please sign in to comment.