-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Serializing some other object in custom serializer #45
Comments
A possible workaround is to define an public class AJsonSerializer extends JsonSerializer {
static interface BJsonWriter extends ObjectWriter<B> {}
private static final AJsonSerializer INSTANCE = new AJsonSerializer();
private final JsonSerializer<B> bJsonSerializer;
public static AJsonSerializer getInstance() {
return INSTANCE;
}
private AJsonSerializer() {
this.bJsonSerializer = (GWT.<AbstractObjectWriter>create( BJsonWriter.class )).getSerializer();
}
@Override
protected void doSerialize( JsonWriter writer, A value, JsonSerializationContext ctx, JsonSerializerParameters params ) {
writer.beginObject();
writer.name( "b" );
writer.value(bJsonSerializer.serialize( writer, value.getB(), ctx, params);
writer.endObject();
}
} A bit more background about this issue : #40 I may add some factory interface where you could define all the serializer/deserializer you need. |
Can I ask why you need to define a custom serializer for your type A ? |
Wow thanks, that was what I was looking for. This works (except I had to use ObjectWriter<B> instead of JsonWriter<B>). The reason for not using the annotations is, that the classes are in other older shared library projects, which must not have JSON annotations, as that projects are used elsewhere with custom build scripts, etc... So it's quite a requirement to leave the classes intact and not using additional dependencies. I am very grateful, thanks. |
Ah right, fixed the example to use You can use mixin annotation when you can't modify the class. |
The problem is that the classes don't have proper getters and setters (non-getter methods start with "get", but must not be called outside of their context, as they throw exceptions), but the fields are private or protected, so they cannot be serialized by the standard serializer. It's related to my previous issue. |
Hello!
I don't find a way how to serialize some other object inside the "doSerialize" method of my custom serializer that extends JsonSerializer.
Let's say I have classes A and B, and A contains field of type B. Now I have custom JSON serializers for both classes A and B and inside AJsonSerializer's doSerialize, I want to write the value of field B.
The thing is, I don't want to write B's fields inside the A's serializer, as in the real life the structure is much more complicated and a class like A contains many other objects. So serializer should be able to delegate each object's writing to its own serializer.
Example:
public class AJsonSerializer extends JsonSerializer {
@OverRide
protected void doSerialize(JsonWriter writer, A value, JsonSerializationContext ctx, JsonSerializerParameters params) {
writer.beginObject();
writer.name("b");
// writer.value(value.getB()); <-- here I want to serialize B with its own custom serializer
writer.endObject();
}
}
The text was updated successfully, but these errors were encountered: