siscodeorg/sisbase

Safely handle malformed/invalid config files

Closed this issue · 3 comments

Current behaviour: The program will crash with an NRE when an invalid config file exists.

Ideal behaviour: The program will backup the existing malformed config file, print a warning, possibly give some information about why it is malformed, and then generate a new file with the default settings

Malformed files can come in two types. Invalid JSON and Invalid Schema.
Whilst Invalid JSON is detectable (since it throws an exception), Invalid Schema isn't.

Notes about Invalid Schema

JToken#.ToType<T>() will create a new instance of T no matter the serialization of the underlying token.

Using JsonConvert.Deserialize<T>(#JToken) has a simmilar effect, which leads me to believe that JToken#.ToType<T>() is how it is implemented.

Attempting to try-catch any JSONException was in vain. Same goes for Exception. Json.NET doesn't throw an exception for invalid schema.

There seems to exist a Json.NET.Schema package that has an JToken#IsValid(JsonSchema) extension method.

A Fix for this issue is already implemented but its pending review.
Branch : 33-main-config-porting
Commit : c4ce7ae

This issue is entirely superseded by the scope of #33. closed.