|
|
|
|
|
by ckdot2
1206 days ago
|
|
Please, don't write your own JSON parser/validator. There's JSON Schema https://json-schema.org which has implementations in most languages.
You can valiate your JSON by a given, standardized JSON schema file - and you're basically done.
After the validation, it's probably good practise to map the JSON to some DTO and may do some further validation which doesn't check the structure of the data but it's meaning. |
|
The reason this is better, is that it's required for your program to work: if your processing functions take a 'MyJSON' as argument, then (a) your program must call the 'checkAgainstMySchema' function; and (b) you can only run your data processing in the successful branch (since that's the only way to get the 'MyJSON' argument you need).
In contrast, the functions which return 'Boolean' and 'JSON' are not required; in the sense that, we could completely forget to do any validation, and still end up with a runnable program. That's dangerous!