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 🙏

© 2025 – Pkg Stats / Ryan Hefner

json-4-ts

v1.1.8

Published

Serialize virtually any type of data

Readme

📦 json-4-ts

json-4-ts is a TypeScript serialization library for encoding and decoding complex JavaScript/TypeScript data structures—including objects, strings, numbers, functions, and symbols—into a structured and deserializable format.


🚀 Features

  • Serialize and deserialize:
    • Objects
    • Strings
    • Numbers
    • Functions (native and user-defined)
    • Symbols
  • Custom format with type tagging
  • Preserves structure and re-creates function bodies
  • Handles unknown and invalid data gracefully

📦 Installation

Since this is a namespace-based library, simply include it in your TypeScript code:

/// <reference path="path/to/json-4-ts.ts" />

Or manually import the Serializer namespace if modularized.


📘 Usage

Serialize

const obj = {
  name: "Alice",
  greet: (msg: string) => `Hello, ${msg}`,
  id: Symbol("unique")
};

const serialized = Serializer.serialize(obj);
console.log(JSON.stringify(serialized, null, 2));

Deserialize

const restored = Serializer.deserialize(serialized);
console.log(restored.name);         // "Alice"
console.log(restored.greet("Bob")); // "Hello, Bob"

🧩 API Reference

Serializer.serialize(data: SerializableDataType): SerializedBlock

Serializes supported JavaScript/TypeScript types into a structured block.

Serializer.deserialize(block: SerializedBlock): SerializableDataType

Restores serialized blocks to live runtime objects, functions, or primitives.


🔧 Type Definitions

SerializableDataType

type SerializableDataType =
  | Serializable
  | Record<string | number | symbol, any>
  | string | String
  | number | Number
  | Function
  | symbol | Symbol;

Serializable

type Serializable = {
  value: SerializableDataType;
};

SerializedBlock

type SerializedBlock = {
  type: string;
  value: SerializedBlockType;
};

SerializedBlockType

type SerializedBlockType =
  | ContextBlock
  | StringBlock
  | NumberBlock
  | ObjectBlock
  | FunctionBlock
  | SymbolBlock
  | null;

Specialized Block Types

StringBlock

type StringBlock = {
  string: string;
};

NumberBlock

type NumberBlock = {
  number: number;
};

ObjectBlock

type ObjectBlock = {
  object: Record<string | number | symbol, SerializedBlock>;
};

FunctionBlock

type FunctionBlock = {
  function: string;
  context: ContextBlock;
};

SymbolBlock

type SymbolBlock = {
  symbol: string;
};

ContextBlock

type ContextBlock = {
  context: {
    this: any;
  };
};

🧠 Symbols

The following symbols are used for fallback states:

Serializer.unknown // Symbol("Unknown data")
Serializer.invalid // Symbol("Invalid data")
  • unknown: Used when a function cannot be serialized
  • invalid: Returned if deserialization fails

🧪 Example

const data = {
  n: 42,
  message: "Hello",
  run: (x: number) => x + 1,
  flag: Symbol("secret")
};

const s = Serializer.serialize(data);
console.log(s);

const restored = Serializer.deserialize(s);
console.log(restored.run(5)); // 6

⚠️ Limitations

  • Function serialization is based on toString() and may not preserve closure scope
  • Native functions may only serialize by name (if they can be resolved with eval)
  • The "this" context is stored as null during serialization
  • Symbols are stored as strings (description only)

📄 License

MIT License