Skip to content
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

Closed
butlnor opened this issue Mar 23, 2015 · 5 comments
Closed

Serializing some other object in custom serializer #45

butlnor opened this issue Mar 23, 2015 · 5 comments

Comments

@butlnor
Copy link

butlnor commented Mar 23, 2015

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();
}
}

@nmorel
Copy link
Owner

nmorel commented Mar 23, 2015

A possible workaround is to define an ObjectWriter for B inside your AJsonSerializer and to retrieve the underlying serializer.

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.

@nmorel
Copy link
Owner

nmorel commented Mar 23, 2015

Can I ask why you need to define a custom serializer for your type A ?
You couldn't achieve it with annotations ?

@butlnor
Copy link
Author

butlnor commented Mar 23, 2015

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.

@butlnor butlnor closed this as completed Mar 23, 2015
@nmorel
Copy link
Owner

nmorel commented Mar 23, 2015

Ah right, fixed the example to use ObjectWriter. I didn't test it :p

You can use mixin annotation when you can't modify the class.

@butlnor
Copy link
Author

butlnor commented Mar 23, 2015

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants