nulls silently dropped even when using "protostuff.runtime.allow_null_array_element"
See original GitHub issueI don´t understand why these two produce different results.
static final class Foo
{
Object o;
Foo(Object o)
{
this.o = o;
}
}
public static void main(String[] args)
{
System.setProperty("protostuff.runtime.allow_null_array_element", "true");
System.out.println("Serializing an ArrayList<String>:");
roundTrip(true);
System.out.println("\nSerializing a String[]:");
roundTrip(false);
}
static void roundTrip(boolean bug)
{
LinkedBuffer buffer = LinkedBuffer.allocate(512);
Foo bef = bug ? new Foo(new ArrayList(Arrays.asList("1", null, "2"))) : new Foo(new String[]{"1", null, "2"});
Schema<Foo> schema = RuntimeSchema.getSchema(Foo.class);
byte[] bytes = ProtostuffIOUtil.toByteArray(bef, schema, buffer);
buffer.clear();
Foo aft = schema.newMessage();
ProtostuffIOUtil.mergeFrom(bytes, aft, schema);
if (bug) {
System.out.println("Before: " + bef.o);
System.out.println("After : " + aft.o);
System.out.println("Set : " + RuntimeEnv.ALLOW_NULL_ARRAY_ELEMENT);
} else {
System.out.println("Before: " + Arrays.toString((String[]) bef.o));
System.out.println("After : " + Arrays.toString((String[]) aft.o));
System.out.println("Set : " + RuntimeEnv.ALLOW_NULL_ARRAY_ELEMENT);
}
}
The output is:
Serializing an ArrayList<String>: Before: [1, null, 2] After : [1, 2] Set : true
Serializing a String[]: Before: [1, null, 2] After : [1, null, 2] Set : true
Notice how the null is silently dropped in the first output.
I have tried this with 1.6.2 and 1.7.2
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
No results found
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Tried both cases i posted above with 1.8.0-SNAPSHOT with the new flag and they both now work just as I expect them would. Thanks, and good job!
Fixed in master. You can currently test with 1.8.0-SNAPSHOT Use:
-Dprotostuff.runtime.preserve_null_elements=true
It applies to both collections and arrays