Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

php-serialize

steelbrain159.7kMIT5.1.3TypeScript support: included

PHP serialize/unserialize in Javascript

readme

PHP-Serialize

It also supports Serializable objects decode. Here's how you can use them.

Installation

$ npm install php-serialize # If you're using npm
$ yarn add php-serialize # If you're using Yarn

Usage

import {serialize, unserialize} from 'php-serialize'

class User {
  constructor({ name, age }) {
    this.name = name
    this.age = age
  }
  serialize() {
    return JSON.stringify({ name: this.name, age: this.age })
  }
  unserialize(rawData) {
    const { name, age } = JSON.parse(rawData)
    this.name = name
    this.age = age
  }
}
const steel = new User({ name: 'Steel Brain', age: 17 })
const serialized = serialize(steel)
const unserialized = unserialize(serialized, { User: User }) // Passing available classes
console.log(unserialized instanceof User) // true

const serializedForNamespace = serialize(steel, {
  'MyApp\\User': User,
})
// ^ Above code will serialize User class to given name

API

export function serialize(
  item: any,
  phpToJsScope: Object = {},
  options: { encoding: 'utf8' | 'binary' } = { encoding: 'utf8' }
): string
export function unserialize(
  item: string,
  scope: Object = {},
  options: { strict: boolean, encoding: 'utf8' | 'binary' } = { strict: false, encoding: 'utf8' }
): any
export function isSerialized(
  item: any,
  strict: false
): boolean

License

This project is licensed under the terms of MIT License. See the License file for more info.

changelog

5.1.3

  • Maintenance release with updated homepage in manifest

5.1.2

  • Fix parsing conflict between floats and BigInt (Thanks @dev-airmaxx)

5.1.1

  • Mistakenly republished 5.1.0 as 5.1.1

5.1.0

  • Add support for BigInt (Thanks @at0mat)

5.0.1

  • Fix a bug in isSerialized (Thanks @maxbronnikov10)

5.0.0

  • Remove dependence on assert native module for validation, and use simple error instead (Thanks @eliandoran)

    Because this changes the error class from AssertionError to Error which might break downstream code, this is being tagged as semver-major.

4.1.1

  • Fix Map support for multiple elements (Thanks @trim21)

4.1.0

  • Add support for JS Maps as equivalent for unordered PHP objects (Thanks @trim21)

4.0.2

  • Workaround an npm publish issue

4.0.1

  • Emit declarations for typings, instead of using source

4.0.0

  • Add TS Typings - Thanks @vace
  • Convert arrays with missing keys to objects in unserialize
  • BREAKING Export modules in CJS and CommonJS.
  • Add support for parsing protected and private fields

3.0.1

  • Fix handling of shallow arrays (Thanks @neoaggelos)

3.0.0

  • Require at least Node v8
  • Add isSerialized method, mirrored from Wordpress source

2.1.0

  • Add support for encoding parameter for serialize/unserialize

2.0.1

  • Fix validation being too strict for pairs

2.0.0

  • Simplify internals
  • Validate input and Throw syntax errors

1.3.1

  • Fix serialization support for big numbers

1.3.0

  • Added support for namespaced serializations

1.2.5

  • Fixed support for multi-byte strings
  • Rewrote most of decode internals to work on Buffers instead of strings (external API still the same)

1.2.4

  • Move flow-bin to dev dependencies from dependencies (sorry guys!)

1.2.3

  • Fix decoding of empty arrays (Thanks @incadawr)

1.2.2

  • Fix encoding/decoding of multi-byte utf8 strings

1.2.1

  • Fix a bug where objects/Array guessing would fail when values were/not numeric.

1.2.0

  • Add support for __PHP_Incomplete_Class

1.1.1

  • Add support for deep serialization (Thanks @cantremember)

1.1.0

  • Complete rewrite
  • Fixed a lot of bugs/limitations
  • Added specs to ensure stability

1.0.0

  • Initial release