Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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) {
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,40 +141,47 @@ 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
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
Expand Down
2 changes: 1 addition & 1 deletion release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down