diff --git a/jr-objects/src/main/java/com/fasterxml/jackson/jr/ob/impl/SimpleValueReader.java b/jr-objects/src/main/java/com/fasterxml/jackson/jr/ob/impl/SimpleValueReader.java index 5f89be68..72c8bbff 100644 --- a/jr-objects/src/main/java/com/fasterxml/jackson/jr/ob/impl/SimpleValueReader.java +++ b/jr-objects/src/main/java/com/fasterxml/jackson/jr/ob/impl/SimpleValueReader.java @@ -6,9 +6,8 @@ import java.net.URL; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.Calendar; -import java.util.Date; -import java.util.UUID; +import java.util.*; +import java.util.stream.DoubleStream; import java.util.stream.IntStream; import java.util.stream.LongStream; @@ -30,6 +29,9 @@ public class SimpleValueReader extends ValueReader private final static int[] NO_INTS = new int[0]; private final static long[] NO_LONGS = new long[0]; + // @since 2.21 + private final static double[] NO_DOUBLES = new double[0]; + protected final int _typeId; public SimpleValueReader(Class raw, int typeId) { @@ -119,9 +121,11 @@ public Object read(JSONReader reader, JsonParser p) throws IOException case SER_BOOLEAN_ARRAY: case SER_SHORT_ARRAY: case SER_FLOAT_ARRAY: - case SER_DOUBLE_ARRAY: throw JSONObjectException.from(p, "Deserialization of `"+_valueTypeDesc()+"` not yet supported"); + case SER_DOUBLE_ARRAY: + return _readDoubleArray(p); + case SER_TREE_NODE: return reader.readTree(); @@ -390,4 +394,36 @@ protected long _fetchLong(JsonParser p) throws IOException throw JSONObjectException.from(p, "Can not get long numeric value from JSON (to construct " +_valueTypeDesc()+") from "+_tokenDesc(p, t)); } + + protected double[] _readDoubleArray(JsonParser p) throws IOException { + if (JsonToken.START_ARRAY.equals(p.currentToken())) { + p.nextToken(); + } + + final DoubleStream.Builder builder = DoubleStream.builder(); + int t = p.currentTokenId(); + + if (t == JsonTokenId.ID_END_ARRAY) { + return NO_DOUBLES; + } + + main_loop: + while (true) { + switch (t) { + case JsonTokenId.ID_NUMBER_FLOAT: + case JsonTokenId.ID_NUMBER_INT: + case JsonTokenId.ID_NULL: + builder.add(p.getValueAsDouble()); + break; + case JsonTokenId.ID_END_ARRAY: + break main_loop; + default: + throw new JSONObjectException("Failed to bind `double` element of `double[]` from value: "+ + _tokenDesc(p)); + } + p.nextToken(); + t = p.currentTokenId(); + } + return builder.build().toArray(); + } } diff --git a/jr-objects/src/main/java/com/fasterxml/jackson/jr/ob/impl/ValueLocatorBase.java b/jr-objects/src/main/java/com/fasterxml/jackson/jr/ob/impl/ValueLocatorBase.java index e9a28d28..c95386f9 100644 --- a/jr-objects/src/main/java/com/fasterxml/jackson/jr/ob/impl/ValueLocatorBase.java +++ b/jr-objects/src/main/java/com/fasterxml/jackson/jr/ob/impl/ValueLocatorBase.java @@ -64,7 +64,7 @@ public abstract class ValueLocatorBase public final static int SER_FLOAT_ARRAY = 8; // since 2.20 public final static int SER_DOUBLE_ARRAY = 9; // since 2.20 public final static int SER_BOOLEAN_ARRAY = 10; - + /** * An implementation of {@link com.fasterxml.jackson.core.TreeNode} */ diff --git a/jr-objects/src/test/java/com/fasterxml/jackson/jr/ob/PrimitiveArrayTest.java b/jr-objects/src/test/java/com/fasterxml/jackson/jr/ob/PrimitiveArrayTest.java index d6702cb0..7e03cbd0 100644 --- a/jr-objects/src/test/java/com/fasterxml/jackson/jr/ob/PrimitiveArrayTest.java +++ b/jr-objects/src/test/java/com/fasterxml/jackson/jr/ob/PrimitiveArrayTest.java @@ -141,15 +141,8 @@ public void testFloatArrayWrite() throws Exception { @Test public void testDoubleArrayRead() throws Exception { - //assertArrayEquals(DOUBLE_ARRAY, JSON.std.beanFrom(double[].class, DOUBLE_ARRAY_JSON), - // 0.00001); - - try { - JSON.std.beanFrom(double[].class, DOUBLE_ARRAY_JSON); - fail("Should not pass"); - } catch (JSONObjectException e) { - verifyException(e, "Deserialization of `double[]` not yet supported"); - } + assertArrayEquals(DOUBLE_ARRAY, JSON.std.beanFrom(double[].class, DOUBLE_ARRAY_JSON), + 0.00001); } @Test @@ -157,24 +150,38 @@ public void testDoubleArrayWrite() throws Exception { assertEquals(DOUBLE_ARRAY_JSON, JSON.std.asString(DOUBLE_ARRAY)); } - // Test empty arrays - // Not yet implemented in Jackson-jr + // Test empty arrays, success cases @Test public void testEmptyArrays() throws Exception { assertArrayEquals(new char[0], JSON.std.beanFrom(char[].class, "\"\"")); assertArrayEquals(new int[0], JSON.std.beanFrom(int[].class, "[]")); assertArrayEquals(new long[0], JSON.std.beanFrom(long[].class, "[]")); + assertArrayEquals(new double[0], JSON.std.beanFrom(double[].class, "[]"), 0.0); } - // Not yet implemented in Jackson-jr + // Empty arrays: Not yet implemented in Jackson-jr @JacksonTestFailureExpected @Test - public void testEmptyArraysFailing() throws Exception { + public void testEmptyArraysFailingBooleanArray() throws Exception { assertArrayEquals(new boolean[0], JSON.std.beanFrom(boolean[].class, "[]")); + } + + @JacksonTestFailureExpected + @Test + public void testEmptyArraysFailingByteArray() throws Exception { assertArrayEquals(new byte[0], JSON.std.beanFrom(byte[].class, "[]")); + } + + @JacksonTestFailureExpected + @Test + public void testEmptyArraysFailingShortArray() throws Exception { assertArrayEquals(new short[0], JSON.std.beanFrom(short[].class, "[]")); + } + + @JacksonTestFailureExpected + @Test + public void testEmptyArraysFailingFloatArray() throws Exception { assertArrayEquals(new float[0], JSON.std.beanFrom(float[].class, "[]"), 0.0f); - assertArrayEquals(new double[0], JSON.std.beanFrom(double[].class, "[]"), 0.0); } // Not yet fully implemented in Jackson-jr diff --git a/release-notes/VERSION-2.x b/release-notes/VERSION-2.x index 08cf9aed..4b3094f8 100644 --- a/release-notes/VERSION-2.x +++ b/release-notes/VERSION-2.x @@ -13,7 +13,7 @@ Modules: 2.21.0 (not yet released) -No changes since 2.20 +#208: Support reading of `double[]` 2.20.0 (28-Aug-2025)