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

serialize-any-object

v0.1.5

Published

SerializeAnything 是一个 TypeScript / Javascript 库,该库提供了 stringify 和 parse 函数,能够做到:

Downloads

27

Readme

Serialize Any Object

SerializeAnything 是一个 TypeScript / Javascript 库,该库提供了 stringify 和 parse 函数,能够做到:

  1. 支持序列化包含循环引用的嵌套对象
  2. 支持序列化匿名函数
  3. 支持反序列化时还原类型信息。

Setup

npm install serialize-any-object

Usage

import { stringify, parse } from 'serialize-any-object';

API

stringify(obj: any): string

将对象序列化为 JSON 字符串。

  • obj: 要序列化的对象。
  • 返回值:序列化后的 JSON 字符串。

parse(jsonString: string, classMap: Map<string, any>): T

将 JSON 字符串反序列化为对象。

  • jsonString: 要反序列化的 JSON 字符串。
  • classMap: 类名到类构造函数的映射。
  • 返回值:反序列化后的对象。

Example

import { parse, stringify } from 'serialize-any-object'

class Library {
  books: Book[] = []
  toString() {
    return `Library with ${this.books.length} books`
  }

  addBook(book: Book) {
    book.library = this
    this.books.push(book)
  }

  findMethod: (library: Library) => Book | undefined = library => library.books[0]
}

// This is a circular reference: books have a reference to library and library has a reference to books
// This is a problem for JSON.stringify, but not for our stringify function
class Book {
  library: Library | null = null
  constructor(public title: string, public author: string) {}
  toString() {
    return `${this.title} by ${this.author}`
  }
}

// This is a lambda function.
// Normally, it is impossible to serialize functions. But our's can.
const findTheGreatGatsby = (library: Library) => library.books.find(book => book.title === 'The Great Gatsby')

const library = new Library()
library.findMethod = findTheGreatGatsby

library.addBook(
  new Book('The Catcher in the Rye', 'J.D. Salinger'),
)
library.addBook(
  new Book('The Great Gatsby', 'F. Scott Fitzgerald'),
)

const libraryString = stringify(library)
// In general, the serialized json does not contain class information.
// But our's does. You should add a map of class names to class constructors.
const libraryParsed = parse<Library>(libraryString, new Map<string, any>([['Library', Library], ['Book', Book]]))