php-serialize
TypeScript icon, indicating that this package has built-in type declarations

4.1.1 • Public • Published

PHP-Serialize

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

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.

Readme

Keywords

none

Package Sidebar

Install

npm i php-serialize

Weekly Downloads

38,352

Version

4.1.1

License

MIT

Unpacked Size

35.7 kB

Total Files

21

Last publish

Collaborators

  • steelbrain