ResponseBuilder toString() returns object classes in string, not just the raw response string

442 Views Asked by At

I'm presently migrating from the Java ASK-SDK v1 to Java ASK SDK v2.

I'm trying to return a webhook call using the ResponseBuilder class that I built my response up and the data is correct, however when I try to populate the HTTP body with the JSON text, the ResponseBuilder.toString() value doesn't just populate the data with just the string, I get the following:

Optional[class Response {
    outputSpeech: class SsmlOutputSpeech {
        class OutputSpeech {
            type: SSML
            playBehavior: null
        }
        ssml: <speak>Some of the things you can say are What would you like to do?</speak>
    }
    card: null
    reprompt: class Reprompt {
        outputSpeech: class SsmlOutputSpeech {
            class OutputSpeech {
                type: SSML
                playBehavior: null
            }
            ssml: <speak>You can say ..., is that what you want?</speak>
        }
    }
    directives: []
    shouldEndSession: false
    canFulfillIntent: null
}]

Is there another way to get the string for the body of the response? The BaseSkillResponse has a getResponse() call, however, I cannot figure out how to use the class to generate the String response output.

2

There are 2 best solutions below

1
On

I was able to get the string with the following in my class:

 private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

 myFunction(){
     try{
         return toJsonString(responseBuilder.build().get());
     } catch(IOException e) {
         e.printStackTrace();
     }
 }

 public String toJsonString(Response response)throws IOException {
     return OBJECT_MAPPER.writeValueAsString(response);
 }
0
On

Solve this by doing the following:

        public String toJsonString(Response response)throws IOException 
        {
          JacksonSerializer jacksonSerializer = new JacksonSerializer();
          constructedResponse = jacksonSerializer.serialize(response);

          JSONObject jsonObject = new JSONObject();

          jsonObject.put("response",constructedResponse);
        }