Skip to content

Commit

Permalink
Using Stream Builder to deserialization of int[] (FasterXML#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shounaks authored Feb 29, 2024
1 parent 63b5991 commit 160a072
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
package com.fasterxml.jackson.jr.ob.impl;

import static com.fasterxml.jackson.jr.ob.impl.ValueWriterLocator.*;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;
import java.util.stream.IntStream;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.core.JsonTokenId;
import com.fasterxml.jackson.jr.ob.JSONObjectException;
import com.fasterxml.jackson.jr.ob.api.ValueReader;

import static com.fasterxml.jackson.jr.ob.impl.ValueWriterLocator.*;

/**
* Default {@link ValueReader} used for simple scalar types and related,
* not including POJO-, {@link java.util.Map} and {@link java.util.Collection}
* types.
*/
public class SimpleValueReader extends ValueReader
{
private final static int[] NO_INTS = new int[0];

protected final int _typeId;

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

protected int[] _readIntArray(JsonParser p) throws IOException
{
// !!! TODO
throw new JSONObjectException("Reading of int[] not yet implemented");
protected int[] _readIntArray(JsonParser p) throws IOException {
if (JsonToken.START_ARRAY.equals(p.currentToken())) {
p.nextToken();
}

final IntStream.Builder builder = IntStream.builder();
int t = p.currentTokenId();

// Tiny optimization
if (t == JsonTokenId.ID_END_ARRAY) {
return NO_INTS;
}

main_loop:
while (true) {
switch (t) {
case JsonTokenId.ID_NUMBER_FLOAT:
case JsonTokenId.ID_NUMBER_INT:
case JsonTokenId.ID_NULL:
builder.add(p.getValueAsInt());
break;
case JsonTokenId.ID_END_ARRAY:
break main_loop;
default:
throw new JSONObjectException("Failed to bind `int` element if `int[]` from value: "+
_tokenDesc(p));
}
p.nextToken();
t = p.currentTokenId();
}
return builder.build().toArray();
}

protected long _fetchLong(JsonParser p) throws IOException
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.fasterxml.jackson.jr.ob;

import static org.junit.Assert.assertArrayEquals;

public class ReadIntArray7Test extends TestBase {
public void testReadIntArray() throws Exception {
final int[] input = new int[]{1, 2, 3, 25, 999};
String json = JSON.std.asString(input);
int[] result = JSON.std.beanFrom(int[].class, json);
assertArrayEquals(input, result);
}

public void testReadIntArray2() throws Exception {
final int[][] input = new int[][]{{1, 2, 3, 25, 999},{456,678,789},{1},{},{1000,2000,3000}};
String json = JSON.std.asString(input);
int[][] result = JSON.std.beanFrom(int[][].class, json);
assertArrayEquals(input, result);
}

public void testReadIntArray3() throws Exception {
final int[][][] input = new int[][][]{{{1, 2, 3, 25, 999},{6,7,3}},{{456}, {678, 789}},{},{{},{23}},{{}}};
String json = JSON.std.asString(input);
int[][][] result = JSON.std.beanFrom(int[][][].class, json);
assertArrayEquals(input, result);
}

public void testReadIntArrayWhenEmpty() throws Exception {
final int[][][] input = new int[][][]{};
String json = JSON.std.asString(input);
int[][][] result = JSON.std.beanFrom(int[][][].class, json);
assertArrayEquals(input, result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -343,16 +343,6 @@ public void testTreeNodeCreationWithoutCodec() throws Exception {
}
}

// not yet supported (but probably should)
public void testIntArray() throws Exception {
try {
JSON.std.beanFrom(IntArrayWrapper.class, "{\"value\":[ 3 ]}");
fail("Should not pass");
} catch (JSONObjectException e) {
verifyException(e, "not yet implemented");
}
}

public void testInvalidSource() throws Exception {
try {
JSON.std.beanFrom(Object.class, Long.valueOf(67));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.fasterxml.jackson.jr.ob;

import com.fasterxml.jackson.jr.ob.JSON;

// for [jackson-jr#25], allowing single-int constructors
public class ReadWithCtors25Test extends TestBase
{
Expand Down
2 changes: 2 additions & 0 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ Julian Honnen (@jhonnen)

@Shounaks

* Contributed #7: Support deserialization of int[]
(2.17.0)
* Contributed PoC of #25: Add support single-int Constructors
(2.17.0)
* Contributed fix for #93: Skip serialization of `groovy.lang.MetaClass` values
Expand Down
5 changes: 5 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ Modules:
=== Releases ===
------------------------------------------------------------------------

Not yet released

#7: Support deserialization of `int[]`
(contributed by @Shounaks)

2.17.0-rc1 (26-Feb-2024)

#25: Add support single-int Constructors
Expand Down

0 comments on commit 160a072

Please sign in to comment.