Created
April 13, 2021 07:35
-
-
Save sagrawal31/2571df62f173345be9819ed2fab0cd46 to your computer and use it in GitHub Desktop.
Convert string to any actual desired format in Groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import groovy.json.* | |
Object getValue(String value) { | |
try { | |
JsonSlurper slurper = new JsonSlurper() | |
return slurper.parseText(value) | |
} catch (JsonException e) { | |
// supress the exception - means it's unparceable | |
} | |
if (value == "N") { | |
return false | |
} else if (value == "Y") { | |
return true | |
} else { | |
log(">>> WARN: Unparceaable value $value") | |
return value | |
} | |
} | |
println getValue("4").dump() | |
println getValue("4.6").dump() | |
println getValue("N").dump() | |
println getValue('{"id": "4.5"}').dump() | |
println getValue("[2,3,4]").dump() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment