By design, all fields are required, ie. null or missing fields will cause a TypeError while reviving. In the case of the Animal class from the introductory example:
constpet=M.fromJSON(Animal,'{"name": null}')// => TypeError: no value for key "name"
To support missing properties or null values, you can either use withDefault or declare the property as a Maybe
Using withDefault
The withDefault metadata takes some metadata and a default value which will be used only if the property is null or missing.
import M from'modelico'const {string,maybe} =M.metadata()classAnimalextendsM.Base {// ... same as beforestaticinnerTypes () {returnObject.freeze({ name:withDefault(string(),'Unknown') }) }}
import M from'modelico'const {string,maybe} =M.metadata()classAnimalextendsM.Base {// ... same as beforestaticinnerTypes () {returnObject.freeze({ name:maybe(string()) }) }}
Note: pet2 does not produce the same JSON it was parsed from. If that is important to you, one possibility would be to not declare the name field and use M.fields(pet2).name to check for its presence and value manually.