v
v copied to clipboard
checker, json: return error on invalid string input instead of empty string result
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
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).