You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@johnzon.apache.org by "Trevor Kerby (Jira)" <ji...@apache.org> on 2021/02/06 20:25:00 UTC

[jira] [Comment Edited] (JOHNZON-335) JsonGeneratorImpl.prepareValue throws JsonGenerationException when JsonGenerator.write is called from a nested JsonbSerializer

    [ https://issues.apache.org/jira/browse/JOHNZON-335?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17280277#comment-17280277 ] 

Trevor Kerby edited comment on JOHNZON-335 at 2/6/21, 8:24 PM:
---------------------------------------------------------------

I wrote a test and tried implementing your fix at that location without much success. I realized though that I misunderstood the problem a bit. The writeStartObject/writeEnd pairs in the InnerTestSerializer and OuterTestSerializer may be somewhat unrelated to the issue - I only included them there because that's what was necessary to get Yasson to play nice with the test. I say that because I noticed that if the testNestedSerializer test is copied into the master branch prior to your workaround commit from the other day, and those writeStartObject/writeEnd pairs are omitted from the test, then the test passes. The original test that I included on submission of this issue failed on Johnzon regardless of the existence of those writeStartObject/writeEnd pairs in the serializers because the serializers were registered with JsonbConfig - which represents the real issue.


was (Author: truantkernel):
I wrote a test and tried implementing your fix at that location without much success. I realized though that I misunderstood the problem a bit. The writeStartObject/writeEnd pairs in the InnerTestSerializer and OuterTestSerializer are unnecessary, and the only reason I had them there was because that's what was necessary to get Yasson to play nice with the test. I say that because I noticed that if the testNestedSerializer test is copied into the master branch prior to your workaround commit from the other day, and those writeStartObject/writeEnd pairs are omitted from the test, then the test passes. The original test that I included on submission of this issue failed on Johnzon regardless of the existence of those writeStartObject/writeEnd pairs in the serializers because the serializers were registered with JsonbConfig - which represents the real issue.

> 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.9, 1.2.10
>            Reporter: Trevor Kerby
>            Priority: Major
>         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)