Serialize/Deserialize custom Map in JacksonDeserializing non-string map keys with JacksonSort a Map<Key, Value> by valuesWhat is object serialization?Sort ArrayList of custom Objects by propertyDeserialize JSON into C# dynamic object?Ignoring new fields on JSON objects using JacksonHow to use Jackson to deserialise an array of objectsHow to tell Jackson to ignore a field during serialization if its value is null?Jackson enum Serializing and DeSerializerJava Serialization/De-serialization giving null object referencesJackson Mixin not working for deserializing non-default constructor object
can i play a electric guitar through a bass amp?
What's the output of a record cartridge playing an out-of-speed record
Why "Having chlorophyll without photosynthesis is actually very dangerous" and "like living with a bomb"?
A newer friend of my brother's gave him a load of baseball cards that are supposedly extremely valuable. Is this a scam?
An academic/student plagiarism
How can bays and straits be determined in a procedurally generated map?
Why, historically, did Gödel think CH was false?
Font hinting is lost in Chrome-like browsers (for some languages )
"You are your self first supporter", a more proper way to say it
How old can references or sources in a thesis be?
To string or not to string
What do you call a Matrix-like slowdown and camera movement effect?
Why doesn't H₄O²⁺ exist?
Smoothness of finite-dimensional functional calculus
Why dont electromagnetic waves interact with each other?
Why Is Death Allowed In the Matrix?
What defenses are there against being summoned by the Gate spell?
Can divisibility rules for digits be generalized to sum of digits
Writing rule stating superpower from different root cause is bad writing
What is the offset in a seaplane's hull?
Why doesn't Newton's third law mean a person bounces back to where they started when they hit the ground?
Maximum likelihood parameters deviate from posterior distributions
"to be prejudice towards/against someone" vs "to be prejudiced against/towards someone"
Python: next in for loop
Serialize/Deserialize custom Map in Jackson
Deserializing non-string map keys with JacksonSort a Map<Key, Value> by valuesWhat is object serialization?Sort ArrayList of custom Objects by propertyDeserialize JSON into C# dynamic object?Ignoring new fields on JSON objects using JacksonHow to use Jackson to deserialise an array of objectsHow to tell Jackson to ignore a field during serialization if its value is null?Jackson enum Serializing and DeSerializerJava Serialization/De-serialization giving null object referencesJackson Mixin not working for deserializing non-default constructor object
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have a pretty simple Map I want to serialize and deserialize in Jackson, but I can't get it to work.
I have tried the following:
@JsonSerialize(keyUsing=TurnKeySerializer.class)
@JsonDeserialize(keyUsing = TurnKeyDeserializer.class)
Map<TurnKey, PlayerTurn> publicTurns = new TreeMap<>();
@JsonIgnoreProperties(ignoreUnknown = true)
@Data //Creates Getter/Setter etc
public class TurnKey implements Comparable<TurnKey> {
private final int turnNumber;
private final String username;
public TurnKey(int turnNumber, String username)
this.turnNumber = turnNumber;
this.username = username;
@Override
public int compareTo(TurnKey o)
int v = Integer.valueOf(turnNumber).compareTo(o.getTurnNumber());
if (v != 0)
return v;
return username.compareTo(o.getUsername());
@Override
public String toString()
return "" +
"turnNumber:" + turnNumber +
", username:'" + username + "'" +
"";
public class TurnKeySerializer extends JsonSerializer<TurnKey>
@Override
public void serialize(TurnKey value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException
if (null == value)
throw new IOException("Could not serialize object to json, input object to serialize is null");
StringWriter writer = new StringWriter();
ObjectMapper mapper = new ObjectMapper();
mapper.writeValue(writer, value);
gen.writeFieldName(writer.toString());
public class TurnKeyDeserializer extends KeyDeserializer
private static final ObjectMapper mapper = new ObjectMapper();
@Override
public TurnKey deserializeKey(String key, DeserializationContext ctxt) throws IOException, JsonProcessingException
return mapper.readValue(key, TurnKey.class);
But I get an exception
com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.LinkedHashMap out of START_ARRAY token
java serialization jackson deserialization
|
show 1 more comment
I have a pretty simple Map I want to serialize and deserialize in Jackson, but I can't get it to work.
I have tried the following:
@JsonSerialize(keyUsing=TurnKeySerializer.class)
@JsonDeserialize(keyUsing = TurnKeyDeserializer.class)
Map<TurnKey, PlayerTurn> publicTurns = new TreeMap<>();
@JsonIgnoreProperties(ignoreUnknown = true)
@Data //Creates Getter/Setter etc
public class TurnKey implements Comparable<TurnKey> {
private final int turnNumber;
private final String username;
public TurnKey(int turnNumber, String username)
this.turnNumber = turnNumber;
this.username = username;
@Override
public int compareTo(TurnKey o)
int v = Integer.valueOf(turnNumber).compareTo(o.getTurnNumber());
if (v != 0)
return v;
return username.compareTo(o.getUsername());
@Override
public String toString()
return "" +
"turnNumber:" + turnNumber +
", username:'" + username + "'" +
"";
public class TurnKeySerializer extends JsonSerializer<TurnKey>
@Override
public void serialize(TurnKey value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException
if (null == value)
throw new IOException("Could not serialize object to json, input object to serialize is null");
StringWriter writer = new StringWriter();
ObjectMapper mapper = new ObjectMapper();
mapper.writeValue(writer, value);
gen.writeFieldName(writer.toString());
public class TurnKeyDeserializer extends KeyDeserializer
private static final ObjectMapper mapper = new ObjectMapper();
@Override
public TurnKey deserializeKey(String key, DeserializationContext ctxt) throws IOException, JsonProcessingException
return mapper.readValue(key, TurnKey.class);
But I get an exception
com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.LinkedHashMap out of START_ARRAY token
java serialization jackson deserialization
Can you explain why you are using a custom (de)serializer for TurnKey? Have you tried it without?
– Tom
Nov 15 '15 at 21:03
Is the serialized json ok?
– Tom
Nov 15 '15 at 21:05
@Tom Because you are required. Otherwise Jackson cannot map. I tried without first, that what led me to this. I am only getting error on deserialization, so I believe so. The toString() looks correct to me at least
– Shervin Asgari
Nov 16 '15 at 6:58
2
Well I do get an exception if I use the TurnKey withouth any serializer/deserializer.Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not find a (Map) Key deserializer for type [simple type, class no.asgari.civilization.server.model.TurnKey] at com.fasterxml.jackson.databind.deser.DeserializerCache._handleUnknownKeyDeserializer(DeserializerCache.java:584)
– Shervin Asgari
Nov 18 '15 at 19:55
1
Maps with non-string keys do need custom deseriizers. But serialisation of such maps will simply call the toString method of the key object. Check that what is in the serialised json is what you expect as input. You may also use that json key with a repository object to fetch a new object from the db
– Japheth Ongeri - inkalimeva
Jul 7 '16 at 11:29
|
show 1 more comment
I have a pretty simple Map I want to serialize and deserialize in Jackson, but I can't get it to work.
I have tried the following:
@JsonSerialize(keyUsing=TurnKeySerializer.class)
@JsonDeserialize(keyUsing = TurnKeyDeserializer.class)
Map<TurnKey, PlayerTurn> publicTurns = new TreeMap<>();
@JsonIgnoreProperties(ignoreUnknown = true)
@Data //Creates Getter/Setter etc
public class TurnKey implements Comparable<TurnKey> {
private final int turnNumber;
private final String username;
public TurnKey(int turnNumber, String username)
this.turnNumber = turnNumber;
this.username = username;
@Override
public int compareTo(TurnKey o)
int v = Integer.valueOf(turnNumber).compareTo(o.getTurnNumber());
if (v != 0)
return v;
return username.compareTo(o.getUsername());
@Override
public String toString()
return "" +
"turnNumber:" + turnNumber +
", username:'" + username + "'" +
"";
public class TurnKeySerializer extends JsonSerializer<TurnKey>
@Override
public void serialize(TurnKey value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException
if (null == value)
throw new IOException("Could not serialize object to json, input object to serialize is null");
StringWriter writer = new StringWriter();
ObjectMapper mapper = new ObjectMapper();
mapper.writeValue(writer, value);
gen.writeFieldName(writer.toString());
public class TurnKeyDeserializer extends KeyDeserializer
private static final ObjectMapper mapper = new ObjectMapper();
@Override
public TurnKey deserializeKey(String key, DeserializationContext ctxt) throws IOException, JsonProcessingException
return mapper.readValue(key, TurnKey.class);
But I get an exception
com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.LinkedHashMap out of START_ARRAY token
java serialization jackson deserialization
I have a pretty simple Map I want to serialize and deserialize in Jackson, but I can't get it to work.
I have tried the following:
@JsonSerialize(keyUsing=TurnKeySerializer.class)
@JsonDeserialize(keyUsing = TurnKeyDeserializer.class)
Map<TurnKey, PlayerTurn> publicTurns = new TreeMap<>();
@JsonIgnoreProperties(ignoreUnknown = true)
@Data //Creates Getter/Setter etc
public class TurnKey implements Comparable<TurnKey> {
private final int turnNumber;
private final String username;
public TurnKey(int turnNumber, String username)
this.turnNumber = turnNumber;
this.username = username;
@Override
public int compareTo(TurnKey o)
int v = Integer.valueOf(turnNumber).compareTo(o.getTurnNumber());
if (v != 0)
return v;
return username.compareTo(o.getUsername());
@Override
public String toString()
return "" +
"turnNumber:" + turnNumber +
", username:'" + username + "'" +
"";
public class TurnKeySerializer extends JsonSerializer<TurnKey>
@Override
public void serialize(TurnKey value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException
if (null == value)
throw new IOException("Could not serialize object to json, input object to serialize is null");
StringWriter writer = new StringWriter();
ObjectMapper mapper = new ObjectMapper();
mapper.writeValue(writer, value);
gen.writeFieldName(writer.toString());
public class TurnKeyDeserializer extends KeyDeserializer
private static final ObjectMapper mapper = new ObjectMapper();
@Override
public TurnKey deserializeKey(String key, DeserializationContext ctxt) throws IOException, JsonProcessingException
return mapper.readValue(key, TurnKey.class);
But I get an exception
com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.LinkedHashMap out of START_ARRAY token
java serialization jackson deserialization
java serialization jackson deserialization
asked Nov 15 '15 at 20:20
Shervin AsgariShervin Asgari
15k2580128
15k2580128
Can you explain why you are using a custom (de)serializer for TurnKey? Have you tried it without?
– Tom
Nov 15 '15 at 21:03
Is the serialized json ok?
– Tom
Nov 15 '15 at 21:05
@Tom Because you are required. Otherwise Jackson cannot map. I tried without first, that what led me to this. I am only getting error on deserialization, so I believe so. The toString() looks correct to me at least
– Shervin Asgari
Nov 16 '15 at 6:58
2
Well I do get an exception if I use the TurnKey withouth any serializer/deserializer.Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not find a (Map) Key deserializer for type [simple type, class no.asgari.civilization.server.model.TurnKey] at com.fasterxml.jackson.databind.deser.DeserializerCache._handleUnknownKeyDeserializer(DeserializerCache.java:584)
– Shervin Asgari
Nov 18 '15 at 19:55
1
Maps with non-string keys do need custom deseriizers. But serialisation of such maps will simply call the toString method of the key object. Check that what is in the serialised json is what you expect as input. You may also use that json key with a repository object to fetch a new object from the db
– Japheth Ongeri - inkalimeva
Jul 7 '16 at 11:29
|
show 1 more comment
Can you explain why you are using a custom (de)serializer for TurnKey? Have you tried it without?
– Tom
Nov 15 '15 at 21:03
Is the serialized json ok?
– Tom
Nov 15 '15 at 21:05
@Tom Because you are required. Otherwise Jackson cannot map. I tried without first, that what led me to this. I am only getting error on deserialization, so I believe so. The toString() looks correct to me at least
– Shervin Asgari
Nov 16 '15 at 6:58
2
Well I do get an exception if I use the TurnKey withouth any serializer/deserializer.Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not find a (Map) Key deserializer for type [simple type, class no.asgari.civilization.server.model.TurnKey] at com.fasterxml.jackson.databind.deser.DeserializerCache._handleUnknownKeyDeserializer(DeserializerCache.java:584)
– Shervin Asgari
Nov 18 '15 at 19:55
1
Maps with non-string keys do need custom deseriizers. But serialisation of such maps will simply call the toString method of the key object. Check that what is in the serialised json is what you expect as input. You may also use that json key with a repository object to fetch a new object from the db
– Japheth Ongeri - inkalimeva
Jul 7 '16 at 11:29
Can you explain why you are using a custom (de)serializer for TurnKey? Have you tried it without?
– Tom
Nov 15 '15 at 21:03
Can you explain why you are using a custom (de)serializer for TurnKey? Have you tried it without?
– Tom
Nov 15 '15 at 21:03
Is the serialized json ok?
– Tom
Nov 15 '15 at 21:05
Is the serialized json ok?
– Tom
Nov 15 '15 at 21:05
@Tom Because you are required. Otherwise Jackson cannot map. I tried without first, that what led me to this. I am only getting error on deserialization, so I believe so. The toString() looks correct to me at least
– Shervin Asgari
Nov 16 '15 at 6:58
@Tom Because you are required. Otherwise Jackson cannot map. I tried without first, that what led me to this. I am only getting error on deserialization, so I believe so. The toString() looks correct to me at least
– Shervin Asgari
Nov 16 '15 at 6:58
2
2
Well I do get an exception if I use the TurnKey withouth any serializer/deserializer.
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not find a (Map) Key deserializer for type [simple type, class no.asgari.civilization.server.model.TurnKey] at com.fasterxml.jackson.databind.deser.DeserializerCache._handleUnknownKeyDeserializer(DeserializerCache.java:584)
– Shervin Asgari
Nov 18 '15 at 19:55
Well I do get an exception if I use the TurnKey withouth any serializer/deserializer.
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not find a (Map) Key deserializer for type [simple type, class no.asgari.civilization.server.model.TurnKey] at com.fasterxml.jackson.databind.deser.DeserializerCache._handleUnknownKeyDeserializer(DeserializerCache.java:584)
– Shervin Asgari
Nov 18 '15 at 19:55
1
1
Maps with non-string keys do need custom deseriizers. But serialisation of such maps will simply call the toString method of the key object. Check that what is in the serialised json is what you expect as input. You may also use that json key with a repository object to fetch a new object from the db
– Japheth Ongeri - inkalimeva
Jul 7 '16 at 11:29
Maps with non-string keys do need custom deseriizers. But serialisation of such maps will simply call the toString method of the key object. Check that what is in the serialised json is what you expect as input. You may also use that json key with a repository object to fetch a new object from the db
– Japheth Ongeri - inkalimeva
Jul 7 '16 at 11:29
|
show 1 more comment
1 Answer
1
active
oldest
votes
You need to define/override the fromString() method in TurnKey. Jackson will use toString() to serialize and fromString() to deserialize. That's what "Can not find a (Map) Key deserializer" means in the error message Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not find a (Map) Key deserializer for type [simple type, class no.asgari.civilization.server.model.TurnKey] at com.fasterxml.jackson.databind.deser.DeserializerCache._handleUnknownKeyDeserializer(DeserializerCache.java:584)
.
A custom KeyDeserializer is not needed.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f33724568%2fserialize-deserialize-custom-mapkey-object-in-jackson%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You need to define/override the fromString() method in TurnKey. Jackson will use toString() to serialize and fromString() to deserialize. That's what "Can not find a (Map) Key deserializer" means in the error message Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not find a (Map) Key deserializer for type [simple type, class no.asgari.civilization.server.model.TurnKey] at com.fasterxml.jackson.databind.deser.DeserializerCache._handleUnknownKeyDeserializer(DeserializerCache.java:584)
.
A custom KeyDeserializer is not needed.
add a comment |
You need to define/override the fromString() method in TurnKey. Jackson will use toString() to serialize and fromString() to deserialize. That's what "Can not find a (Map) Key deserializer" means in the error message Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not find a (Map) Key deserializer for type [simple type, class no.asgari.civilization.server.model.TurnKey] at com.fasterxml.jackson.databind.deser.DeserializerCache._handleUnknownKeyDeserializer(DeserializerCache.java:584)
.
A custom KeyDeserializer is not needed.
add a comment |
You need to define/override the fromString() method in TurnKey. Jackson will use toString() to serialize and fromString() to deserialize. That's what "Can not find a (Map) Key deserializer" means in the error message Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not find a (Map) Key deserializer for type [simple type, class no.asgari.civilization.server.model.TurnKey] at com.fasterxml.jackson.databind.deser.DeserializerCache._handleUnknownKeyDeserializer(DeserializerCache.java:584)
.
A custom KeyDeserializer is not needed.
You need to define/override the fromString() method in TurnKey. Jackson will use toString() to serialize and fromString() to deserialize. That's what "Can not find a (Map) Key deserializer" means in the error message Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not find a (Map) Key deserializer for type [simple type, class no.asgari.civilization.server.model.TurnKey] at com.fasterxml.jackson.databind.deser.DeserializerCache._handleUnknownKeyDeserializer(DeserializerCache.java:584)
.
A custom KeyDeserializer is not needed.
edited Mar 8 at 3:57
answered Mar 8 at 3:52
JeanieJJeanieJ
11418
11418
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f33724568%2fserialize-deserialize-custom-mapkey-object-in-jackson%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Can you explain why you are using a custom (de)serializer for TurnKey? Have you tried it without?
– Tom
Nov 15 '15 at 21:03
Is the serialized json ok?
– Tom
Nov 15 '15 at 21:05
@Tom Because you are required. Otherwise Jackson cannot map. I tried without first, that what led me to this. I am only getting error on deserialization, so I believe so. The toString() looks correct to me at least
– Shervin Asgari
Nov 16 '15 at 6:58
2
Well I do get an exception if I use the TurnKey withouth any serializer/deserializer.
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not find a (Map) Key deserializer for type [simple type, class no.asgari.civilization.server.model.TurnKey] at com.fasterxml.jackson.databind.deser.DeserializerCache._handleUnknownKeyDeserializer(DeserializerCache.java:584)
– Shervin Asgari
Nov 18 '15 at 19:55
1
Maps with non-string keys do need custom deseriizers. But serialisation of such maps will simply call the toString method of the key object. Check that what is in the serialised json is what you expect as input. You may also use that json key with a repository object to fetch a new object from the db
– Japheth Ongeri - inkalimeva
Jul 7 '16 at 11:29