You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When working with models toJson and fromJson serializers, there is inconsistent behavior when it comes to the typed fields.
The model itself gets returned as a standard Object, but the model's fields preserve their original type.
In cases where you the field is one of the standard types (string, number, etc.), this isn't a problem.
The problem arrises when you deal with types like:
Date
ObjectID
other models
etc.
All these types will end up being converted to some other type by JSON.stringify, but I believe the developer experience will be more consistent if this conversion happens at the model level.
Example of Problem
// example model
@Model()classTweetextendsSapiModelMixin(){
@Json()content: string;
@Json()postedDate: Date;}// example instancelett=newTweet();t.content="Hello world!";t.postedDate=newDate();awaitt.save();// serialize to json does not convert typesletjson=t.toJson();console.log(typeofjson.postedDate);// => Dateconsole.log(typeofjson.id);// => ObjectID// serialize from json does not convert typesletnewTweet=Tweet.fromJSON({content: "hello 2",postedDate: "2018-03-28T20:24:13.960Z"});console.log(typeofnewTweet.postedDate);// => string
Notes
If we decided to have the toJson and fromJson serialization do type conversion, we might have a convenient place to place some other conveniences, namely to support TypeScript's enum type, and serialize that to the enums string name.
The text was updated successfully, but these errors were encountered:
When working with models
toJson
andfromJson
serializers, there is inconsistent behavior when it comes to the typed fields.The model itself gets returned as a standard
Object
, but the model's fields preserve their original type.In cases where you the field is one of the standard types (
string
,number
, etc.), this isn't a problem.The problem arrises when you deal with types like:
Date
ObjectID
All these types will end up being converted to some other type by
JSON.stringify
, but I believe the developer experience will be more consistent if this conversion happens at the model level.Example of Problem
Notes
If we decided to have the
toJson
andfromJson
serialization do type conversion, we might have a convenient place to place some other conveniences, namely to support TypeScript'senum
type, and serialize that to the enums string name.The text was updated successfully, but these errors were encountered: