edcoreweb / json-class-serializer

Serialize complex classes to and from json.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

json-class-serializer

Serialize javascript classes to and from json.

class User {
    constructor (id, status) {
        this.id = id
        this.status = status
    }
}

class UserCollection extends Array {
    getActive () {
        return this.filter(item => item.status === 'active')
    }
}
const serializer = new Serializer([UserCollection, User])

const users = new UserCollection(
    new User(1, 'active'),
    new User(2, 'inactive'),
)

const serialized = serializer.stringify(users)

const newUsers = serializer.parse(serialized)

console.log(users, serialized, newUsers)

About

Serialize complex classes to and from json.


Languages

Language:JavaScript 100.0%