Change deserialized Scala 2.11 recursive concrete map type in Jackson
My goal is to read and iterate over nested JSON maps in the same order they appeared in the source JSON. For example, with JSON like
{'nested' : {'key2': 2, 'key1' : 1}}
I want to be able to iterate over the 'nested' map in the same order the keys appeared in the source JSON.
The default Jackson Scala bindings use Map1-Map4 for 1-4 keys, and HashTrieMap for 5 or more keys. I'd like to use ListMap as the concrete Map implementation in order to (hopefully) keep the key order. There seems to be a number of suggestions out there (SimpleModule.addAbstractTypeMapping, SimpleAbstractTypeResolver.addMapping, and others) but I haven't been able to get any of them to work. My current code is
val module = new SimpleModule
module.addAbstractTypeMapping(classOf[Map[_, _]], classOf[ListMap[_, _]])
val mapper = new ObjectMapper() with ScalaObjectMapper
mapper.registerModule(DefaultScalaModule)
mapper.registerModule(module)
val json = """{
"inside" : {
"val1" : 1,
"val3" : "17",
"val2" : "hello2",
"val0" : "hello0",
"val21" : "hello21"
}}"""
val parsed = mapper.readValue[Map[String, Any]](json)
println(parsed.getClass)
parsed.get("inside") match {
case Some(l) => {println(l.getClass); println(l)}
case None => println("none")
}
which results in the output
class scala.collection.immutable.Map$Map1
class scala.collection.immutable.HashMap$HashTrieMap
Map(val1 -> 1, val3 -> 17, val21 -> hello21, val2 -> hello2, val0 -> hello0)
Which is neither a ListMap nor the the original JSON order. I'm currently using Jackson 2.9.6 but am happy to change versions if required to get the desired effect.
json scala dictionary jackson
add a comment |
My goal is to read and iterate over nested JSON maps in the same order they appeared in the source JSON. For example, with JSON like
{'nested' : {'key2': 2, 'key1' : 1}}
I want to be able to iterate over the 'nested' map in the same order the keys appeared in the source JSON.
The default Jackson Scala bindings use Map1-Map4 for 1-4 keys, and HashTrieMap for 5 or more keys. I'd like to use ListMap as the concrete Map implementation in order to (hopefully) keep the key order. There seems to be a number of suggestions out there (SimpleModule.addAbstractTypeMapping, SimpleAbstractTypeResolver.addMapping, and others) but I haven't been able to get any of them to work. My current code is
val module = new SimpleModule
module.addAbstractTypeMapping(classOf[Map[_, _]], classOf[ListMap[_, _]])
val mapper = new ObjectMapper() with ScalaObjectMapper
mapper.registerModule(DefaultScalaModule)
mapper.registerModule(module)
val json = """{
"inside" : {
"val1" : 1,
"val3" : "17",
"val2" : "hello2",
"val0" : "hello0",
"val21" : "hello21"
}}"""
val parsed = mapper.readValue[Map[String, Any]](json)
println(parsed.getClass)
parsed.get("inside") match {
case Some(l) => {println(l.getClass); println(l)}
case None => println("none")
}
which results in the output
class scala.collection.immutable.Map$Map1
class scala.collection.immutable.HashMap$HashTrieMap
Map(val1 -> 1, val3 -> 17, val21 -> hello21, val2 -> hello2, val0 -> hello0)
Which is neither a ListMap nor the the original JSON order. I'm currently using Jackson 2.9.6 but am happy to change versions if required to get the desired effect.
json scala dictionary jackson
add a comment |
My goal is to read and iterate over nested JSON maps in the same order they appeared in the source JSON. For example, with JSON like
{'nested' : {'key2': 2, 'key1' : 1}}
I want to be able to iterate over the 'nested' map in the same order the keys appeared in the source JSON.
The default Jackson Scala bindings use Map1-Map4 for 1-4 keys, and HashTrieMap for 5 or more keys. I'd like to use ListMap as the concrete Map implementation in order to (hopefully) keep the key order. There seems to be a number of suggestions out there (SimpleModule.addAbstractTypeMapping, SimpleAbstractTypeResolver.addMapping, and others) but I haven't been able to get any of them to work. My current code is
val module = new SimpleModule
module.addAbstractTypeMapping(classOf[Map[_, _]], classOf[ListMap[_, _]])
val mapper = new ObjectMapper() with ScalaObjectMapper
mapper.registerModule(DefaultScalaModule)
mapper.registerModule(module)
val json = """{
"inside" : {
"val1" : 1,
"val3" : "17",
"val2" : "hello2",
"val0" : "hello0",
"val21" : "hello21"
}}"""
val parsed = mapper.readValue[Map[String, Any]](json)
println(parsed.getClass)
parsed.get("inside") match {
case Some(l) => {println(l.getClass); println(l)}
case None => println("none")
}
which results in the output
class scala.collection.immutable.Map$Map1
class scala.collection.immutable.HashMap$HashTrieMap
Map(val1 -> 1, val3 -> 17, val21 -> hello21, val2 -> hello2, val0 -> hello0)
Which is neither a ListMap nor the the original JSON order. I'm currently using Jackson 2.9.6 but am happy to change versions if required to get the desired effect.
json scala dictionary jackson
My goal is to read and iterate over nested JSON maps in the same order they appeared in the source JSON. For example, with JSON like
{'nested' : {'key2': 2, 'key1' : 1}}
I want to be able to iterate over the 'nested' map in the same order the keys appeared in the source JSON.
The default Jackson Scala bindings use Map1-Map4 for 1-4 keys, and HashTrieMap for 5 or more keys. I'd like to use ListMap as the concrete Map implementation in order to (hopefully) keep the key order. There seems to be a number of suggestions out there (SimpleModule.addAbstractTypeMapping, SimpleAbstractTypeResolver.addMapping, and others) but I haven't been able to get any of them to work. My current code is
val module = new SimpleModule
module.addAbstractTypeMapping(classOf[Map[_, _]], classOf[ListMap[_, _]])
val mapper = new ObjectMapper() with ScalaObjectMapper
mapper.registerModule(DefaultScalaModule)
mapper.registerModule(module)
val json = """{
"inside" : {
"val1" : 1,
"val3" : "17",
"val2" : "hello2",
"val0" : "hello0",
"val21" : "hello21"
}}"""
val parsed = mapper.readValue[Map[String, Any]](json)
println(parsed.getClass)
parsed.get("inside") match {
case Some(l) => {println(l.getClass); println(l)}
case None => println("none")
}
which results in the output
class scala.collection.immutable.Map$Map1
class scala.collection.immutable.HashMap$HashTrieMap
Map(val1 -> 1, val3 -> 17, val21 -> hello21, val2 -> hello2, val0 -> hello0)
Which is neither a ListMap nor the the original JSON order. I'm currently using Jackson 2.9.6 but am happy to change versions if required to get the desired effect.
json scala dictionary jackson
json scala dictionary jackson
edited Jan 2 at 5:27
Book Of Zeus
45.9k12160161
45.9k12160161
asked Jan 2 at 3:36
14725801472580
285
285
add a comment |
add a comment |
0
active
oldest
votes
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%2f54000939%2fchange-deserialized-scala-2-11-recursive-concrete-map-type-in-jackson%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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%2f54000939%2fchange-deserialized-scala-2-11-recursive-concrete-map-type-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