Closed ctrychta closed 2 years ago
I made a few changes but it looks ok to me:
package net.openhft.chronicle.wire;
import net.openhft.chronicle.bytes.Bytes;
import net.openhft.chronicle.core.pool.ClassAliasPool;
import org.junit.Assert;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
public class JSONEmptySequences {
@Test
public void emptySequence() {
ClassAliasPool.CLASS_ALIASES.addAlias(Foo.class);
final Bytes data = Bytes.elasticByteBuffer();
data.append("!Foo {\n" +
" field1: 1234,\n" +
" field2: 456,\n" +
" field3: [ ],\n" +
" field4: [\n" +
" abc,\n" +
" xyz\n" +
" ]\n" +
"}");
final JSONWire wire = new JSONWire(data);
final Foo f = new Foo();
wire.getValueIn().object(f, Foo.class);
Assert.assertEquals("!Foo {\n" +
" field1: 1234,\n" +
" field2: 456,\n" +
" field3: [ ],\n" +
" field4: [\n" +
" abc,\n" +
" xyz\n" +
" ]\n" +
"}\n", f.toString());
}
private static final class Foo extends SelfDescribingMarshallable {
int field1;
int field2;
final List<String> field3 = new ArrayList<>();
final List<String> field4 = new ArrayList<>();
}
}
or if you want to put quotes around the field name, like this
package net.openhft.chronicle.wire;
import net.openhft.chronicle.bytes.Bytes;
import net.openhft.chronicle.core.pool.ClassAliasPool;
import org.junit.Assert;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class JSONEmptySequences {
@Test
public void emptySequence() {
ClassAliasPool.CLASS_ALIASES.addAlias(Foo.class);
final Bytes data = Bytes.elasticByteBuffer();
data.append("!Foo {\"field1\":1234,\"field2\":456,\"field3\":[ ],\"field4\":[\"abc\",\"xyz\" ]}");
final Wire wire = WireType.JSON.apply(data);
Foo f = (Foo) wire.getValueIn().object();
Assert.assertEquals("!Foo {\n" +
" field1: 1234,\n" +
" field2: 456,\n" +
" field3: [ ],\n" +
" field4: [\n" +
" abc,\n" +
" xyz\n" +
" ]\n" +
"}\n", f.toString());
}
private static final class Foo extends SelfDescribingMarshallable {
int field1;
int field2;
final List<String> field3 = new ArrayList<>();
final List<String> field4 = new ArrayList<>();
}
}
I can reproduce the issue - it appears to be trickier and needing a wire.read().sequence()
with handler
When using
sequence
to read fields I've found that it sometime does not correctly handle the case where a list is empty, which can lead to subsequent fields holding incorrect data.For example this json:
may be parsed as:
This seems to be due to not consuming the comma after the empty sequence. Below is a code snippet showing this behavior. You can see the difference in behavior when uncommenting
((JSONWire) wire).consumePadding(1);
.