Created
May 3, 2020 19:12
-
-
Save pichillilorenzo/122ae36dfc67e1b40a5f24c26f41c662 to your computer and use it in GitHub Desktop.
@JsonAppend() example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { JsonProperty, JsonClassType, ObjectMapper, JsonAppend } from 'jackson-js'; | |
@JsonAppend({attrs: [{ | |
value: 'version', | |
}]}) | |
class User { | |
@JsonProperty() @JsonClassType({type: () => [Number]}) | |
id: number; | |
@JsonProperty() @JsonClassType({type: () => [String]}) | |
email: string; | |
constructor(id: number, email: string) { | |
this.id = id; | |
this.email = email; | |
} | |
} | |
const user = new User(1, 'john.alfa@gmail.com'); | |
const objectMapper = new ObjectMapper(); | |
const jsonData = objectMapper.stringify<User>(user, { | |
attributes: { | |
version: 1.2 | |
} | |
}); | |
console.log(jsonData); | |
// {"id":1,"email":"john.alfa@gmail.com","version":1.2} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment