How to serialize JSON object in custom getter?











up vote
2
down vote

favorite












I want to get this json from class User:



{
"id" : "1234",
"campaignId" : {
"campaignId" : "whatever"
}
}


This is the class:



class User {

private String id;
private String campaignId;

public String getId() {
return id;
}

public String getCampaignId() {
return "{ "campaignId" : ""+ campaignId +"" }";
}

}


But I have a wrong json:



{
"id" : "1234",
"campaignId" : "{
"campaignId" : "whatever"
}"
}


As you see, first level campaignId is a string instead of an object containing a campaignId key.



How can I achieve that without creating another POJO?










share|improve this question






















  • Try to use a library like GSON to do that
    – Saulo Aires
    Nov 9 at 14:58










  • another thing, why do you want "campaignId" twice? does this make sense ?
    – Saulo Aires
    Nov 9 at 14:59










  • You could use a custom serializer, but that looks a bit bulky to me.
    – Glains
    Nov 9 at 15:00










  • @SauloAires External API expects JSON with that format. It doesn't make sense to me either...
    – Héctor
    Nov 9 at 15:02






  • 1




    Ok, I did it returning Map<String, String> instead of String and it worked properly. I will post it as answer
    – Héctor
    Nov 9 at 15:03















up vote
2
down vote

favorite












I want to get this json from class User:



{
"id" : "1234",
"campaignId" : {
"campaignId" : "whatever"
}
}


This is the class:



class User {

private String id;
private String campaignId;

public String getId() {
return id;
}

public String getCampaignId() {
return "{ "campaignId" : ""+ campaignId +"" }";
}

}


But I have a wrong json:



{
"id" : "1234",
"campaignId" : "{
"campaignId" : "whatever"
}"
}


As you see, first level campaignId is a string instead of an object containing a campaignId key.



How can I achieve that without creating another POJO?










share|improve this question






















  • Try to use a library like GSON to do that
    – Saulo Aires
    Nov 9 at 14:58










  • another thing, why do you want "campaignId" twice? does this make sense ?
    – Saulo Aires
    Nov 9 at 14:59










  • You could use a custom serializer, but that looks a bit bulky to me.
    – Glains
    Nov 9 at 15:00










  • @SauloAires External API expects JSON with that format. It doesn't make sense to me either...
    – Héctor
    Nov 9 at 15:02






  • 1




    Ok, I did it returning Map<String, String> instead of String and it worked properly. I will post it as answer
    – Héctor
    Nov 9 at 15:03













up vote
2
down vote

favorite









up vote
2
down vote

favorite











I want to get this json from class User:



{
"id" : "1234",
"campaignId" : {
"campaignId" : "whatever"
}
}


This is the class:



class User {

private String id;
private String campaignId;

public String getId() {
return id;
}

public String getCampaignId() {
return "{ "campaignId" : ""+ campaignId +"" }";
}

}


But I have a wrong json:



{
"id" : "1234",
"campaignId" : "{
"campaignId" : "whatever"
}"
}


As you see, first level campaignId is a string instead of an object containing a campaignId key.



How can I achieve that without creating another POJO?










share|improve this question













I want to get this json from class User:



{
"id" : "1234",
"campaignId" : {
"campaignId" : "whatever"
}
}


This is the class:



class User {

private String id;
private String campaignId;

public String getId() {
return id;
}

public String getCampaignId() {
return "{ "campaignId" : ""+ campaignId +"" }";
}

}


But I have a wrong json:



{
"id" : "1234",
"campaignId" : "{
"campaignId" : "whatever"
}"
}


As you see, first level campaignId is a string instead of an object containing a campaignId key.



How can I achieve that without creating another POJO?







java json jackson






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 9 at 14:56









Héctor

9,0541357122




9,0541357122












  • Try to use a library like GSON to do that
    – Saulo Aires
    Nov 9 at 14:58










  • another thing, why do you want "campaignId" twice? does this make sense ?
    – Saulo Aires
    Nov 9 at 14:59










  • You could use a custom serializer, but that looks a bit bulky to me.
    – Glains
    Nov 9 at 15:00










  • @SauloAires External API expects JSON with that format. It doesn't make sense to me either...
    – Héctor
    Nov 9 at 15:02






  • 1




    Ok, I did it returning Map<String, String> instead of String and it worked properly. I will post it as answer
    – Héctor
    Nov 9 at 15:03


















  • Try to use a library like GSON to do that
    – Saulo Aires
    Nov 9 at 14:58










  • another thing, why do you want "campaignId" twice? does this make sense ?
    – Saulo Aires
    Nov 9 at 14:59










  • You could use a custom serializer, but that looks a bit bulky to me.
    – Glains
    Nov 9 at 15:00










  • @SauloAires External API expects JSON with that format. It doesn't make sense to me either...
    – Héctor
    Nov 9 at 15:02






  • 1




    Ok, I did it returning Map<String, String> instead of String and it worked properly. I will post it as answer
    – Héctor
    Nov 9 at 15:03
















Try to use a library like GSON to do that
– Saulo Aires
Nov 9 at 14:58




Try to use a library like GSON to do that
– Saulo Aires
Nov 9 at 14:58












another thing, why do you want "campaignId" twice? does this make sense ?
– Saulo Aires
Nov 9 at 14:59




another thing, why do you want "campaignId" twice? does this make sense ?
– Saulo Aires
Nov 9 at 14:59












You could use a custom serializer, but that looks a bit bulky to me.
– Glains
Nov 9 at 15:00




You could use a custom serializer, but that looks a bit bulky to me.
– Glains
Nov 9 at 15:00












@SauloAires External API expects JSON with that format. It doesn't make sense to me either...
– Héctor
Nov 9 at 15:02




@SauloAires External API expects JSON with that format. It doesn't make sense to me either...
– Héctor
Nov 9 at 15:02




1




1




Ok, I did it returning Map<String, String> instead of String and it worked properly. I will post it as answer
– Héctor
Nov 9 at 15:03




Ok, I did it returning Map<String, String> instead of String and it worked properly. I will post it as answer
– Héctor
Nov 9 at 15:03












2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted











I want to get this json from class User: ...



"campaignId" : {
"campaignId" : "whatever"




...




private String campaignId;




Those things simply don't go together.



If your requirement is that campaignId should be a nested structure, like a map, then model it accordingly.



In other words: the type in your "bean" should be something else than String. For example, a map. Or maybe, some self-written class that has a single member being a string.



Anything else is just very confusing for your readers. Your code should communicate your intent. And a field that is a string isn't a map.



Its like saying: "look here, my cute cat", where in reality, you have a dog, but you force him into a kitten-costume to look like a cat.






share|improve this answer

















  • 2




    Disclaimer: no animals were harmed for the production of this answer.
    – GhostCat
    Nov 9 at 15:31


















up vote
-1
down vote













I solved returning Map instead of String:



public Map<String, String> getCampaignId() {
Map<String, String> c = new HashMap<>();
c.put("campaignId", campaignId);
return c;
}


That's working but if anybody knows how can be done using Jackson annotations or another more elegant solution, I will accept.






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',
    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%2f53228113%2fhow-to-serialize-json-object-in-custom-getter%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








    up vote
    2
    down vote



    accepted











    I want to get this json from class User: ...



    "campaignId" : {
    "campaignId" : "whatever"




    ...




    private String campaignId;




    Those things simply don't go together.



    If your requirement is that campaignId should be a nested structure, like a map, then model it accordingly.



    In other words: the type in your "bean" should be something else than String. For example, a map. Or maybe, some self-written class that has a single member being a string.



    Anything else is just very confusing for your readers. Your code should communicate your intent. And a field that is a string isn't a map.



    Its like saying: "look here, my cute cat", where in reality, you have a dog, but you force him into a kitten-costume to look like a cat.






    share|improve this answer

















    • 2




      Disclaimer: no animals were harmed for the production of this answer.
      – GhostCat
      Nov 9 at 15:31















    up vote
    2
    down vote



    accepted











    I want to get this json from class User: ...



    "campaignId" : {
    "campaignId" : "whatever"




    ...




    private String campaignId;




    Those things simply don't go together.



    If your requirement is that campaignId should be a nested structure, like a map, then model it accordingly.



    In other words: the type in your "bean" should be something else than String. For example, a map. Or maybe, some self-written class that has a single member being a string.



    Anything else is just very confusing for your readers. Your code should communicate your intent. And a field that is a string isn't a map.



    Its like saying: "look here, my cute cat", where in reality, you have a dog, but you force him into a kitten-costume to look like a cat.






    share|improve this answer

















    • 2




      Disclaimer: no animals were harmed for the production of this answer.
      – GhostCat
      Nov 9 at 15:31













    up vote
    2
    down vote



    accepted







    up vote
    2
    down vote



    accepted







    I want to get this json from class User: ...



    "campaignId" : {
    "campaignId" : "whatever"




    ...




    private String campaignId;




    Those things simply don't go together.



    If your requirement is that campaignId should be a nested structure, like a map, then model it accordingly.



    In other words: the type in your "bean" should be something else than String. For example, a map. Or maybe, some self-written class that has a single member being a string.



    Anything else is just very confusing for your readers. Your code should communicate your intent. And a field that is a string isn't a map.



    Its like saying: "look here, my cute cat", where in reality, you have a dog, but you force him into a kitten-costume to look like a cat.






    share|improve this answer













    I want to get this json from class User: ...



    "campaignId" : {
    "campaignId" : "whatever"




    ...




    private String campaignId;




    Those things simply don't go together.



    If your requirement is that campaignId should be a nested structure, like a map, then model it accordingly.



    In other words: the type in your "bean" should be something else than String. For example, a map. Or maybe, some self-written class that has a single member being a string.



    Anything else is just very confusing for your readers. Your code should communicate your intent. And a field that is a string isn't a map.



    Its like saying: "look here, my cute cat", where in reality, you have a dog, but you force him into a kitten-costume to look like a cat.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 9 at 15:31









    GhostCat

    85.9k1682141




    85.9k1682141








    • 2




      Disclaimer: no animals were harmed for the production of this answer.
      – GhostCat
      Nov 9 at 15:31














    • 2




      Disclaimer: no animals were harmed for the production of this answer.
      – GhostCat
      Nov 9 at 15:31








    2




    2




    Disclaimer: no animals were harmed for the production of this answer.
    – GhostCat
    Nov 9 at 15:31




    Disclaimer: no animals were harmed for the production of this answer.
    – GhostCat
    Nov 9 at 15:31












    up vote
    -1
    down vote













    I solved returning Map instead of String:



    public Map<String, String> getCampaignId() {
    Map<String, String> c = new HashMap<>();
    c.put("campaignId", campaignId);
    return c;
    }


    That's working but if anybody knows how can be done using Jackson annotations or another more elegant solution, I will accept.






    share|improve this answer

























      up vote
      -1
      down vote













      I solved returning Map instead of String:



      public Map<String, String> getCampaignId() {
      Map<String, String> c = new HashMap<>();
      c.put("campaignId", campaignId);
      return c;
      }


      That's working but if anybody knows how can be done using Jackson annotations or another more elegant solution, I will accept.






      share|improve this answer























        up vote
        -1
        down vote










        up vote
        -1
        down vote









        I solved returning Map instead of String:



        public Map<String, String> getCampaignId() {
        Map<String, String> c = new HashMap<>();
        c.put("campaignId", campaignId);
        return c;
        }


        That's working but if anybody knows how can be done using Jackson annotations or another more elegant solution, I will accept.






        share|improve this answer












        I solved returning Map instead of String:



        public Map<String, String> getCampaignId() {
        Map<String, String> c = new HashMap<>();
        c.put("campaignId", campaignId);
        return c;
        }


        That's working but if anybody knows how can be done using Jackson annotations or another more elegant solution, I will accept.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 9 at 15:26









        Héctor

        9,0541357122




        9,0541357122






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53228113%2fhow-to-serialize-json-object-in-custom-getter%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







            Popular posts from this blog

            How to pass form data using jquery Ajax to insert data in database?

            National Museum of Racing and Hall of Fame

            Guess what letter conforming each word