Skip to content

Commit 160a072

Browse files
authored
Using Stream Builder to deserialization of int[] (#125)
1 parent 63b5991 commit 160a072

File tree

7 files changed

+79
-36
lines changed

7 files changed

+79
-36
lines changed

jr-objects/src/main/java/com/fasterxml/jackson/jr/ob/impl/SimpleValueReader.java

+39-7
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
11
package com.fasterxml.jackson.jr.ob.impl;
22

3-
import static com.fasterxml.jackson.jr.ob.impl.ValueWriterLocator.*;
4-
53
import java.io.File;
64
import java.io.IOException;
75
import java.net.URI;
86
import java.net.URL;
97
import java.nio.file.Path;
108
import java.nio.file.Paths;
11-
import java.util.*;
9+
import java.util.Calendar;
10+
import java.util.Date;
11+
import java.util.UUID;
12+
import java.util.stream.IntStream;
1213

1314
import com.fasterxml.jackson.core.JsonParser;
1415
import com.fasterxml.jackson.core.JsonToken;
1516
import com.fasterxml.jackson.core.JsonTokenId;
1617
import com.fasterxml.jackson.jr.ob.JSONObjectException;
1718
import com.fasterxml.jackson.jr.ob.api.ValueReader;
1819

20+
import static com.fasterxml.jackson.jr.ob.impl.ValueWriterLocator.*;
21+
1922
/**
2023
* Default {@link ValueReader} used for simple scalar types and related,
2124
* not including POJO-, {@link java.util.Map} and {@link java.util.Collection}
2225
* types.
2326
*/
2427
public class SimpleValueReader extends ValueReader
2528
{
29+
private final static int[] NO_INTS = new int[0];
30+
2631
protected final int _typeId;
2732

2833
public SimpleValueReader(Class<?> raw, int typeId) {
@@ -290,10 +295,37 @@ protected Path _readPath(JsonParser p) throws IOException {
290295
}
291296
}
292297

293-
protected int[] _readIntArray(JsonParser p) throws IOException
294-
{
295-
// !!! TODO
296-
throw new JSONObjectException("Reading of int[] not yet implemented");
298+
protected int[] _readIntArray(JsonParser p) throws IOException {
299+
if (JsonToken.START_ARRAY.equals(p.currentToken())) {
300+
p.nextToken();
301+
}
302+
303+
final IntStream.Builder builder = IntStream.builder();
304+
int t = p.currentTokenId();
305+
306+
// Tiny optimization
307+
if (t == JsonTokenId.ID_END_ARRAY) {
308+
return NO_INTS;
309+
}
310+
311+
main_loop:
312+
while (true) {
313+
switch (t) {
314+
case JsonTokenId.ID_NUMBER_FLOAT:
315+
case JsonTokenId.ID_NUMBER_INT:
316+
case JsonTokenId.ID_NULL:
317+
builder.add(p.getValueAsInt());
318+
break;
319+
case JsonTokenId.ID_END_ARRAY:
320+
break main_loop;
321+
default:
322+
throw new JSONObjectException("Failed to bind `int` element if `int[]` from value: "+
323+
_tokenDesc(p));
324+
}
325+
p.nextToken();
326+
t = p.currentTokenId();
327+
}
328+
return builder.build().toArray();
297329
}
298330

299331
protected long _fetchLong(JsonParser p) throws IOException

jr-objects/src/test/java/com/fasterxml/jackson/jr/failing/ReadIntArray7Test.java

-17
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.fasterxml.jackson.jr.ob;
2+
3+
import static org.junit.Assert.assertArrayEquals;
4+
5+
public class ReadIntArray7Test extends TestBase {
6+
public void testReadIntArray() throws Exception {
7+
final int[] input = new int[]{1, 2, 3, 25, 999};
8+
String json = JSON.std.asString(input);
9+
int[] result = JSON.std.beanFrom(int[].class, json);
10+
assertArrayEquals(input, result);
11+
}
12+
13+
public void testReadIntArray2() throws Exception {
14+
final int[][] input = new int[][]{{1, 2, 3, 25, 999},{456,678,789},{1},{},{1000,2000,3000}};
15+
String json = JSON.std.asString(input);
16+
int[][] result = JSON.std.beanFrom(int[][].class, json);
17+
assertArrayEquals(input, result);
18+
}
19+
20+
public void testReadIntArray3() throws Exception {
21+
final int[][][] input = new int[][][]{{{1, 2, 3, 25, 999},{6,7,3}},{{456}, {678, 789}},{},{{},{23}},{{}}};
22+
String json = JSON.std.asString(input);
23+
int[][][] result = JSON.std.beanFrom(int[][][].class, json);
24+
assertArrayEquals(input, result);
25+
}
26+
27+
public void testReadIntArrayWhenEmpty() throws Exception {
28+
final int[][][] input = new int[][][]{};
29+
String json = JSON.std.asString(input);
30+
int[][][] result = JSON.std.beanFrom(int[][][].class, json);
31+
assertArrayEquals(input, result);
32+
}
33+
}

jr-objects/src/test/java/com/fasterxml/jackson/jr/ob/ReadSimpleTest.java

-10
Original file line numberDiff line numberDiff line change
@@ -343,16 +343,6 @@ public void testTreeNodeCreationWithoutCodec() throws Exception {
343343
}
344344
}
345345

346-
// not yet supported (but probably should)
347-
public void testIntArray() throws Exception {
348-
try {
349-
JSON.std.beanFrom(IntArrayWrapper.class, "{\"value\":[ 3 ]}");
350-
fail("Should not pass");
351-
} catch (JSONObjectException e) {
352-
verifyException(e, "not yet implemented");
353-
}
354-
}
355-
356346
public void testInvalidSource() throws Exception {
357347
try {
358348
JSON.std.beanFrom(Object.class, Long.valueOf(67));

jr-objects/src/test/java/com/fasterxml/jackson/jr/ob/ReadWithCtors25Test.java

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.fasterxml.jackson.jr.ob;
22

3-
import com.fasterxml.jackson.jr.ob.JSON;
4-
53
// for [jackson-jr#25], allowing single-int constructors
64
public class ReadWithCtors25Test extends TestBase
75
{

release-notes/CREDITS-2.x

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ Julian Honnen (@jhonnen)
4848

4949
@Shounaks
5050

51+
* Contributed #7: Support deserialization of int[]
52+
(2.17.0)
5153
* Contributed PoC of #25: Add support single-int Constructors
5254
(2.17.0)
5355
* Contributed fix for #93: Skip serialization of `groovy.lang.MetaClass` values

release-notes/VERSION-2.x

+5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ Modules:
1111
=== Releases ===
1212
------------------------------------------------------------------------
1313

14+
Not yet released
15+
16+
#7: Support deserialization of `int[]`
17+
(contributed by @Shounaks)
18+
1419
2.17.0-rc1 (26-Feb-2024)
1520

1621
#25: Add support single-int Constructors

0 commit comments

Comments
 (0)