2019-03-15 admin


什么是w-json,A small module to write JSON files.



Support me on Patreon Buy me a book PayPal Ask me anything Version Downloads Get help on Codementor

Buy Me A Coffee

A small module to write JSON files.

If you want to read JSON files, check out r-json.

<g-emoji class=“g-emoji” alias=“cloud” fallback-src=“https://github.githubassets.com/images/icons/emoji/unicode/2601.png”>☁️</g-emoji> Installation

# Using npm
npm install --save w-json

# Using yarn
yarn add w-json

<g-emoji class=“g-emoji” alias=“clipboard” fallback-src=“https://github.githubassets.com/images/icons/emoji/unicode/1f4cb.png”>📋</g-emoji> Example

// Dependencies
var WriteJson = require("w-json");

// Write the same file synchronously
WriteJson(__dirname + "/test.json", { "hello": "mars" });

// 4 space indent and new line at the end
WriteJson(__dirname + "/test.json", { "hello": "mars" }, {
    new_line: true
  , space: 4

// Write test.json asynchronously
WriteJson(__dirname + "/test.json", { "hello": "world" }, function (err, data) {
    console.log(err || "written");

<g-emoji class=“g-emoji” alias=“question” fallback-src=“https://github.githubassets.com/images/icons/emoji/unicode/2753.png”>❓</g-emoji> Get Help

There are few ways to get help:

  1. Please post questions on Stack Overflow. You can open issues with questions, as long you add a link to your Stack Overflow question.

  2. For bug reports and feature requests, open issues. <g-emoji class=“g-emoji” alias=“bug” fallback-src=“https://github.githubassets.com/images/icons/emoji/unicode/1f41b.png”>🐛</g-emoji>

  3. For direct and quick help, you can use Codementor. <g-emoji class=“g-emoji” alias=“rocket” fallback-src=“https://github.githubassets.com/images/icons/emoji/unicode/1f680.png”>🚀</g-emoji>

<g-emoji class=“g-emoji” alias=“memo” fallback-src=“https://github.githubassets.com/images/icons/emoji/unicode/1f4dd.png”>📝</g-emoji> Documentation

wJson(path, data, options, callback)

Writes a JSON file.


  • String path: The JSON file path.

  • Object data: The JSON data to write in the provided file.

  • Object|Number|Boolean options: An object containing the fields below. If boolean, it will be handled as new_line, if number it will be handled as space.

  • space (Number): An optional space value for beautifying the json output (default: 2).

  • new_line (Boolean): If true, a new line character will be added at the end of the stringified content.

  • Function callback: An optional callback. If not passed, the function will run in sync mode.

<g-emoji class=“g-emoji” alias=“yum” fallback-src=“https://github.githubassets.com/images/icons/emoji/unicode/1f60b.png”>😋</g-emoji> How to contribute

Have an idea? Found a bug? See how to contribute.

<g-emoji class=“g-emoji” alias=“sparkling_heart” fallback-src=“https://github.githubassets.com/images/icons/emoji/unicode/1f496.png”>💖</g-emoji> Support my projects

I open-source almost everything I can, and I try to reply to everyone needing help using these projects. Obviously, this takes time. You can integrate and use these projects in your applications for free! You can even change the source code and redistribute (even resell it).

However, if you get some profit from this or just want to encourage me to continue creating stuff, there are few ways you can do it:

  • Starring and sharing the projects you like <g-emoji class=“g-emoji” alias=“rocket” fallback-src=“https://github.githubassets.com/images/icons/emoji/unicode/1f680.png”>🚀</g-emoji>

  • Buy me a book—I love books! I will remember you after years if you buy me one. <g-emoji class=“g-emoji” alias=“grin” fallback-src=“https://github.githubassets.com/images/icons/emoji/unicode/1f601.png”>😁</g-emoji> <g-emoji class=“g-emoji” alias=“book” fallback-src=“https://github.githubassets.com/images/icons/emoji/unicode/1f4d6.png”>📖</g-emoji>

  • PayPal—You can make one-time donations via PayPal. I’ll probably buy a coffee tea. <g-emoji class=“g-emoji” alias=“tea” fallback-src=“https://github.githubassets.com/images/icons/emoji/unicode/1f375.png”>🍵</g-emoji>

  • Support me on Patreon—Set up a recurring monthly donation and you will get interesting news about what I’m doing (things that I don’t share with everyone).

  • Bitcoin—You can send me bitcoins at this address (or scanning the code below): 1P9BRsmazNQcuyTxEqveUsnf5CERdq35V6

Thanks! <g-emoji class=“g-emoji” alias=“heart” fallback-src=“https://github.githubassets.com/images/icons/emoji/unicode/2764.png”>❤️</g-emoji>

<g-emoji class=“g-emoji” alias=“dizzy” fallback-src=“https://github.githubassets.com/images/icons/emoji/unicode/1f4ab.png”>💫</g-emoji> Where is this library used?

If you are using this library in one of your projects, add it in this list. <g-emoji class=“g-emoji” alias=“sparkles” fallback-src=“https://github.githubassets.com/images/icons/emoji/unicode/2728.png”>✨</g-emoji>

  • auto-geo-sunset (by Amin El-Naggar)—Automagically figures out your location using your ip, and uses that to calculate the time of sunset.
  • babel-it—Babelify your code before npm publish.
  • bible—Read the Holy Bible via the command line.
  • birthday—Know when a friend’s birthday is coming.
  • blah—A command line tool to optimize the repetitive actions.
  • bloggify (by Bloggify)—We make publishing easy.
  • bloggify-cli (by Bloggify)—We make publishing easy.
  • cdnjs-importer—Easy way to import a library into CDNJS.
  • cli-sunset—A fancy command line tool for knowing the sunset time.
  • edit-json-file—Edit a json file with ease.
  • engine-app (by jillix)—Engine app related helper functions.
  • engine-composition-crud (by jillix)—The default module for creating, reading, updating and deleting Engine instances.
  • engine-parser (by jillix)—Engine composition parser.
  • engine-tools (by jillix)—Engine Tools library and CLI app.
  • git-stats—Local git statistics including GitHub-like contributions calendars.
  • idea—A lightweight CLI tool and module for keeping ideas in a safe place quick and easy.
  • machine-ip—Update your public ip information in a git repository.
  • made-in-brazil—A list of neat projects made in Brazil.
  • made-in-india—A list of neat projects made in India.
  • made-in-romania—A list of cool projects made in Romania.
  • packy—Set default fields in your package.json files.
  • ship-release—Publish new versions on GitHub and npm with ease.
  • tithe—Organize and track the tithe payments.
  • w-package-json—Write package.json files.
  • web-term—A full screen terminal in your browser.

<g-emoji class=“g-emoji” alias=“scroll” fallback-src=“https://github.githubassets.com/images/icons/emoji/unicode/1f4dc.png”>📜</g-emoji> License

MIT © Ionică Bizău


转载请注明:文章转载自 JavaScript中文网 [https://www.javascriptcn.com]