Use jackson annotation on a field to be the root for deserialization
I have a class that I want to deserialize to:
@Data
public class Config
private Map<String, Item> items;
@Data
public class Item
private String a;
private String b;
private String c;
JSON string:
"item1":
"a": "1",
"b": "2",
"c": "3"
,
"item2":
"a": "10",
"b": "20",
"c": "30"
,
"item3":
"a": "5",
"b": "6",
"c": "7"
What should I use to deserialize the JSON string into Config?
EDIT: Added more items to JSON
java json jackson annotations
add a comment |
I have a class that I want to deserialize to:
@Data
public class Config
private Map<String, Item> items;
@Data
public class Item
private String a;
private String b;
private String c;
JSON string:
"item1":
"a": "1",
"b": "2",
"c": "3"
,
"item2":
"a": "10",
"b": "20",
"c": "30"
,
"item3":
"a": "5",
"b": "6",
"c": "7"
What should I use to deserialize the JSON string into Config?
EDIT: Added more items to JSON
java json jackson annotations
items
anditem1
is not matching
– michaeak
Nov 13 '18 at 7:08
1
Don't useHashMap
on the declaration side, it is a bad practise. Rather useMap
– michaeak
Nov 13 '18 at 7:09
1
Can you showItem
class? has only String field as property?
– user7294900
Nov 13 '18 at 7:13
Using Map and I added Item class.
– Dilshod Tadjibaev
Nov 13 '18 at 8:00
add a comment |
I have a class that I want to deserialize to:
@Data
public class Config
private Map<String, Item> items;
@Data
public class Item
private String a;
private String b;
private String c;
JSON string:
"item1":
"a": "1",
"b": "2",
"c": "3"
,
"item2":
"a": "10",
"b": "20",
"c": "30"
,
"item3":
"a": "5",
"b": "6",
"c": "7"
What should I use to deserialize the JSON string into Config?
EDIT: Added more items to JSON
java json jackson annotations
I have a class that I want to deserialize to:
@Data
public class Config
private Map<String, Item> items;
@Data
public class Item
private String a;
private String b;
private String c;
JSON string:
"item1":
"a": "1",
"b": "2",
"c": "3"
,
"item2":
"a": "10",
"b": "20",
"c": "30"
,
"item3":
"a": "5",
"b": "6",
"c": "7"
What should I use to deserialize the JSON string into Config?
EDIT: Added more items to JSON
java json jackson annotations
java json jackson annotations
edited Nov 13 '18 at 7:57
Dilshod Tadjibaev
asked Nov 13 '18 at 7:07
Dilshod TadjibaevDilshod Tadjibaev
1,063816
1,063816
items
anditem1
is not matching
– michaeak
Nov 13 '18 at 7:08
1
Don't useHashMap
on the declaration side, it is a bad practise. Rather useMap
– michaeak
Nov 13 '18 at 7:09
1
Can you showItem
class? has only String field as property?
– user7294900
Nov 13 '18 at 7:13
Using Map and I added Item class.
– Dilshod Tadjibaev
Nov 13 '18 at 8:00
add a comment |
items
anditem1
is not matching
– michaeak
Nov 13 '18 at 7:08
1
Don't useHashMap
on the declaration side, it is a bad practise. Rather useMap
– michaeak
Nov 13 '18 at 7:09
1
Can you showItem
class? has only String field as property?
– user7294900
Nov 13 '18 at 7:13
Using Map and I added Item class.
– Dilshod Tadjibaev
Nov 13 '18 at 8:00
items
and item1
is not matching– michaeak
Nov 13 '18 at 7:08
items
and item1
is not matching– michaeak
Nov 13 '18 at 7:08
1
1
Don't use
HashMap
on the declaration side, it is a bad practise. Rather use Map
– michaeak
Nov 13 '18 at 7:09
Don't use
HashMap
on the declaration side, it is a bad practise. Rather use Map
– michaeak
Nov 13 '18 at 7:09
1
1
Can you show
Item
class? has only String field as property?– user7294900
Nov 13 '18 at 7:13
Can you show
Item
class? has only String field as property?– user7294900
Nov 13 '18 at 7:13
Using Map and I added Item class.
– Dilshod Tadjibaev
Nov 13 '18 at 8:00
Using Map and I added Item class.
– Dilshod Tadjibaev
Nov 13 '18 at 8:00
add a comment |
2 Answers
2
active
oldest
votes
UPDATE
Use JsonAlias to get multiple values in single property:
@JsonAlias("item1", "item2", "item3")
private Map<String, Item> items;
Annotation that can be used to define one or more alternative names for a property
Older answer to old question
Add JsonProperty with name item1
@Data
public class Config
@JsonProperty("item1")
private Map<String, Item> items;
Marker annotation that can be used to define a non-static method as a "setter" or "getter" for a logical property (depending on its signature), or non-static object field to be used (serialized, deserialized) as a logical property.
EDIT
Updated as Map
following @michaeak comment
Thank you for your answer but I should have made the example clearer. There are multiple items.
– Dilshod Tadjibaev
Nov 13 '18 at 8:01
@DilshodTadjibaev updated answer
– user7294900
Nov 13 '18 at 8:08
add a comment |
A coworker helped me with this answer to my question:
@Data
public class Config
private Map<String, Item> items;
@JsonAnyGetter
public Map<String, Item> getItems()
return items;
@JsonAnySetter
public void setItem(final String key, final Item value)
items.put(key, value);
@Data
public class Item
private String a;
private String b;
private String c;
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%2f53275596%2fuse-jackson-annotation-on-a-field-to-be-the-root-for-deserialization%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
UPDATE
Use JsonAlias to get multiple values in single property:
@JsonAlias("item1", "item2", "item3")
private Map<String, Item> items;
Annotation that can be used to define one or more alternative names for a property
Older answer to old question
Add JsonProperty with name item1
@Data
public class Config
@JsonProperty("item1")
private Map<String, Item> items;
Marker annotation that can be used to define a non-static method as a "setter" or "getter" for a logical property (depending on its signature), or non-static object field to be used (serialized, deserialized) as a logical property.
EDIT
Updated as Map
following @michaeak comment
Thank you for your answer but I should have made the example clearer. There are multiple items.
– Dilshod Tadjibaev
Nov 13 '18 at 8:01
@DilshodTadjibaev updated answer
– user7294900
Nov 13 '18 at 8:08
add a comment |
UPDATE
Use JsonAlias to get multiple values in single property:
@JsonAlias("item1", "item2", "item3")
private Map<String, Item> items;
Annotation that can be used to define one or more alternative names for a property
Older answer to old question
Add JsonProperty with name item1
@Data
public class Config
@JsonProperty("item1")
private Map<String, Item> items;
Marker annotation that can be used to define a non-static method as a "setter" or "getter" for a logical property (depending on its signature), or non-static object field to be used (serialized, deserialized) as a logical property.
EDIT
Updated as Map
following @michaeak comment
Thank you for your answer but I should have made the example clearer. There are multiple items.
– Dilshod Tadjibaev
Nov 13 '18 at 8:01
@DilshodTadjibaev updated answer
– user7294900
Nov 13 '18 at 8:08
add a comment |
UPDATE
Use JsonAlias to get multiple values in single property:
@JsonAlias("item1", "item2", "item3")
private Map<String, Item> items;
Annotation that can be used to define one or more alternative names for a property
Older answer to old question
Add JsonProperty with name item1
@Data
public class Config
@JsonProperty("item1")
private Map<String, Item> items;
Marker annotation that can be used to define a non-static method as a "setter" or "getter" for a logical property (depending on its signature), or non-static object field to be used (serialized, deserialized) as a logical property.
EDIT
Updated as Map
following @michaeak comment
UPDATE
Use JsonAlias to get multiple values in single property:
@JsonAlias("item1", "item2", "item3")
private Map<String, Item> items;
Annotation that can be used to define one or more alternative names for a property
Older answer to old question
Add JsonProperty with name item1
@Data
public class Config
@JsonProperty("item1")
private Map<String, Item> items;
Marker annotation that can be used to define a non-static method as a "setter" or "getter" for a logical property (depending on its signature), or non-static object field to be used (serialized, deserialized) as a logical property.
EDIT
Updated as Map
following @michaeak comment
edited Nov 13 '18 at 8:08
answered Nov 13 '18 at 7:10
user7294900user7294900
21.1k113258
21.1k113258
Thank you for your answer but I should have made the example clearer. There are multiple items.
– Dilshod Tadjibaev
Nov 13 '18 at 8:01
@DilshodTadjibaev updated answer
– user7294900
Nov 13 '18 at 8:08
add a comment |
Thank you for your answer but I should have made the example clearer. There are multiple items.
– Dilshod Tadjibaev
Nov 13 '18 at 8:01
@DilshodTadjibaev updated answer
– user7294900
Nov 13 '18 at 8:08
Thank you for your answer but I should have made the example clearer. There are multiple items.
– Dilshod Tadjibaev
Nov 13 '18 at 8:01
Thank you for your answer but I should have made the example clearer. There are multiple items.
– Dilshod Tadjibaev
Nov 13 '18 at 8:01
@DilshodTadjibaev updated answer
– user7294900
Nov 13 '18 at 8:08
@DilshodTadjibaev updated answer
– user7294900
Nov 13 '18 at 8:08
add a comment |
A coworker helped me with this answer to my question:
@Data
public class Config
private Map<String, Item> items;
@JsonAnyGetter
public Map<String, Item> getItems()
return items;
@JsonAnySetter
public void setItem(final String key, final Item value)
items.put(key, value);
@Data
public class Item
private String a;
private String b;
private String c;
add a comment |
A coworker helped me with this answer to my question:
@Data
public class Config
private Map<String, Item> items;
@JsonAnyGetter
public Map<String, Item> getItems()
return items;
@JsonAnySetter
public void setItem(final String key, final Item value)
items.put(key, value);
@Data
public class Item
private String a;
private String b;
private String c;
add a comment |
A coworker helped me with this answer to my question:
@Data
public class Config
private Map<String, Item> items;
@JsonAnyGetter
public Map<String, Item> getItems()
return items;
@JsonAnySetter
public void setItem(final String key, final Item value)
items.put(key, value);
@Data
public class Item
private String a;
private String b;
private String c;
A coworker helped me with this answer to my question:
@Data
public class Config
private Map<String, Item> items;
@JsonAnyGetter
public Map<String, Item> getItems()
return items;
@JsonAnySetter
public void setItem(final String key, final Item value)
items.put(key, value);
@Data
public class Item
private String a;
private String b;
private String c;
answered Nov 13 '18 at 19:15
Dilshod TadjibaevDilshod Tadjibaev
1,063816
1,063816
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%2f53275596%2fuse-jackson-annotation-on-a-field-to-be-the-root-for-deserialization%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
items
anditem1
is not matching– michaeak
Nov 13 '18 at 7:08
1
Don't use
HashMap
on the declaration side, it is a bad practise. Rather useMap
– michaeak
Nov 13 '18 at 7:09
1
Can you show
Item
class? has only String field as property?– user7294900
Nov 13 '18 at 7:13
Using Map and I added Item class.
– Dilshod Tadjibaev
Nov 13 '18 at 8:00