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 🙏

© 2026 – Pkg Stats / Ryan Hefner

ts-cjson

v1.0.0

Published

A zero-dependency TypeScript port of cJSON, the ultralightweight JSON parser in ANSI C.

Readme

ts-cJSON

A direct TypeScript translation of the ultralightweight JSON parser in ANSI C.

If you find this project useful, you can support this and further ports at ko-fi.com/scottmoore0.

License

MIT License

cJSON (original C version) - Copyright (c) 2009-2017 Dave Gamble and cJSON contributors

ts-cJSON (direct TypeScript translation) - Copyright (c) 2026 Scott Moore

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Usage

This is a direct translation of cJSON from C to TypeScript. The public API, data structures, and behavior are preserved as faithfully as possible.

To read more about cJSON, please see the original cJSON repository.

The key differences from the C version are:

  • Zero dependencies - all C standard library shims (memory management, string handling, formatted I/O) are contained in the source itself.
  • No manual memory management - JavaScript's garbage collector replaces malloc/free.
  • ES modules - files are linked with standard import/export statements.
  • Single-threaded - JavaScript's event loop model means thread-safety concerns from the C version do not apply.

Installation

Because the core library is contained in a single self-contained file (cJSON.ts), you can copy it directly into your project:

cp cJSON.ts /path/to/your/project/src/

If you prefer, you can also install the whole repository as a package:

git clone https://github.com/ScottMoore0/ts-cJSON.git

Importing

import { cJSON_Parse, cJSON_Print, cJSON_CreateObject, cJSON_AddStringToObject, cJSON_Delete } from './cJSON.js';

Quick example

import { cJSON_Parse, cJSON_Print, cJSON_CreateObject, cJSON_AddStringToObject, cJSON_AddNumberToObject, cJSON_Delete } from './cJSON.js';

// Parse JSON
const json = cJSON_Parse('{"name":"Awesome 4K","resolutions":[{"width":1280,"height":720}]}');

// Print JSON
const printed = cJSON_Print(json);
console.log(printed); // formatted JSON string

// Build JSON programmatically
const monitor = cJSON_CreateObject();
cJSON_AddStringToObject(monitor, "name", "Awesome 4K");
cJSON_AddNumberToObject(monitor, "width", 3840);
cJSON_AddNumberToObject(monitor, "height", 2160);

const output = cJSON_Print(monitor);
console.log(output);

// Cleanup
// Note: In the translated TypeScript, cJSON_Delete is still provided for API
// compatibility, but JavaScript's garbage collector handles most cleanup
// automatically. Use cJSON_Delete when you want to release internal buffers
// immediately or when porting existing C code verbatim.
cJSON_Delete(monitor);

Building

Unlike the original C version, ts-cJSON requires no compilation step. It is valid TypeScript (and JavaScript) source code that runs directly in Node.js, Deno, Bun, or modern browsers.

TypeScript Compiler

If your project uses TypeScript, add the file to your tsconfig.json:

{
  "compilerOptions": {
    "target": "ES2022",
    "module": "ESNext",
    "moduleResolution": "node",
    "strict": false,
    "esModuleInterop": true
  },
  "include": ["src/**/*.ts"]
}

Important: The translated code uses patterns that emulate C pointer arithmetic and unsafe type casts. It is intentionally not strict-compliant. You should isolate it in its own module (as shown above) and wrap it in a strictly-typed API surface for the rest of your application.

Node.js / tsx

Run directly without pre-compilation:

npx tsx cJSON.ts

Or with Deno:

deno run --allow-all cJSON.ts

Bundling

Because the library is self-contained with zero npm dependencies, it bundles cleanly with esbuild, Rollup, or Vite:

npx esbuild cJSON.ts --bundle --platform=node --outfile=dist/cjson.js

Data Structure

The C struct cJSON has been translated to a TypeScript class with identical field names:

class cJSON {
  next: any = null;
  prev: any = null;
  child: any = null;
  type: number = 0;
  valuestring: string = "";
  valueint: number = 0;
  valuedouble: number = 0;
  string: string = "";
}

Type checking functions (cJSON_IsString, cJSON_IsNumber, etc.) behave exactly as in the C version.

Tests

The repository includes the original cJSON test fixtures and the translated Unity test framework. To run the tests:

# If a test runner entry point is available
npx tsx tests/unity/test/tests/testunity.ts

Test data is located in:

  • tests/inputs/ - JSON parse/print round-trip fixtures
  • tests/json-patch-tests/ - JSON Patch / JSON Pointer test suites (for cJSON_Utils)
  • fuzzing/inputs/ - Fuzzing corpus

Caveats

The following limitations from the original C version still apply:

  • Zero character in strings - cJSON does not support strings containing \u0000.
  • UTF-8 only - Input must be valid UTF-8.
  • Floating point - IEEE-754 double precision is assumed.
  • Deep nesting limit - Nesting is limited to 1000 levels by default.
  • Case sensitivity - Object key lookups are case-sensitive.
  • Duplicate keys - Objects with duplicate member names are supported in parsing/printing, but cJSON_GetObjectItemCaseSensitive returns only the first match.

The following C-specific caveats do not apply to the TypeScript version:

  • Memory leaks - JavaScript's garbage collector eliminates manual malloc/free concerns.
  • Thread safety - JavaScript is single-threaded; no special thread-safety measures are needed.
  • C standard compliance - The code runs wherever TypeScript/JavaScript runs (Node.js, Deno, Bun, browsers).

Acknowledgements