0

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
    }
  }
}
James Baker
  • 1,143
  • 17
  • 39
  • 1
    Should have added, the exception is being thrown on the second line: generator.writeStartObject(descriptor.getClass().getName()); – James Baker Sep 18 '19 at 10:04

1 Answers1

0

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.

Romain Manni-Bucau
  • 3,354
  • 1
  • 16
  • 13