Giter Site home page Giter Site logo

Comments (10)

nmorel avatar nmorel commented on July 29, 2024

You can take a look here to define custom serializer/deserializer : https://github.com/nmorel/gwt-jackson/wiki/Custom-serializers-and-deserializers

from gwt-jackson.

pkolaric avatar pkolaric commented on July 29, 2024

I already did it, but i had to make the mentioned methods public so i could do:
protected T doDeserialize(JsonReader reader, JsonDeserializationContext ctx, JsonDeserializerParameters params) {
....
map.put(key, GWTDeserializers.getDeserializer(key.getType()).deserialize(reader, ctx));
}

where GWTDeserializers holds a class to deserializer map:

private static final Map<Class, JsonDeserializer> mapTypeToDeserializer = new HashMap>();

from gwt-jackson.

nmorel avatar nmorel commented on July 29, 2024

I don't understand what you are trying to achieve.
Which type are you deserializing ?

from gwt-jackson.

pkolaric avatar pkolaric commented on July 29, 2024

I'm deserializing a map Map<String,Object>, where key defines value type. For example let's call it 'Settings'.

class Settings extends Map<String,Object> {
}

json example:
"settings": {
"minValue": 2,
"maxValue": 10,
"label": "someLabel",
"tags": ["tag1", "tag2", "tag3"],
"someSettingWhichIsAPojo": {
"property1":"value1", "property2": 233}
}

minValue, maxValue, label, tags all work because they're primitives and i can easily create a Class -> JsonDeserializer map, which will map Long -> LongJsonDeserializer.getInstance(), String -> .. etc.

The problem is with custom classes, for example, the property someSettingWhichIsAPojo (that maps to MyClass). Deserialization can't be delegated to a gwt-jackson deserializer:

class MyClass {
String property1;
Long property2;
}

public static interface MyMapper extends ObjectReader, ObjectWriter {}
private static final AbstractObjectMapper MAPPER_MYCLASS = GWT.create(MyMapper .class);
MAPPER_MYCLASS.getDeserializer() <-- is protected, so it doesn't work. If it was public i could delegate deserialization to it

Everything works fine if i make those two methods (getSerializer and getDeserializer) public, so i'm suggesting that they're made public in the API.

from gwt-jackson.

nmorel avatar nmorel commented on July 29, 2024

Ok I see your problem now.

If you know all the keys, you can add setter and in the setter, use the put method.
Your Settings class will be considered as a POJO.

In the current snapshot, the annotation @JsonAnySetter is also supported. You can have something like this :

class Settings extends Map {
  void setAPojo(APojo aPojo) {
    put("aPojo", aPojo);
  }

  void setAnotherPojo(AnotherPojo anotherPojo) {
    put("anotherPojo", anotherPojo);
  }

  @JsonAnySetter
  void setAllOtherProperties(String key, Object value) {
    put(key, value);
  }
}

from gwt-jackson.

nmorel avatar nmorel commented on July 29, 2024

You can also use JsonReader.nextValue() to retrieve the string and then call ObjectMapper.read(String).

from gwt-jackson.

nmorel avatar nmorel commented on July 29, 2024

If these workarounds don't work for you, I will maybe add some kind of provider/factory to get access to the JsonSerializer/JsonDeserializer directly like :

public interface JsonDeserializerProvider<T> {
  JsonDeserializer<T> get();
}

public interface MyClassDeserializer extends JsonDeserializerProvider<MyClass> {}

MyClassDeserializer deserializer = GWT.create(MyClassDeserializer.class);

or

public interface JsonMapperFactory {}

public interface MyJsonFactory extends JsonMapperFactory {
  JsonSerializer<MyClass> getMyClassSerializer();
  JsonDeserializer<MyClass> getMyClassDeserializer();
  JsonSerializer<OtherClass> otherClass();
}

MyJsonFactory factory = GWT.create(MyJsonFactory.class);

I don't like making public stuff like your proposal. It's hard to maintain the compatibility after.

from gwt-jackson.

pkolaric avatar pkolaric commented on July 29, 2024

1.) Custom setters + @JsonAnySetter would work but i'd have to create setters for all non-primitive keys and getters for all keys (@JsonAnyGetter only works for primitives)
2.) ObjectMapper.read(String) would work for ObjectMappers, what about delegating to custom deserializers created in GWT or deserializing primitives?

from gwt-jackson.

pkolaric avatar pkolaric commented on July 29, 2024

I think i'll go with protected setters/getters this time. I still think there should be access to generated serializers/deserializers. If i had to vote i would prefer your 2nd proposal (with JsonMapperFactory).

from gwt-jackson.

nmorel avatar nmorel commented on July 29, 2024

Finally, I made them public. I'll add the factory interface later and probably put them back protected before the 1.0.0 release.

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.