RReverser's

Ingvar Stepanyan

JavaScript developer, speaker and reverse engineer. D2D programmer. Sometimes human.


User-readable object serialization for JavaScript

View on GitHub

Small serialization helper for those who wants to get JS representation of object but gets only this dirty JSON.

Sometimes it's useful to serialize object into the JS user-readable representation but the only option you have is JSON which adds all this damn quotes around any keys (incl. valid identifiers), indents entire contents of any objects/arrays etc.

This small serializer allows you to overcome that and get pretty representations, just as you would write it with own hands in code:

objJSON.stringify(obj, null, 2)serialize(obj)
{a: 1}
{
  "a": 1
}
{a: 1}
{a: 1, b: 2}
{
  "a": 1,
  "b": 2
}
{a: 1, b: 2}
{a: 1, b: 2, c: 3}
{
  "a": 1,
  "b": 2,
  "c": 3
}
{
  a: 1,
  b: 2,
  c: 3
}
[{a: 1, b: 2, c: 3, '-': '+'}]
[
  {
    "a": 1,
    "b": 2,
    "c": 3,
    "-": "+"
  }
]
[{
  a: 1,
  b: 2,
  c: 3,
  "-": "+"
}]
[{a: 1}, {b: 2}]
[
  {
    "a": 1
  },
  {
    "b": 2
  }
]
[
  {a: 1},
  {b: 2}
]
comments powered by Disqus