sjbarag / brs

An interpreter for the BrightScript language that runs on non-Roku platforms.
MIT License
113 stars 43 forks source link

implement JSON-related global utilities #93

Closed strattonbrazil closed 5 years ago

strattonbrazil commented 5 years ago

Brightscript implements a couple functions for working with JSON (https://sdkdocs.roku.com/display/sdkdoc/Global+Utility+Functions#GlobalUtilityFunctions-ParseJson(jsonStringasString)asObject). We should implement both of them as they heavily used in a typical codebase.

jwfearn commented 5 years ago

Here's some test code I ran on a Roku box:

print formatJson(invalid) ' expect 'null'
print formatJson(false) ' expect 'false'
print formatJson(-1.2e-34) ' expect '-1.2e-34'
print formatJson("ok") ' expect '"ok"'
' print formatJson("💩") ' expect '"\uD83D\uDCA9"' FAIL
print formatJson("💩", 1) ' expect: "💩"
print formatJson([invalid, false, -1.2e-34, "ok"])
print formatJson({ null: invalid, boolean: false, number: -1.2e-34, string: "ok" })
print formatJson(sub (): end sub) ' expect '""' and console error message

print parseJson("null") ' expect invalid
print parseJson("false") ' expect false
print parseJson("-1.2e-34") ' expect -1.2e-34
print parseJson("""ok""") ' expect "ok"
print parseJson("""💩""") ' expect: "💩"
' print parseJson("""\uF4A9""") ' expect: "💩", FAIL
print parseJson("""\uD83D\uDCA9""") ' expect: "💩"
print parseJson("[null, false, -1.2e-34, ""ok""]")
print parseJson("{""null"": null, ""boolean"": false, ""number"": -1.2e-34, ""string"": ""ok""}")
print parseJson("I'm not JSON") ' expect invalid and console error message

' Unicode round-trip:
print parseJson(formatJson("💩", 1)) ' expect: "💩"
' print parseJson(formatJson("💩")) ' expect: "💩" FAIL
sjbarag commented 5 years ago

Looks like #119 finished off almost all of this, save for the flags parameter in formatJson (which appears to be accepted but ignored at runtime). Thanks again @jwfearn !