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!