Skip to content
Open
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 @@ -28,9 +28,11 @@ public class SimpleValueReader extends ValueReader
{
private final static int[] NO_INTS = new int[0];
private final static long[] NO_LONGS = new long[0];

private final static boolean[] NO_BOOLEANS = new boolean[0];
private final static short[] NO_SHORTS = new short[0];
private final static float[] NO_FLOATS = new float[0];
// @since 2.21
private final static double[] NO_DOUBLES = new double[0];
private final static double[] NO_DOUBLES = new double[0];

protected final int _typeId;

Expand Down Expand Up @@ -116,13 +118,12 @@ public Object read(JSONReader reader, JsonParser p) throws IOException
return _readIntArray(p);
case SER_LONG_ARRAY:
return _readLongArray(p);

// Not yet supported:
case SER_BOOLEAN_ARRAY:
return _readBooleanArray(p);
case SER_SHORT_ARRAY:
return _readShortArray(p);
case SER_FLOAT_ARRAY:
throw JSONObjectException.from(p,
"Deserialization of `"+_valueTypeDesc()+"` not yet supported");
return _readFloatArray(p);
case SER_DOUBLE_ARRAY:
return _readDoubleArray(p);

Expand Down Expand Up @@ -394,6 +395,118 @@ 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 boolean[] _readBooleanArray(JsonParser p) throws IOException {
if (JsonToken.START_ARRAY.equals(p.currentToken())) {
p.nextToken();
}

List<Boolean> values = new ArrayList<>();
int t = p.currentTokenId();

if (t == JsonTokenId.ID_END_ARRAY) {
return NO_BOOLEANS;
}

main_loop:
while (true) {
switch (t) {
case JsonTokenId.ID_TRUE:
values.add(Boolean.TRUE);
break;
case JsonTokenId.ID_FALSE:
values.add(Boolean.FALSE);
break;
case JsonTokenId.ID_NULL:
values.add(Boolean.FALSE);
break;
case JsonTokenId.ID_END_ARRAY:
break main_loop;
default:
throw new JSONObjectException("Failed to bind `boolean` element of `boolean[]` from value: "+
_tokenDesc(p));
}
p.nextToken();
t = p.currentTokenId();
}
boolean[] result = new boolean[values.size()];
for (int i = 0; i < result.length; i++) {
result[i] = values.get(i);
}
return result;
}

protected short[] _readShortArray(JsonParser p) throws IOException {
if (JsonToken.START_ARRAY.equals(p.currentToken())) {
p.nextToken();
}

List<Short> values = new ArrayList<>();
int t = p.currentTokenId();

if (t == JsonTokenId.ID_END_ARRAY) {
return NO_SHORTS;
}

main_loop:
while (true) {
switch (t) {
case JsonTokenId.ID_NUMBER_FLOAT:
case JsonTokenId.ID_NUMBER_INT:
case JsonTokenId.ID_NULL:
values.add((short) p.getValueAsInt());
break;
case JsonTokenId.ID_END_ARRAY:
break main_loop;
default:
throw new JSONObjectException("Failed to bind `short` element of `short[]` from value: "+
_tokenDesc(p));
}
p.nextToken();
t = p.currentTokenId();
}
short[] result = new short[values.size()];
for (int i = 0; i < result.length; i++) {
result[i] = values.get(i);
}
return result;
}

protected float[] _readFloatArray(JsonParser p) throws IOException {
if (JsonToken.START_ARRAY.equals(p.currentToken())) {
p.nextToken();
}

List<Float> values = new ArrayList<>();
int t = p.currentTokenId();

if (t == JsonTokenId.ID_END_ARRAY) {
return NO_FLOATS;
}

main_loop:
while (true) {
switch (t) {
case JsonTokenId.ID_NUMBER_FLOAT:
case JsonTokenId.ID_NUMBER_INT:
case JsonTokenId.ID_NULL:
values.add((float) p.getValueAsDouble());
break;
case JsonTokenId.ID_END_ARRAY:
break main_loop;
default:
throw new JSONObjectException("Failed to bind `float` element of `float[]` from value: "+
_tokenDesc(p));
}
p.nextToken();
t = p.currentTokenId();
}
float[] result = new float[values.size()];
for (int i = 0; i < result.length; i++) {
result[i] = values.get(i);
}
return result;
}

protected double[] _readDoubleArray(JsonParser p) throws IOException {
if (JsonToken.START_ARRAY.equals(p.currentToken())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ static class AllArraysBean {
private final static boolean[] BOOLEAN_ARRAY = new boolean[] { true, false, true, false, true };

// Not yet implemented in Jackson-jr
@JacksonTestFailureExpected
@Test
public void testBooleanArrayRead() throws Exception {
assertArrayEquals(BOOLEAN_ARRAY, JSON.std.beanFrom(boolean[].class, BOOLEAN_ARRAY_JSON));
Expand Down Expand Up @@ -74,13 +73,7 @@ public void testCharArray() throws Exception {
// Not yet implemented in Jackson-jr
@Test
public void testShortArrayRead() throws Exception {
// assertArrayEquals(SHORT_ARRAY, JSON.std.beanFrom(short[].class, SHORT_ARRAY_JSON));
try {
JSON.std.beanFrom(short[].class, SHORT_ARRAY_JSON);
fail("Should not pass");
} catch (JSONObjectException e) {
verifyException(e, "Deserialization of `short[]` not yet supported");
}
assertArrayEquals(SHORT_ARRAY, JSON.std.beanFrom(short[].class, SHORT_ARRAY_JSON));
}

@Test
Expand Down Expand Up @@ -120,15 +113,8 @@ public void testLongArrayWrite() throws Exception {
// Not yet implemented in Jackson-jr
@Test
public void testFloatArrayRead() throws Exception {
//assertArrayEquals(FLOAT_ARRAY, JSON.std.beanFrom(float[].class, FLOAT_ARRAY_JSON),
// 0.00001f);

try {
JSON.std.beanFrom(float[].class, FLOAT_ARRAY_JSON);
fail("Should not pass");
} catch (JSONObjectException e) {
verifyException(e, "Deserialization of `float[]` not yet supported");
}
assertArrayEquals(FLOAT_ARRAY, JSON.std.beanFrom(float[].class, FLOAT_ARRAY_JSON),
0.00001f);
}

@Test
Expand Down Expand Up @@ -156,14 +142,10 @@ 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);
}

// Empty arrays: Not yet implemented in Jackson-jr
@JacksonTestFailureExpected
@Test
public void testEmptyArraysFailingBooleanArray() throws Exception {
assertArrayEquals(new boolean[0], JSON.std.beanFrom(boolean[].class, "[]"));
assertArrayEquals(new short[0], JSON.std.beanFrom(short[].class, "[]"));
assertArrayEquals(new float[0], JSON.std.beanFrom(float[].class, "[]"), 0.0f);
assertArrayEquals(new double[0], JSON.std.beanFrom(double[].class, "[]"), 0.0);
}

@JacksonTestFailureExpected
Expand All @@ -172,20 +154,6 @@ 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);
}

// Not yet fully implemented in Jackson-jr
@JacksonTestFailureExpected
@Test
public void testArraysAsObjectFields() throws Exception {
AllArraysBean input = new AllArraysBean();
Expand Down