Difference building JSON object with Yasson and Johnzon

301 Views Asked by At

I have the following code using the JSON Processing API (descriptor is the interface I'm trying to serialize).

  public void serialize(ComponentDescriptor descriptor, JsonGenerator generator, SerializationContext ctx) {
    generator.writeStartObject();
    generator.writeStartObject(descriptor.getClass().getName());
    generator.write("name", descriptor.getName());
    ctx.serialize("settings", descriptor.getSettings(), generator);
    generator.writeEnd();
    generator.writeEnd();
  }

This works fine with Yasson, but if I try to serialize the same object with Johnzon I get:

javax.json.stream.JsonGenerationException: state START_OBJECT does not accept a value

There's clearly a difference between the two implementations (which I'm guessing there shouldn't be), but what is the correct way to write my object (ideally so it works in both)?

The sort of output I'm expecting is:

{
  "my.package.TestDescriptor": {
    "name": "Test",
    "settings": {
      "host": "localhost",
      "port":8080
    }
  }
}
1

There are 1 best solutions below

0
Romain Manni-Bucau On

Code looks ok Maybe give a try to johnzon 1.2.1 which got released today and complies to JSON-B tests. If it does nlt work a workaround cna be to serialize a map instead of using the generator manually, not sexy but portable.