• gson serializing interfac based classes without properties

    From Philipp Kraus@21:1/5 to All on Thu Oct 29 14:12:28 2020
    Hello,

    I have got an interface:

    @Nonnull
    String name();

    @Nonnull
    Object generate();

    void registerAndExecute( @Nonnull final String p_tasklist );

    and then multiple classes which implements this interface, but the classes does not contains any properties.

    No I would like to serialize the classname with GSON, so I have creates my own serializer & deserializer, my deserializer is working, but my serializer not:

    final class CSerializeActivityFactory implements JsonSerializer<IActivityFactory>
    {
    @Override
    public JsonElement serialize( final IActivityFactory p_element, final Type p_type, final JsonSerializationContext p_ctx )
    {
    return p_ctx.serialize( p_element.getClass().getName() );
    }
    }

    I have registered the serializer on my GSON with gsonbuilder.registerTypeAdapter( IActivityFactory.class, new CSerializerActivityFactory() )

    In my case I would like to pass the class name and create on the deserializing process a new class instance (this is working), but my object which I passed into gson for serialization is skipped / empty, so the deserializer breaks.

    I have tried also a registerHierarchyType and I'm trying a TypeAdapterFactory.

    In tries my serializing method is not executed. Can anybody give me a helpful tip?

    Thanks a lot

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Philipp Kraus@21:1/5 to Philipp Kraus on Thu Oct 29 15:50:29 2020
    I have taken a look into GSON OBjectType Adapter and I think this section:

    TypeAdapter<Object> typeAdapter = this.gson.getAdapter(value.getClass());
    if (typeAdapter instanceof ObjectTypeAdapter) {
    out.beginObject();
    out.endObject();
    } else {
    typeAdapter.write(out, value);
    }

    is not working correct in my case, because the getAdapter return for my interface class the ObjectTypeAdapter



    Philipp Kraus schrieb am Donnerstag, 29. Oktober 2020 um 22:12:42 UTC+1:
    Hello,

    I have got an interface:

    @Nonnull
    String name();

    @Nonnull
    Object generate();

    void registerAndExecute( @Nonnull final String p_tasklist );

    and then multiple classes which implements this interface, but the classes does not contains any properties.

    No I would like to serialize the classname with GSON, so I have creates my own serializer & deserializer, my deserializer is working, but my serializer not:

    final class CSerializeActivityFactory implements JsonSerializer<IActivityFactory>
    {
    @Override
    public JsonElement serialize( final IActivityFactory p_element, final Type p_type, final JsonSerializationContext p_ctx )
    {
    return p_ctx.serialize( p_element.getClass().getName() );
    }
    }

    I have registered the serializer on my GSON with gsonbuilder.registerTypeAdapter( IActivityFactory.class, new CSerializerActivityFactory() )

    In my case I would like to pass the class name and create on the deserializing process a new class instance (this is working), but my object which I passed into gson for serialization is skipped / empty, so the deserializer breaks.

    I have tried also a registerHierarchyType and I'm trying a TypeAdapterFactory.

    In tries my serializing method is not executed. Can anybody give me a helpful tip?

    Thanks a lot

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)