Skip to content

Repository files navigation

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'classUser{constructor({ name, age }){this.name=namethis.age=age}serialize(){returnJSON.stringify({name: this.name,age: this.age})}unserialize(rawData){const{ name, age }=JSON.parse(rawData)this.name=namethis.age=age}}conststeel=newUser({name: 'Steel Brain',age: 17})constserialized=serialize(steel)constunserialized=unserialize(serialized,{User: User})// Passing available classesconsole.log(unserializedinstanceofUser)// trueconstserializedForNamespace=serialize(steel,{'MyApp\\User': User,})// ^ Above code will serialize User class to given name

API

export functionserialize(item: any,phpToJsScope: Object={},options: {encoding: 'utf8'|'binary'}={encoding: 'utf8'}): string
export functionunserialize(item: string,scope: Object={},options: {strict: boolean,encoding: 'utf8'|'binary'}={strict: false,encoding: 'utf8'}): any
export functionisSerialized(item: any,strict: false): boolean

License

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

About

PHP Serialize/Unserialize in Javascript

Resources

Stars

120 stars

Watchers

2 watching

Forks

Releases

Used by

Contributors

Languages