Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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 @@ -191,6 +191,15 @@ public void writeField(String fieldName, Object value, int type) throws IOExcept
case SER_BOOLEAN_ARRAY:
writeBooleanArrayField(fieldName, (boolean[]) value);
return;
case SER_SHORT_ARRAY:
writeShortArrayField(fieldName, (short[]) value);
return;
case SER_FLOAT_ARRAY:
writeFloatArrayField(fieldName, (float[]) value);
return;
case SER_DOUBLE_ARRAY:
writeDoubleArrayField(fieldName, (double[]) value);
return;
case SER_TREE_NODE:
writeTreeNodeField(fieldName, (TreeNode) value);
return;
Expand Down Expand Up @@ -319,6 +328,15 @@ protected void _writeValue(Object value, int type) throws IOException
case SER_BOOLEAN_ARRAY:
writeBooleanArrayValue((boolean[]) value);
return;
case SER_SHORT_ARRAY:
writeShortArrayValue((short[]) value);
return;
case SER_FLOAT_ARRAY:
writeFloatArrayValue((float[]) value);
return;
case SER_DOUBLE_ARRAY:
writeDoubleArrayValue((double[]) value);
return;
case SER_TREE_NODE:
writeTreeNodeValue((TreeNode) value);
return;
Expand Down Expand Up @@ -555,6 +573,45 @@ protected void writeBooleanArrayField(String fieldName, boolean[] v) throws IOEx
writeBooleanArrayValue(v);
}

protected void writeShortArrayValue(short[] v) throws IOException {
_generator.writeStartArray();
for (int i = 0, len = v.length; i < len; ++i) {
_generator.writeNumber(v[i]);
}
_generator.writeEndArray();
}

protected void writeShortArrayField(String fieldName, short[] v) throws IOException {
_generator.writeFieldName(fieldName);
writeShortArrayValue(v);
}

protected void writeFloatArrayValue(float[] v) throws IOException {
_generator.writeStartArray();
for (int i = 0, len = v.length; i < len; ++i) {
_generator.writeNumber(v[i]);
}
_generator.writeEndArray();
}

protected void writeFloatArrayField(String fieldName, float[] v) throws IOException {
_generator.writeFieldName(fieldName);
writeFloatArrayValue(v);
}

protected void writeDoubleArrayValue(double[] v) throws IOException {
_generator.writeStartArray();
for (int i = 0, len = v.length; i < len; ++i) {
_generator.writeNumber(v[i]);
}
_generator.writeEndArray();
}

protected void writeDoubleArrayField(String fieldName, double[] v) throws IOException {
_generator.writeFieldName(fieldName);
writeDoubleArrayValue(v);
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class looks good.

protected void writeTreeNodeValue(TreeNode v) throws IOException {
if (_treeCodec == null) {
throw new JSONObjectException("No `TreeCodec` configured: can not serialize `TreeNode` values");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,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];
private final static double[] NO_DOUBLES = new double[0];

protected final int _typeId;

Expand Down Expand Up @@ -110,6 +115,16 @@ public Object read(JSONReader reader, JsonParser p) throws IOException

case SER_INT_ARRAY:
return _readIntArray(p);
case SER_LONG_ARRAY:
return _readLongArray(p);
case SER_BOOLEAN_ARRAY:
return _readBooleanArray(p);
case SER_SHORT_ARRAY:
return _readShortArray(p);
case SER_FLOAT_ARRAY:
return _readFloatArray(p);
case SER_DOUBLE_ARRAY:
return _readDoubleArray(p);

case SER_TREE_NODE:
return reader.readTree();
Expand Down Expand Up @@ -328,6 +343,190 @@ protected int[] _readIntArray(JsonParser p) throws IOException {
return builder.build().toArray();
}

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

java.util.List<Long> values = new java.util.ArrayList<>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very inefficient, no. Have a look at how int[] is handled.

int t = p.currentTokenId();

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

main_loop:
while (true) {
switch (t) {
case JsonTokenId.ID_NUMBER_FLOAT:
case JsonTokenId.ID_NUMBER_INT:
case JsonTokenId.ID_NULL:
values.add(p.getValueAsLong());
break;
case JsonTokenId.ID_END_ARRAY:
break main_loop;
default:
throw new JSONObjectException("Failed to bind `long` element if `long[]` from value: "+
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Funny. Copied typo from int[] case :)

("if" -> "of")

(will fix separately)

_tokenDesc(p));
}
p.nextToken();
t = p.currentTokenId();
}
long[] result = new long[values.size()];
for (int i = 0; i < result.length; i++) {
result[i] = values.get(i);
}
return result;
}

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

java.util.List<Boolean> values = new java.util.ArrayList<>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above, need to build an array to avoid all the boxing, unboxing.

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 if `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();
}

java.util.List<Short> values = new java.util.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 if `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();
}

java.util.List<Float> values = new java.util.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 if `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())) {
p.nextToken();
}

java.util.List<Double> values = new java.util.ArrayList<>();
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:
values.add(p.getValueAsDouble());
break;
case JsonTokenId.ID_END_ARRAY:
break main_loop;
default:
throw new JSONObjectException("Failed to bind `double` element if `double[]` from value: "+
_tokenDesc(p));
}
p.nextToken();
t = p.currentTokenId();
}
double[] result = new double[values.size()];
for (int i = 0; i < result.length; i++) {
result[i] = values.get(i);
}
return result;
}

protected long _fetchLong(JsonParser p) throws IOException
{
JsonToken t = p.currentToken();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ public abstract class ValueLocatorBase
public final static int SER_INT_ARRAY = 5;
public final static int SER_LONG_ARRAY = 6;
public final static int SER_BOOLEAN_ARRAY = 7;
public final static int SER_SHORT_ARRAY = 38;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs to be consecutive.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... which gets tricky wrt whether to renumber entries or move these to later.

I think renumbering is needed, so move SER_TREE_NODE etc up by number of added entries needed.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I saw that, and had the same thought, but I figured you'd know which of the two options (renumbering all later constants, or moving the other arrays to the end, which is ugly) would make the most sense.

public final static int SER_FLOAT_ARRAY = 39;
public final static int SER_DOUBLE_ARRAY = 40;

/**
* An implementation of {@link com.fasterxml.jackson.core.TreeNode}
Expand Down Expand Up @@ -159,6 +162,15 @@ protected int _findSimpleType(Class<?> raw, boolean forSer)
if (raw == boolean[].class) {
return SER_BOOLEAN_ARRAY;
}
if (raw == short[].class) {
return SER_SHORT_ARRAY;
}
if (raw == float[].class) {
return SER_FLOAT_ARRAY;
}
if (raw == double[].class) {
return SER_DOUBLE_ARRAY;
}
// Hmmh. Could support all types; add as/when needed
return SER_UNKNOWN;
}
Expand Down
Loading
Loading