Comments (10)
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.
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.
I don't understand what you are trying to achieve.
Which type are you deserializing ?
from gwt-jackson.
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.
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.
You can also use JsonReader.nextValue() to retrieve the string and then call ObjectMapper.read(String).
from gwt-jackson.
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.
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.
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.
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)
- No source code is available for type com.fasterxml.jackson.databind.ObjectMapper; did you forget to inherit a required module? HOT 3
- Support for java.util.Optional HOT 1
- Expected a string but was BEGIN_OBJECT at line 1 column 2 HOT 1
- Please REVERT the dependency on Jackson 2.9.*, Jackson 2.9.* is NOT compatible with GWT. HOT 2
- Support for @JsonCreator(mode = Mode.DELEGATING)
- Custom deserializer compilation error
- Getter returning Optional<T> of nullable T causing compilation error HOT 2
- GWT 2.6.1 + GwtJackson 0.15.3 HOT 3
- JsonSerializationContext.Builder override not working as expected
- Error:GWT Compiler: No source code is available for type com.fasterxml.jackson.annotation.JsonTypeInfo.As; did you forget to inherit a required module?
- JsonTypeInfo defaultImpl attribute not working HOT 1
- Update jackson dependency to 2.9.8
- Update javapoet dependency to 1.11.1
- Support for JsonFormat.Feature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE
- Annotations on Java8 default interface are ignored HOT 1
- Can't figure out the stackoverflow I am getting
- More (de)serialization customization (JsonFilter or equivalent)
- Double.NaN / NEGATIVE_INFINITY / POSITIVE_INFINITY - Numeric values must be finite, but was
- Many compile problems
- I want to deserialize a property with any type of value.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gwt-jackson.