-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from metosin/tatu_faster
Faster de-serialization of Maps and Vectors
- Loading branch information
Showing
15 changed files
with
233 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package jsonista.jackson; | ||
|
||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.core.JsonToken; | ||
import com.fasterxml.jackson.databind.*; | ||
import com.fasterxml.jackson.databind.deser.ContextualDeserializer; | ||
import com.fasterxml.jackson.databind.deser.std.StdDeserializer; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class ArrayListDeserializer extends StdDeserializer<List<Object>> implements ContextualDeserializer { | ||
|
||
private JsonDeserializer<Object> _valueDeserializer; | ||
|
||
public ArrayListDeserializer() { | ||
super(List.class); | ||
} | ||
|
||
public ArrayListDeserializer(JsonDeserializer<Object> valueDeser) { | ||
this(); | ||
_valueDeserializer = valueDeser; | ||
} | ||
|
||
@Override | ||
public JsonDeserializer<?> createContextual(DeserializationContext ctxt, BeanProperty beanProperty) throws JsonMappingException { | ||
JavaType object = ctxt.constructType(Object.class); | ||
JsonDeserializer<Object> valueDeser = ctxt.findNonContextualValueDeserializer(object); | ||
return this.withResolved(valueDeser); | ||
} | ||
|
||
private JsonDeserializer<List<Object>> withResolved(JsonDeserializer<Object> valueDeser) { | ||
return this._valueDeserializer == valueDeser ? this : new ArrayListDeserializer(valueDeser); | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
public List<Object> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { | ||
ArrayList<Object> list = new ArrayList<>(); | ||
|
||
JsonDeserializer<Object> deser = ctxt.findNonContextualValueDeserializer(ctxt.constructType(Object.class)); | ||
while (p.nextValue() != JsonToken.END_ARRAY) { | ||
list.add(deser.deserialize(p, ctxt)); | ||
} | ||
return list; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package jsonista.jackson; | ||
|
||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.core.JsonToken; | ||
import com.fasterxml.jackson.databind.*; | ||
import com.fasterxml.jackson.databind.deser.ContextualDeserializer; | ||
import com.fasterxml.jackson.databind.deser.std.StdDeserializer; | ||
|
||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class HashMapDeserializer extends StdDeserializer<Map<Object, Object>> implements ContextualDeserializer { | ||
|
||
private KeyDeserializer _keyDeserializer; | ||
private JsonDeserializer<?> _valueDeserializer; | ||
|
||
public HashMapDeserializer() { | ||
super(Map.class); | ||
} | ||
|
||
public HashMapDeserializer(KeyDeserializer keyDeser, JsonDeserializer<?> valueDeser) { | ||
this(); | ||
_keyDeserializer = keyDeser; | ||
_valueDeserializer = valueDeser; | ||
} | ||
|
||
protected HashMapDeserializer withResolved(KeyDeserializer keyDeser, JsonDeserializer<?> valueDeser) { | ||
return this._keyDeserializer == keyDeser && this._valueDeserializer == valueDeser ? this : new HashMapDeserializer(keyDeser, valueDeser); | ||
} | ||
|
||
@Override | ||
public JsonDeserializer<Map<Object, Object>> createContextual(DeserializationContext ctxt, BeanProperty beanProperty) throws JsonMappingException { | ||
JavaType object = ctxt.constructType(Object.class); | ||
KeyDeserializer keyDeser = ctxt.findKeyDeserializer(object, null); | ||
JsonDeserializer<Object> valueDeser = ctxt.findNonContextualValueDeserializer(object); | ||
return this.withResolved(keyDeser, valueDeser); | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
public Map<Object, Object> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { | ||
Map<Object, Object> map = new HashMap<>(); | ||
while (p.nextToken() != JsonToken.END_OBJECT) { | ||
Object key = _keyDeserializer.deserializeKey(p.getCurrentName(), ctxt); | ||
p.nextToken(); | ||
Object value = _valueDeserializer.deserialize(p, ctxt); | ||
map.put(key, value); | ||
} | ||
return map; | ||
} | ||
} |
Oops, something went wrong.