Created
May 3, 2020 18:43
-
-
Save pichillilorenzo/68b740b597bd60a191bd857f550c9cef to your computer and use it in GitHub Desktop.
@JsonAlias() 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, JsonAlias, ObjectMapper } from 'jackson-js'; | |
class Book { | |
@JsonProperty() @JsonClassType({type: () => [String]}) | |
name: string; | |
@JsonProperty() @JsonClassType({type: () => [String]}) | |
@JsonAlias({values: ['bkcat', 'mybkcat']}) | |
category: string; | |
} | |
class Writer { | |
@JsonProperty() @JsonClassType({type: () => [Number]}) | |
id: number; | |
@JsonProperty() @JsonClassType({type: () => [String]}) | |
name: string; | |
@JsonProperty() @JsonClassType({type: () => [Array, [Book]]}) | |
books: Book[] = []; | |
} | |
const objectMapper = new ObjectMapper(); | |
// eslint-disable-next-line max-len | |
const jsonData = '{"id":1,"name":"John","books":[{"name":"Learning TypeScript","bkcat":"Web Development"},{"name":"Learning Spring","mybkcat":"Java"}]}'; | |
const writer = objectMapper.parse<Writer>(jsonData, {mainCreator: () => [Writer]}); | |
console.log(writer); | |
/* | |
Writer { | |
books: [ | |
Book { name: 'Learning TypeScript', category: 'Web Development' }, | |
Book { name: 'Learning Spring', category: 'Java' } | |
], | |
id: 1, | |
name: 'John' | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment