Giter Site home page Giter Site logo

Comments (5)

nmorel avatar nmorel commented on July 29, 2024 1

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.

from gwt-jackson.

nmorel avatar nmorel commented on July 29, 2024

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

from gwt-jackson.

butlnor avatar butlnor commented on July 29, 2024

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.

from gwt-jackson.

nmorel avatar nmorel commented on July 29, 2024

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.

from gwt-jackson.

butlnor avatar butlnor commented on July 29, 2024

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.

from gwt-jackson.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.