lua-cjson icon indicating copy to clipboard operation
lua-cjson copied to clipboard

handling totally invalid jsons

Open alexyuriev opened this issue 5 years ago • 1 comments

Hi,

Since this is not addressed in documentation I thought I would ask here - what's the "right" way of handling invalid JSONs without blowing up?

alexyuriev avatar Aug 26 '19 20:08 alexyuriev

I don't think there is nothing specific to this library regarding that. You either have the option of using cjson.safe (that is documented), or just protecting the call with pcall:

+   $ lua5.4
Lua 5.4.3  Copyright (C) 1994-2021 Lua.org, PUC-Rio
+> cjson = require 'cjson'
+> cjson_safe = require 'cjson.safe'
+> pcall(cjson.decode, "foo")
false   Expected value but found invalid token at character 1
+> cjson_safe.decode("foo")
nil     Expected value but found invalid token at character 1

graywolf avatar May 01 '22 12:05 graywolf