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

acos-json-encoder

v1.0.2

Published

JSON serializer and deserializer used by ACOS.games

Downloads

5

Readme

ACOS JSON Encoder for Websocket Networking

Efficiently encode your JSON object or array into serialized bytes for primitive types, also includes a string dictionary to reduce bandwidth bytes transmitted to players.

This package should be used in combination with the acos-json-delta to maximize bandwidth reduction.

Installation

npm i acos-json-encoder

Usage

CommonJS (nodejs)

const { encode, decode, createDefaultDict } = require("acos-json-encoder");

ES6 (webpack, vite, etc)

import {encode, decode, createDefaultDict} = require("acos-json-encoder");

Encoding and Decoding example

// example dictionary to match any strings that might appear in JSON
// this will reduce these strings into two bytes when detected
let myDictionary = [
  "name",
  "rank",
  "score",
  "rating",
  "teamid",
  "ready",
  "type",
  "team_o",
  "team_x",
];

// example JSON data to encode/decode
let jsonData = {
  iobYl: {
    name: "Player2326",
    rank: 0,
    score: 0,
    rating: 2636,
    teamid: "team_o",
    ready: true,
    type: "X",
  },
  DjTS3: {
    name: "Player7145",
    rank: 0,
    score: 0,
    rating: 2364,
    teamid: "team_x",
    ready: true,
    type: "O",
  },
};

// set the dictionary to use by default
createDefaultDict(myDictionary);

// encode and serialize the data into bytes
let jsonEncoded = encode(jsonData);

// decode the bytes back into a JSON string
let decoded = decode(jsonEncoded);

// validate the original matches the decoded
if (JSON.stringify(jsonData) == JSON.stringify(decoded)) {
  console.log("Encoding MATCHES");
}

// print byte sizes
console.log("JSON string size: ", JSON.stringify(jsonData).length);
console.log("Encoded JSON size:", jsonEncoded.byteLength);

// -------- output --------------
// Encoding MATCHES
// JSON string size:  211
// Encoded JSON size: 94

Methods

createDefaultDict(dictionary)

Setup the default dictionary to use by this package.

Parameters
  • dictionary (array of strings) - dictionary to use for this encoding.
Returns

Dictionary object used internally. You should not need to use it.

.

encode(json, [dictionary])

Parameters
  • json (object or array) - the data you want to encode into bytes
  • dictionary (array of strings) - OPTIONAL dictionary to use for this encoding.

Encode the JSON data with optional dictionary. If no dictionary specified will use the one set using createDefaultDict.

Returns

A byte array of type Uint8Array. Compatible with WebSocket's send using bytes instead of string.

.

decode(bytes, [dictionary])

Parameters
  • bytes (byte array) - the byte data to decode into JSON data
  • dictionary (array of strings) - OPTIONAL dictionary to use for this encoding.

Decode the byte array with optional dictionary. If no dictionary specified will use the one set using createDefaultDict.

Returns

JSON data. Compatible with WebSocket's receive callback using the message raw data in byte format.