Problems with the BSON serializer

Hi,

I’ve been trying to use the BSON serializer and I ran into a problem when using an event which contains a Set/List with more than one element. I’ve been able to reproduce the problem in a test (in DBObjectXStreamSerializerTest):

@Test
public void testSerializeAndDeserializeDomainEventWithSetOfStrings() {
Set<String> strings = new HashSet<String>();
strings.add("a");
strings.add("b");
SerializedObject<byte[]> serializedEvent = testSubject.serialize(new SecondTestEvent(strings), byte[].class);

Object actualResult = testSubject.deserialize(serializedEvent);
assertTrue(actualResult instanceof TestEvent);
TestEvent actualEvent = (TestEvent) actualResult;
assertEquals(strings, actualEvent.getStrings());
}

public static class SecondTestEvent {
private Set<String> strings;

public SecondTestEvent(Set<String> strings) {
this.strings = new HashSet<String>(strings);
}

public Set<String> getStrings() {
return strings;
}
}

As I am not familiar with XStream, it proved difficult for me to pinpoint exactly how to fix the problem. Maybe someone on the list who is more familiar with XStream can figure it out more easily ?

Thank you,

Hi Nicolas,

the BSON serializer seems to contain a bug that prevents this. I wasn’t very happy with the structure it generates anyway. I’ll need to rewrite that part. I’ve already got an idea on how to do it, the issue is to find the time for it.

Will be continued.
Cheers,

Allard

Hi Allard,

I might try to implement a JSON serializer using Jackson in the near future. As far as I know, it’ll still be possible to store the result in BSON in Mongo, since Mongo has a JSON parser, but maybe I’m wrong on this one. The caveat is, I guess it’ll be slower than the BSON serializer.

Thank you,

Hi Nicolas,

yesterday I managed to fix the issues using a BSON writer for xstream. It can now write all sorts of structures.
The resulting BSON might not aleays be ideal, but that’s because Xstream expexts certain elements to be orderd, and JSON only supports ordering in its arrays.

Code hasn’t been pushed yet. I’ll need to add some javadoc here and there.

By the way, Jackson, afaik, requires getters and setters. The latter make your events really ugly. On the other hand, the JSON that comes out is a lot cleaner.

Cheers,

Allard

Hi Allard,

I re-tried the BSON serializer today, and everything seems to be fine.

Thank you !