Trevor Kerby created JOHNZON-335:
------------------------------------

             Summary: JsonGeneratorImpl.prepareValue throws 
JsonGenerationException when JsonGenerator.write is called from a nested 
JsonbSerializer
                 Key: JOHNZON-335
                 URL: https://issues.apache.org/jira/browse/JOHNZON-335
             Project: Johnzon
          Issue Type: Bug
          Components: JSON-B
    Affects Versions: 1.2.10, 1.2.9
            Reporter: Trevor Kerby
         Attachments: JohnzonNestedSerializerTest.java

When using two JsonbSerializers registered in JsonbConfig, with one serializer 
- the _outer serializer_ - calling SerializationContext.serialize on an object 
annotated to be serialized with the other serializer - the _inner serializer_ - 
Johnzon fails to write the expected json and throws a JsonGenerationException. 
This seems to happen upon any JsonGenerator.write method called inside the 
_inner serializer_.


{code:java}
        public static class OuterTestSerializer implements 
JsonbSerializer<OuterTestModel> {
                @Override
                public void serialize(OuterTestModel obj, JsonGenerator 
generator, SerializationContext ctx) {
                        generator.writeStartObject();
                        generator.write("foo", "generated in outer serializer");
                        ctx.serialize("inner", new InnerTestModel(), generator);
                        generator.writeEnd();
                }
        }

        public static class InnerTestSerializer implements 
JsonbSerializer<InnerTestModel> {
                @Override
                public void serialize(InnerTestModel obj, JsonGenerator 
generator, SerializationContext ctx) {
                        generator.writeStartObject();
                        generator.write("bar", "generated in inner serializer");
                        generator.writeEnd();
                }
        }
{code}

Below is the formatted version of the json that's expected

{code:json}
{
    "foo": "generated in outer serializer",
    "inner": 
    {
        "bar": "generated in inner serializer"
    }
}
{code}

However Johnzon throws this expection

{code}
javax.json.stream.JsonGenerationException: state IN_OBJECT does not accept a 
value
        at 
org.apache.johnzon.core.JsonGeneratorImpl.prepareValue(JsonGeneratorImpl.java:643)
        at 
org.apache.johnzon.core.JsonGeneratorImpl.writeStartObject(JsonGeneratorImpl.java:137)
        at 
org.apache.johnzon.mapper.DynamicMappingGenerator$InObjectOrPrimitiveJsonGenerator.writeStartObject(DynamicMappingGenerator.java:109)
        at 
org.apache.johnzon.mapper.DynamicMappingGenerator$InObjectOrPrimitiveJsonGenerator.ensureStart(DynamicMappingGenerator.java:99)
        at 
org.apache.johnzon.mapper.DynamicMappingGenerator$InObjectOrPrimitiveJsonGenerator.writeStartObject(DynamicMappingGenerator.java:106)
        at 
usa.kerby.tk.jhal.JohnzonNestedSerializerTest$InnerTestSerializer.serialize(JohnzonNestedSerializerTest.java:55)
        at 
usa.kerby.tk.jhal.JohnzonNestedSerializerTest$InnerTestSerializer.serialize(JohnzonNestedSerializerTest.java:52)
        at 
org.apache.johnzon.jsonb.JohnzonBuilder.lambda$null$24(JohnzonBuilder.java:313)
        at 
org.apache.johnzon.mapper.MappingGeneratorImpl.writeObject(MappingGeneratorImpl.java:97)
        at 
org.apache.johnzon.mapper.DynamicMappingGenerator.writeObject(DynamicMappingGenerator.java:53)
        at 
org.apache.johnzon.jsonb.serializer.JohnzonSerializationContext.serialize(JohnzonSerializationContext.java:36)
        at 
usa.kerby.tk.jhal.JohnzonNestedSerializerTest$OuterTestSerializer.serialize(JohnzonNestedSerializerTest.java:46)
        at 
usa.kerby.tk.jhal.JohnzonNestedSerializerTest$OuterTestSerializer.serialize(JohnzonNestedSerializerTest.java:41)
        at 
org.apache.johnzon.jsonb.JohnzonBuilder.lambda$null$24(JohnzonBuilder.java:313)
        at 
org.apache.johnzon.mapper.MappingGeneratorImpl.doWriteObject(MappingGeneratorImpl.java:174)
        at org.apache.johnzon.mapper.Mapper.writeObject(Mapper.java:235)
        at 
org.apache.johnzon.mapper.Mapper.writeObjectWithGenerator(Mapper.java:208)
        at org.apache.johnzon.mapper.Mapper.writeObject(Mapper.java:203)
        at org.apache.johnzon.mapper.Mapper.writeObjectAsString(Mapper.java:252)
        at org.apache.johnzon.jsonb.JohnzonJsonb.toJson(JohnzonJsonb.java:268)
        at 
usa.kerby.tk.jhal.JohnzonNestedSerializerTest.testNestedSerializer(JohnzonNestedSerializerTest.java:28)
{code}

Tested against Yasson 1.0.8, Johnzon 1.2.9, and Johnzon 1.2.10. Yasson produces 
the expected JSON. Am I right in thinking this is a bug? 



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to