
Javascript Serializer can convert almost any object in a browser to a string representation of itself. It’s useful when generating error logs when stack traces are not enough.
Using Javascript Serializer
To use Javascript Serializer, import the following two files:
<script src="https://cdn.rawgit.com/efossas/Javascript-Serializer/ca583a1e/serialize.min.js"><script>
<script src="https://cdn.rawgit.com/efossas/Javascript-Serializer/ca583a1e/deserialize.min.js"><script>
Using the serialize function couldn’t be easier. You just pass the variable you want serialized into the function and it returns a string representation of the variable.
var myObject = { 'example' : 'object' };
var serializedObject = serialize(myObject); // you can deserialize the string as well:
var deserializedObject = deserialize(serializedObject);

Caveats. What It Won’t Serialize
In order to serialize an object, you need one of three things:
- If it’s a primitive value, simple, you just store it as a string. To keep typing, you store primitive strings with single quotes around them.
- If it’s an object, hopefully there is a reflection method that gives you a string that can help you reconstruct it.
- If it doesn’t have reflection, but it’s iterable, you can copy the object to a string by iterating through it.
- If it’s a DOM Node, you can use the XMLSerializer.serializeToString & DOMParser.parseFromString.
You can see below, how the serialize function works, looking for one of those four conditions. However, it uses an additional trick. If it detects a native object (which is a built in object attached to the window), it will try to find it’s name on the…