Git Product home page Git Product logo

serialize-js's Introduction

serialize-js

Build Status

What's this?

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

Why does it exist?

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}
]

How can I customize the output?

You can optionally pass options object as second argument (serialize(obj, { /*...options...*/ })).

Possible options are below:

initialIndent

Type: Number|String Default: ''

Initial indentation of output (generated indentation will be relative to this one). It can be either number of spaces or explicit string.

indent

Type: Number|String Default: 2

Indentation to be used for nested representations. It can be either number of spaces or explicit string (like '\t').

forceJSON

Type: Boolean Default: false

If set to true, generates JSON-compatible output (all the keys are wrapped with quotes, but indentation is still optimized).

serialize-js's People

Contributors

rreverser avatar youngrafi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

serialize-js's Issues

Option to keep JSON syntax

It's often useful to make a JSON file pretty (with nice indentation etc.) while keeping it valid (e.g. keys in quotes).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.