JSON get truncated in REST service. Possible becau

2019-09-18 17:48发布

问题:

I am trying to return a JSON response with text that may contain double quotes in them. For example

 "12.10 On-Going Submission of ""Made Up"" Samples." 

I have escaped the double quotes using

StringEscapeUtils.escapeJava(artifact.text);

which perfectly escapes the quotes

\"12.10 On-Going Submission of \"\"Made Up\"\" Samples.\"

But the JSON response is truncated

{
"documentName":"ICENSE AGREEMENT6",
"listOfArtifacts":[
{
"type":"Clause",
"artifacts":[
{
"documentId":6951,
"documentName":"ICENSE AGREEMENT6",
"artifactId":7029,
"text":"\\\"12.10 On-Going Submission o\\\"\\\"" 

This is a REST service where I have manually returned a JSON. (This is not the entire code but I hope you get the idea. in the end the result is rendered as JSON)

    def index() {

        def document
        def artifacts
        def documentId
        def documentName
        def artifactType
        def artifactStatus
        def includeClassifications
        def classifications
        def mapOfAtifactTypes = [:]
        def listOfArtifacts = []
        def listOfClassifications = []
        def rtnVal = [:]

        documentId = params.documentId
        documentName = params.documentName

        try {    
                if (artifacts) {  
                        def k = StringEscapeUtils.escapeJava(artifact.text); 
                        def artifactToAdd = [
                                documentId: artifact.documentId,
                                documentName: artifact.document.docName,
                                artifactId: artifact.id,

                                //text: k,    
                                text: artifact.text.replace("\"","\\\""),           
                                status: artifact.status ?: Artifact.ArtifactStatus.FOR_REVIEW.value,
                                hasClassification: listOfClassifications ? true : false
                        ];

                        listOfArtifacts.add(artifactToAdd)
                        }
                    rtnVal.listOfArtifacts = []
                    mapOfAtifactTypes.each { entry ->
                        rtnVal.listOfArtifacts.add([
                                type: entry.key,
                                artifacts: entry.value
                                ])
                    }
            }

        } catch (Exception e) {
            e.printStackTrace()
            rtnVal = [
                    status: "Bad request",
                    msg: e
            ]
            render e
        }        
        render rtnVal as JSON
    }

So even with the escaping being correct why is the JSON getting truncated ? Also on the console I am getting an error :

2014-09-25 14:55:07,555 [http-bio-8080-exec-1] ERROR errors.GrailsExceptionResolver  - StringIndexOutOfBoundsException occurred when processing request: [GET] /artifact - parameters:
documentName: ICENSE AGREEMENT6
String index out of range: -25. Stacktrace follows:
Message: String index out of range: -25
    Line | Method
->> 1911 | substring      in java.lang.String
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   1946 | subSequence    in     ''
|   1042 | append . . . . in java.io.PrintWriter
|     56 | append         in     ''
|    180 | value . . . .  in grails.converters.JSON
|    162 | convertAnother in     ''
|    202 | value . . . .  in     ''
|    162 | convertAnother in     ''
|    202 | value . . . .  in     ''
|    162 | convertAnother in     ''
|    202 | value . . . .  in     ''
|    134 | render         in     ''
|    150 | render . . . . in     ''
|    328 | $tt__index     in com.thomsonreuters.ald.aeandsdx.ArtifactController
|    198 | doFilter . . . in grails.plugin.cache.web.filter.PageFragmentCachingFilter
|     63 | doFilter       in grails.plugin.cache.web.filter.AbstractFilter
|   1145 | runWorker . .  in java.util.concurrent.ThreadPoolExecutor
|    615 | run            in java.util.concurrent.ThreadPoolExecutor$Worker
^    745 | run . . . . .  in java.lang.Thread

回答1:

You can let the JSON converter do the job for you:

String content = '"12.10 On-Going Submission of ""Made Up"" Samples."'
render ([text: content] as JSON)