-
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
make AbstractObjectMapper.getSerializer / getDeserializer public? #40
Comments
You can take a look here to define custom serializer/deserializer : https://github.com/nmorel/gwt-jackson/wiki/Custom-serializers-and-deserializers |
I already did it, but i had to make the mentioned methods public so i could do: where GWTDeserializers holds a class to deserializer map: private static final Map<Class, JsonDeserializer> mapTypeToDeserializer = new HashMap>(); |
I don't understand what you are trying to achieve. |
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: 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 { public static interface MyMapper extends ObjectReader, ObjectWriter {} 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. |
Ok I see your problem now. If you know all the keys, you can add setter and in the setter, use the put method. 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);
}
} |
You can also use JsonReader.nextValue() to retrieve the string and then call ObjectMapper.read(String). |
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. |
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) |
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). |
…ublic There are a few use cases where having a direct access to the JsonSerializer and JsonDeserializer can be useful. For example, when you implement a custom JsonSerializer that uses a generated one. In the future, a factory interface will be added to gain access to any number of ObjectMapper/ObjectReader/ObjectWriter/JsonSerializer/JsonDeserializer.
Finally, I made them public. I'll add the factory interface later and probably put them back protected before the 1.0.0 release. |
It would be nice if
com.github.nmorel.gwtjackson.client.AbstractObjectMapper
methods
protected JsonSerializer getSerializer()
and
protected JsonDeserializer getDeserializer()
were public. Then a custom serializer/deserializer could delegate object deserialization/serialization.
For example if we're deserializing a typed map (Map<String,Object>) where map key defines value type and the values are POJOs.
Or is there a better way to do it?
The text was updated successfully, but these errors were encountered: