Skip to content

edcoreweb/json-class-serializer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

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)