项目作者: any-json

项目描述 :
Convert (almost) anything to JSON
高级语言: TypeScript
项目地址: git://github.com/any-json/any-json.git
创建时间: 2015-02-03T19:27:42Z
项目社区:https://github.com/any-json/any-json

开源协议:MIT License

下载


any-json

any-json can be used to convert (almost) anything to and from JSON.

NPM version

Install from npm

  1. npm install any-json -g

Examples

convert

  1. # Prints as JSON to standard out
  2. any-json package.json5
  3. # Writes the contents from `package.json` to `package.json5` as JSON5
  4. any-json package.json package.json5
  5. # Same as above (the `convert` command is default).
  6. any-json convert package.json5
  7. any-json convert package.json package.json5

combine

  1. # Prints an JSON array containing an item for every JSON file in directory
  2. any-json combine *.json
  3. # Combines A.json and B.json, writing the result to C.json
  4. any-json combine A.json B.json --out C.json
  5. # Create a csv from a collection of flat YAML files
  6. any-json combine *.yaml --out=data.csv

split

  1. # Creates a JSON file for each row in the CSV where the name is based on the `product_id` column
  2. any-json split products.csv prod-{product_id}.json

Formats and Data Safety

Safe

When only JSON features are used, conversion should not result in any data loss when using these formats.

  • cson
  • hjson
  • json
  • json5
  • yaml

Problematic

Some loss of information may occur. Improved parsers/serializers could provide better compatibility, but implementation is provided as-is. Known issues are listed below the format.

  • ini
    • All numbers are converted to strings (the ini library would need to quote strings in order be recognize numbers)
    • In the unlikely case an object was not an array but has only has sequential, numeric members starting at zero it will be decoded as an array.
  • toml
    • Top-level array and date objects are not supported
      • It would be non-standard, but this could be worked around by wrapping the content in an object (then discarding the object when decoding).
  • xml

Limited

These formats are conceptually different and only work on a limited basis. With effort, conventions could be established to provide a more complete transfer but there will be some impedance.

Tabular formats:

  • csv
  • xls
  • xlsx

Usage

Command Line

  1. usage: any-json [command] FILE [options] [OUT_FILE]
  2. any-json can be used to convert (almost) anything to JSON.
  3. This version supports:
  4. cson, csv, hjson, ini, json, json5, toml, yaml
  5. This version has is beta support for:
  6. xls, xlsx
  7. The beta formats should work, but since they are new,
  8. behavior may change in later releases in response to feedback
  9. without requiring a major version update.
  10. command:
  11. convert convert between formats (default when none specified)
  12. combine combine multiple documents
  13. split spilts a document (containing an array) into multiple documents
  14. options:
  15. -?, --help Prints this help and exits.
  16. --version Prints version information and exits.
  17. --input-format=FORMAT Specifies the format of the input (assumed by file
  18. extension when not provided).
  19. --output-format=FORMAT Specifies the format of the output (default: json or
  20. assumed by file extension when available).
  21. combine (additional options):
  22. --out=OUT_FILE The output file.

API

  1. const anyjson = require('any-json')
  2. const obj = await anyjson.decode(/* string to parse */, /* format (string) */)
  3. const str = await anyjson.encode(/* object to encode */, /* desired format (string) */)

Contributing

Contributions welcome! If any-json is not meeting your needs or your have an idea for an improvement, please open an issue or create a pull request.

History

For detailed release history, see Releases.

  • v3 Re-written to be Promise-based and bi-directional (serialization capabilities as well as parsing).
  • v2 removed the experimental/unreliable format detection.