Does anyone know the best approach for adding a toJson
function to a custom class? I've considered implementing functions like Foo.toJson
, Foo.toJsonObject
, or Foo.jsonify
.
Some examples suggest using a serializer
. Is that necessary, or is there a more standard way of achieving this functionality?
I want to be able to use the following code:
const foo = new Foo();
fse.writeJson('file.json', foo);
Additionally, for database connection purposes, I also need to manually create a json representation:
const foo = new Foo();
db.writeObject(foo.toJson());