Quantcast

Cannot have backslashes in JSON value for JSONSlurper?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Cannot have backslashes in JSON value for JSONSlurper?

strayph
The following crashes horriblywhen trying to convert to JSON a Java string with backslashes.

Output follows:
shr$ ./slash.groovy
root: [goo:20201001\\x02]
text: {"goo":"20201001\\\\x02"}
Caught: groovy.json.JsonException: Expected a value on line: 1, column: 26.
But got an unterminated object.
        at slash.run(slash.groovy:20)

Code follows for slash.groovy:
#!/usr/bin/env groovy

import groovy.json.JsonSlurper

s = "20201001\\\\x02"
//m = [h: s]                                                                    

def builder = new groovy.json.JsonBuilder()
def root = builder {
    goo s
}

println "root: $root"

def text = builder.toString()

println "text: $text"

def slurper = new JsonSlurper()
def result = slurper.parseText(text)

println "result: $result"
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Cannot have backslashes in JSON value for JSONSlurper?

strayph
Converting s to a string with single quotes instead of a Groovy String with double quotes has no effect.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Cannot have backslashes in JSON value for JSONSlurper?

strayph
Fails with: s = '20201001\\x02' also.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Cannot have backslashes in JSON value for JSONSlurper?

strayph
In reply to this post by strayph
Ah...I see this supposedly was fixed in 1.8.1
I am downloading 1.8.4 now to give it a try.

Loading...