npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

yason

v0.0.3

Published

Yet Another Simple Object Notation - Object serialization in between JSON and YAML

Downloads

5

Readme

Yet Another Simple Object Notation (YASON)

YASON is a small superset of JSON, compatible with the YAML language. It extends JSON with support for (circular) references and typing (YAML tags).

This project was created as a support project for other projects and YASON strings should be easily filterable by line-based filtering tools (e.g. grep). Therefore YASON generators should not generate newlines by default, although they are supported.

YASON is JSON with the following YAML-compatible changes:

  • References: A referencable element can be defined by putting &refname in front of it and by using *refname to refer to it.
  • Classes/Types/Namespaces: Elements can be classified using YAML-compatible tags (!tag, !!tag). A tag is defined as an exclamation mark followed by a sequence of non-whitespace characters. The tag ends with a whitespace character.
  • By default no newlines at generation time, although newlines are supported by the parser, they should not be generated by the generator

A YASON stream is a sequence of YASON values. Two values are separated by a newline and any number of whitespaces.

Basic Usage

To serialize a single object, simply use the stringify method:

var YASON = require("yason");
var obj = {
  test: [1, "b", {cde: "fgh"}],
  another_test: {another_key: "another value"}
};
obj["circular-reference"] = obj;
obj["test"].push(obj["another_test"]);
console.log(YASON.stringify(obj));

To deserialize a single object, use the parse method:

var YASON = require("yason");
var str = '&root { lirum: "larum", "asdf": ["1", 2, "three", ' +
  '&ref0 {four: "5"}], root: *root, "refer to ref0": *ref0}';
console.log(YASON.parse(str));

Usage / API

Use const YASON = require('yason'); to include the YASON module in your nodejs application. The YASON object will then contain the following items:

  • Generator: YASON generator (serializer) class.
  • Parser: YASON parser (deserializer) class.
  • GeneratorTransform: A nodejs transform stream that transforms JavaScript values into YASON strings.
  • ParserTransform: A nodejs transform stream that transforms YASON strings (or Buffers) into JavaScript values. Since streams don't support writing null values, they can't be serialized using this transform.
  • stringify: Function that serializes a JavaScript value into a YASON string.
  • parse: Function that deserializes a YASON string into a JavaScript value.

YASON.stringify(value, options)

Converts the value (object, array, number, string, boolean, null) into a YASON string. The method supports the following options:

  • classifier: A function with the signature classifier(value, container) that returns the class (tag) that should be used for the value. The tag must not contain any whitespaces (including newlines). A "!" will be prepended automatically. If null is returned, no tag will be used for the value. value is the value that will be serialized next, container is the object or array that contains the value (or null).

YASON.parse(string)

Parses a YASON string and converts it to the corresponding value. The result may contain values of type YasonTaggedValue. Tagged values are values of the YASON string that were tagged with a !tag, e.g. because the classifier of the stringify method returned a non-null value.

Generator

The generator serializes JavaScript values into YASON strings.

var generator = new YASON.Generator(options)

Creates a new YASON generator object. Valid options are:

  • classifier: The classifier that should be used for the stringify(...) method. If stringify(...) is called with a different classifier option, that option will override the constructor option.
  • stringReferences: Options for string references. If the same string appears more than once, further appearances may be replaced with a *referenceId and the first appearance will be turned into a &referenceId reference definition. This is only done if some conditions are met, for now the only condition is the string length. ** stringReferences.minimumLength: Strings are only turned into references if the are at least minimumLength characters long.

generator.stringify(value, options)

See descriptions of YASON.stringify(value, options)

Parser

const Parser = YASON.Parser

The parser is a class with a single class-method Parser.parse(string).

Parser.parse(string)

Parses a string and converts it into a JavaScript value.

GeneratorTransform

A nodejs Transform that converts JavaScript values into YASON strings. It puts a newline after each serialized value.

var trans = new YASON.GeneratorTransform(options)

Create a new generator Transform object. The options parameter can contain the following settings:

  • generator: Options that will be passed through to the YASON.Generator constructor. See the description of the Generator constructor for more information.

You can use the Transform like any other nodejs Transform, e.g. pipe it to stdout:

var YasonGeneratorTransform = require("yason").GeneratorTransform;

var out = new YasonGeneratorTransform();
out.pipe(process.stdout);

out.write({lirum: "larum"});
out.write({bla: ["ble", "blu", "larum"], 1: [2, {3: 4}]});

ParserTransform

A nodejs Transform that converts YASON strings (Buffers) into JavaScript values. Values are seperated by any number of whitespaces of which at least one is a newline.

var trans = new YASON.ParserTransform(options)

Creates a new parser Transform object. Currently no options are supported. The transform can be used like any other nodejs Transform:

var YasonParserTransform = require("yason");

var input = new YasonParserTransform();
input.on("data", (data) => {
  console.log("data: ", data);
});

process.stdin.pipe(input);
input.resume();