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

encode and decode invalid Json, throws error

Open flowdopip opened this issue 1 year ago • 7 comments

Hi all,

I just want to understand why encode invalid JSON works well without exception but decode breaks with an error.

My use case is to encode data on Redis for caching an HTTP request, and when another request matches the criteria, the value on Redis, when decoded, throws an error saying that it is not possible to decode. I´m still trying to understand what is the upstream answering with invalid json, but I need now to not store on redis when json is not valid

flowdopip avatar Jul 29 '24 12:07 flowdopip

you can write the json data into a file and check what is invalid in it.

zhuizhuhaomeng avatar Jul 29 '24 15:07 zhuizhuhaomeng

sure, but my point is why encode does not throw an error?

flowdopip avatar Jul 29 '24 15:07 flowdopip

would you please give an example of the json data

zhuizhuhaomeng avatar Aug 01 '24 00:08 zhuizhuhaomeng

`

local cjson = require "cjson.safe"

local invalidJson = '{"key": "value"'

local req_json, error_encode = cjson.encode(invalidJson)

print(req_json) print(error_encode)

local res_json, error_decode = cjson.decode(invalidJson)

print(res_json) print(error_decode)`

even if you try with cjson only instead of cjson.safe the encode always works for any kind of data

flowdopip avatar Aug 02 '24 08:08 flowdopip


local cjson = require "cjson.safe"

local invalidJson = '{"key": "value"'

local req_json, error_encode = cjson.encode(invalidJson)

print(req_json)
print(error_encode)

local res_json, error_decode = cjson.decode( req_json)  ---> You should
decode the encoded json here

print(res_json)
print(error_decode)

On Fri, Aug 2, 2024 at 4:51 PM luis.silva @.***> wrote:

`

local cjson = require "cjson.safe"

local invalidJson = '{"key": "value"'

local req_json, error_encode = cjson.encode(invalidJson)

print(req_json) print(error_encode)

local res_json, error_decode = cjson.decode(invalidJson)

print(res_json) print(error_decode)`

— Reply to this email directly, view it on GitHub https://github.com/openresty/lua-cjson/issues/105#issuecomment-2264891963, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA6RHPWV2ZJTZFB66DQ6ROTZPNCCRAVCNFSM6AAAAABLUHFWZCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDENRUHA4TCOJWGM . You are receiving this because you commented.Message ID: @.***>

zhuizhuhaomeng avatar Aug 06 '24 00:08 zhuizhuhaomeng

Sure, it was a typo, but as you can see, you can encode invalid JSON, but the decode will return nil. So my point is why encode returns an encoded value when decode could not decode.

flowdopip avatar Aug 06 '24 09:08 flowdopip

I think you have no idea about what are JSON encoding and JSON decoding.

local invalidJson = '{"key": "value"' The above is a Lua string. You can encode the Lua string into a JSON string.

For the JSON decoding, it must be a valid JSON-encoded string other than any string.

you can use print() to see the result of req_json.

zhuizhuhaomeng avatar Aug 06 '24 15:08 zhuizhuhaomeng

Thanks for you explanation.

flowdopip avatar Aug 07 '24 08:08 flowdopip