Skip to content

Unexpected JSON.stringify and toJSON behavior #271

Closed
@kentor

Description

@kentor
> Immutable = require('immutable')
> Model = Immutable.Record({})
[Function: Record]
> Model.prototype.toJSON = function() { return 'model' }
[Function]
> JSON.stringify({ a: new Model() })
'{"a":"model"}'
> JSON.stringify(Immutable.Map({ a: new Model() }))
'{"a":{}}'

I expected the last line to return '{"a":"model"}'.

I know toJSON on iterables is an alias to toJS and calls toJS recursively which is why the Model's toJSON isn't called at all. Perhaps toJSON should instead call toJSON recursively which gives us a more familiar way to override the serialization behavior?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions