I was reading Redux documentation, and this is what I read:
It is highly recommended that you only put plain serializable objects, arrays, and primitives into your store. It's technically possible to insert non-serializable items into the store, but doing so can break the ability to persist and rehydrate the contents of a store, as well as interfere with time-travel debugging.
I want to know when can an object be called serializable, and when not?
Just for understand : Serializable means that the data can be stored. And ,as @maruru said, the data can be converted to a string, then deserializable without losing information. You can define your own serializable Object by defining the toString() method. In my case, i use static method parse and stringify (like JSON)
class A{
...
toString(): string
static parse(serialized:string): A // deserialize - convert to instance
static stringify(instance:A): string // serialize - convert to string
}
In the meanwhile, serialize mean exactly : convert into a format that can be stored ( in a redux store by example) ( not always string, buffer is good too, or binary or anything that can be stored ;) )
Marco Alka
Software Engineer, Technical Consultant & Mentor
Serializable means that the data can be converted to pure text without losing information. Usually, when talking about serialization in JS, people mean that one can do the following
// data contains your data, for example: // const data = { foo: 'bar', }; const data2 = JSON.parse(JSON.stringify(data));with
dataanddata2containing the very same data after the above program executed. What that means is that you have to be careful with certain structures. For example, you cannot serialize a cyclic reference, because a naive text representation can neither store references, nor cyclic behavior.// data contains your data, for example: // const data = { foo: 'bar', }; // data.self = data; // <-- cyclic reference // The following will fail with `TypeError: cyclic object value` const data2 = JSON.parse(JSON.stringify(data));So, if you are not sure if your object is serializable, use the above code snippet and see if everything works out.