v icon indicating copy to clipboard operation
v copied to clipboard

checker, json: return error on invalid string input instead of empty string result

Open ttytm opened this issue 1 year ago • 1 comments

With the PR we get errors for invalid Cjson input.

Currently, there are many occasions where decoding panics without an error. Or the main concern in the related issue, is an empty result return without an any error in case of using quotation marks when they shouldn't be used.

Atm:

import json

struct TestStruct {
	host string
}

json.decode(TestStruct, "abc")! // Panics, but without an error value

//quote ⬇️ should not be there
txt := '"{"host": "localhost"}'
json.decode(TestStruct, txt)! // Currently an empty string

New errors:

error: json.decode: invalid json string
    5 | }
    6 | 
    7 | txt := '"{"host": "localhost"}'
      |        ~~~~~~~~~~~~~~~~~~~~~~~~
    8 | json.decode(TestStruct, txt)!
error: json.decode: invalid json string
    5 | }
    6 | 
    7 | json.decode(TestStruct, 'abc')!
      |                         ~~~~~
    8 | 

The change is simple but should hopefully do most validation needed for the module in regards to the nice progress that is being made on the json2 module.

Resolves #19801

ttytm avatar Apr 04 '24 12:04 ttytm

Imho the error should be a runtime one (done in the json module preferably), since the passed text will be only seldom a hardcoded json string (it is done in the issue just for as an illustration/brevity).

spytheman avatar Apr 04 '24 13:04 spytheman