Ben Thomas
Ben Thomas

Reputation: 33

Groovy JsonBuilder adds strange character sequences when building Json

I have an empowered enum in a Java class: public enum FinalResponse {

THING_1("Thing1") {
    @Override
    String getDescription() {
        return "Based on what you've told us so far, it’s likely ; (it goes on)
    }

    @Override
    String getApplyUrl() {
        return "https://www.theinternet.com";
    }
},

And when I call getDescription from the following class:

class FinalResponseMaker {

    FinalResponseMaker() {}

    static String getResponse(FinalResponse response) {
        def output = JsonOutput.toJson([[
            code: response.getCode(),                         
            description: response.getDescription(),                             
            apply_url: response.getApplyUrl()]])
    JsonOutput.prettyPrint(output)
    }
}

The output from this code contains the string

it\u2019s likely, 

i.e for some reason the inverted comma in "it's" has been turned into its unicode character.

Why? How can I make this stop happening?

I have tried using

    def pretty = JsonOutput.prettyPrint(output)
    def unescaped = JsonOutput.unescaped(pretty)
    return unescaped

but that doesn't work. Any help would be great, thanks

Upvotes: 1

Views: 1690

Answers (1)

rockympls
rockympls

Reputation: 321

Not the prettiest solution in the world, but ...

description: new String(response.getDescription().getBytes("UTF8"))

This should ensure that you don't get any of that escaped malarky.

Upvotes: 1

Related Questions