Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handling totally invalid jsons #67

Open
alexyuriev opened this issue Aug 26, 2019 · 1 comment
Open

handling totally invalid jsons #67

alexyuriev opened this issue Aug 26, 2019 · 1 comment

Comments

@alexyuriev
Copy link

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?

@graywolf
Copy link

graywolf commented May 1, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants