Closed flowdopip closed 1 month ago
you can write the json data into a file and check what is invalid in it.
sure, but my point is why encode does not throw an error?
would you please give an example of the json data
`
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
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: @.***>
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.
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.
Thanks for you explanation.
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