Use jackson annotation on a field to be the root for deserialization










0















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










share|improve this question
























  • items and item1 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 use Map

    – 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















0















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










share|improve this question
























  • items and item1 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 use Map

    – 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













0












0








0


1






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










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 7:57







Dilshod Tadjibaev

















asked Nov 13 '18 at 7:07









Dilshod TadjibaevDilshod Tadjibaev

1,063816




1,063816












  • items and item1 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 use Map

    – 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

















  • items and item1 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 use Map

    – 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
















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












2 Answers
2






active

oldest

votes


















0














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






share|improve this answer

























  • 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


















0














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;






share|improve this answer






















    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
    );



    );













    draft saved

    draft discarded


















    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









    0














    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






    share|improve this answer

























    • 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















    0














    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






    share|improve this answer

























    • 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













    0












    0








    0







    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






    share|improve this answer















    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







    share|improve this answer














    share|improve this answer



    share|improve this answer








    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

















    • 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













    0














    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;






    share|improve this answer



























      0














      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;






      share|improve this answer

























        0












        0








        0







        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;






        share|improve this answer













        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;







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 '18 at 19:15









        Dilshod TadjibaevDilshod Tadjibaev

        1,063816




        1,063816



























            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3

            Museum of Modern and Contemporary Art of Trento and Rovereto