All Projects → blakeembrey → Javascript Stringify

blakeembrey / Javascript Stringify

Licence: mit
Stringify is to `eval` as `JSON.stringify` is to `JSON.parse`

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects

Projects that are alternatives of or similar to Javascript Stringify

Ason
[DEPRECATED]: Prefer Moshi, Jackson, Gson, or LoganSquare
Stars: ✭ 777 (+692.86%)
Mutual labels:  json, serialize
Serialize Javascript
Serialize JavaScript to a superset of JSON that includes regular expressions and functions.
Stars: ✭ 2,433 (+2382.65%)
Mutual labels:  json, serialize
Kakajson
Fast conversion between JSON and model in Swift.
Stars: ✭ 867 (+784.69%)
Mutual labels:  json, serialize
Dataset Serialize
JSON to DataSet and DataSet to JSON converter for Delphi and Lazarus (FPC)
Stars: ✭ 213 (+117.35%)
Mutual labels:  json, serialize
Jc
CLI tool and python library that converts the output of popular command-line tools and file-types to JSON or Dictionaries. This allows piping of output to tools like jq and simplifying automation scripts.
Stars: ✭ 967 (+886.73%)
Mutual labels:  json, serialize
Swagger Merger
🔗 Merge multiple swagger files into a swagger file, support JSON/YAML.
Stars: ✭ 94 (-4.08%)
Mutual labels:  json
Php Jsondb
A PHP Class that reads JSON file as a database. Use for sample DBs
Stars: ✭ 96 (-2.04%)
Mutual labels:  json
Ssl Checker
Python script that collects SSL/TLS information from hosts
Stars: ✭ 94 (-4.08%)
Mutual labels:  json
X
新生命X组件,数据中间件XCode、日志、网络、RPC、序列化、缓存、Windows服务
Stars: ✭ 1,322 (+1248.98%)
Mutual labels:  serialize
Fast Serialization
FST: fast java serialization drop in-replacement
Stars: ✭ 1,348 (+1275.51%)
Mutual labels:  json
Hid Examples
Examples to accompany the book "Haskell in Depth"
Stars: ✭ 96 (-2.04%)
Mutual labels:  code
Body reconstruction references
Paper, dataset and code collection on human body reconstruction
Stars: ✭ 96 (-2.04%)
Mutual labels:  code
Swurg
Parse OpenAPI documents into Burp Suite for automating OpenAPI-based APIs security assessments (approved by PortSwigger for inclusion in their official BApp Store).
Stars: ✭ 94 (-4.08%)
Mutual labels:  json
Pysmi
SNMP MIB parser
Stars: ✭ 96 (-2.04%)
Mutual labels:  json
Restson Rust
Easy-to-use REST client for Rust programming language
Stars: ✭ 93 (-5.1%)
Mutual labels:  json
Schemer
Schema registry for CSV, TSV, JSON, AVRO and Parquet schema. Supports schema inference and GraphQL API.
Stars: ✭ 97 (-1.02%)
Mutual labels:  json
Phpstorm Solarized
Solarized theme for JetBrains PhpStorm
Stars: ✭ 93 (-5.1%)
Mutual labels:  code
Play Circe
circe for play
Stars: ✭ 95 (-3.06%)
Mutual labels:  json
Undictify
Python library providing type-checked function calls at runtime
Stars: ✭ 97 (-1.02%)
Mutual labels:  json
Generic Json Swift
A simple Swift library for working with generic JSON structures
Stars: ✭ 95 (-3.06%)
Mutual labels:  json

JavaScript Stringify

NPM version NPM downloads Build status Test coverage

Stringify is to eval as JSON.stringify is to JSON.parse.

Installation

npm install javascript-stringify --save

Buy Me A Coffee

Usage

import { stringify } from "javascript-stringify";

The API is similar JSON.stringify:

  • value The value to convert to a string
  • replacer A function that alters the behavior of the stringification process
  • space A string or number that's used to insert white space into the output for readability purposes
  • options
    • maxDepth (number, default: 100) The maximum depth of values to stringify
    • maxValues (number, default: 100000) The maximum number of values to stringify
    • references (boolean, default: false) Restore circular/repeated references in the object (uses IIFE)
    • skipUndefinedProperties (boolean, default: false) Omits undefined properties instead of restoring as undefined

Examples

stringify({}); // "{}"
stringify(true); // "true"
stringify("foo"); // "'foo'"

stringify({ x: 5, y: 6 }); // "{x:5,y:6}"
stringify([1, 2, 3, "string"]); // "[1,2,3,'string']"

stringify({ a: { b: { c: 1 } } }, null, null, { maxDepth: 2 }); // "{a:{b:{}}}"

/**
 * Invalid key names are automatically stringified.
 */

stringify({ "some-key": 10 }); // "{'some-key':10}"

/**
 * Some object types and values can remain identical.
 */

stringify([/.+/gi, new Number(10), new Date()]); // "[/.+/gi,new Number(10),new Date(1406623295732)]"

/**
 * Unknown or circular references are removed.
 */

var obj = { x: 10 };
obj.circular = obj;

stringify(obj); // "{x:10}"
stringify(obj, null, null, { references: true }); // "(function(){var x={x:10};x.circular=x;return x;}())"

/**
 * Specify indentation - just like `JSON.stringify`.
 */

stringify({ a: 2 }, null, " "); // "{\n a: 2\n}"
stringify({ uno: 1, dos: 2 }, null, "\t"); // "{\n\tuno: 1,\n\tdos: 2\n}"

/**
 * Add custom replacer behaviour - like double quoted strings.
 */

stringify(["test", "string"], function(value, indent, stringify) {
  if (typeof value === "string") {
    return '"' + value.replace(/"/g, '\\"') + '"';
  }

  return stringify(value);
});
//=> '["test","string"]'

Formatting

You can use your own code formatter on the result of javascript-stringify. Here is an example using eslint:

const { CLIEngine } = require("eslint");
const { stringify } = require("javascript-stringify");

const { APP_ROOT_PATH, ESLINTRC_FILE_PATH } = require("./constants");

const ESLINT_CLI = new CLIEngine({ fix: true, cwd: APP_ROOT_PATH, configFile: ESLINTRC_FILE_PATH });

module.exports = (objectToStringify) => {
  return ESLINT_CLI.executeOnText(stringify(objectToStringify)).results[0].output;
};

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].