users@jsonb-spec.java.net

[jsonb-spec users] Re: Getting started with adapters/serializers

From: Roman Grigoriadi <roman.grigoriadi_at_oracle.com>
Date: Fri, 27 Jan 2017 17:41:54 +0100

Both should work. Alternatively you can provide a serializer / adapter
for the wrapping type of your field.
javax.json.stream.JsonGenerationException is from JSONP and basicly says
that you have just written a json key and trying to write something that
cannot follow a key in valid json document.

Here is an example of how such Serializer may look like:

public class StringPaddingSerializer implements JsonbSerializer<String> {
     @Override
     public void serialize(String obj, JsonGenerator generator,
SerializationContext ctx) {
         generator.write(withPadding(obj);
     }

     private String withPadding(String subject) {...}
}

for a pojo:
public class StringWrapper {
     @JsonbTypeSerializer(StringPaddingSerializer.class)
     public String strField;
}


Regards,
Roman

On 01/27/2017 04:53 PM, mjremijan_at_yahoo.com wrote:
> I'm just getting started with the RI of JsonB to start learning this
> technology. I've a bit confused on how to customize the serialization
> and deserialization of a single property inside of a bean. Simple
> example, suppose I have a String productID property and when I
> serialize it to JSON I want to left-pad 0 until the string is 15
> characters long. I've looked at annotating in two ways:
>
> (1)
> @JsonbTypeSerializer(ProductIDSerializer.class)
> public String productID;
>
> (2)
> @JsonbTypeAdapter(ProductIDAdapte.class)
> public String productID;
>
> I thought I would either need an adapter or a serializer. Neither of
> these worked.
>
> When I tried to run with the Serializer, I get this error:
>
> Jan 27, 2017 9:49:23 AM org.eclipse.yasson.internal.Marshaller marshall
> SEVERE: Generating incomplete JSON
> Exception in thread "main" javax.json.stream.JsonGenerationException:
> Illegal method during JSON generation, not valid in current context
> IN_FIELD
> at
> org.glassfish.json.JsonGeneratorImpl.writeKey(JsonGeneratorImpl.java:48
> 1)
> at
> org.eclipse.yasson.internal.serializer.UserSerializerSerializer.seriali
> ze(UserSerializerSerializer.java:49)
> at
> org.eclipse.yasson.internal.AbstractContainerSerializer.serializerCapto
> r(AbstractContainerSerializer.java:77)
> at
> org.eclipse.yasson.internal.serializer.ObjectSerializer.marshallPropert
> y(ObjectSerializer.java:75)
> at
> org.eclipse.yasson.internal.serializer.ObjectSerializer.serializeIntern
> al(ObjectSerializer.java:49)
> at
> org.eclipse.yasson.internal.AbstractContainerSerializer.serialize(Abstr
> actContainerSerializer.java:52)
> at
> org.eclipse.yasson.internal.Marshaller.serializeRoot(Marshaller.java:10
> 4)
> at
> org.eclipse.yasson.internal.Marshaller.marshall(Marshaller.java:73)
> at
> org.eclipse.yasson.internal.JsonBinding.toJson(JsonBinding.java:103)
> at org.thoth.jsonb.Main.main(Main.java:25)
>
>
> When I try to run with the adapter, I get this error:
>
> SEVERE: Generating incomplete JSON
> Exception in thread "main" java.lang.StackOverflowError
> at
> org.eclipse.yasson.internal.serializer.AdaptedObjectSerializer$AdaptedO
> bjectSerializerModel.getCustomization(AdaptedObjectSerializer.java:71)
> at
> org.eclipse.yasson.internal.serializer.AdaptedObjectSerializer$AdaptedO
> bjectSerializerModel.getCustomization(AdaptedObjectSerializer.java:71)
> ....
>
> So where can I find example which demonstrates what I'm looking to do.
>
> Thanks!