Created
May 3, 2020 21:00
-
-
Save pichillilorenzo/89ccabd454bee0aebb8f4e2af88ef630 to your computer and use it in GitHub Desktop.
@jsonfilter() 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 { JsonClassType, JsonProperty, ObjectMapper, JsonFilter, JsonFilterType } from 'jackson-js'; | |
@JsonFilter({value: 'studentFilter'}) | |
class Student { | |
@JsonProperty({value: 'stdName'}) @JsonClassType({type: () => [String]}) | |
name: string; | |
@JsonProperty() @JsonClassType({type: () => [Number]}) | |
age: number; | |
@JsonProperty() @JsonClassType({type: () => [String]}) | |
college: string; | |
@JsonProperty() @JsonClassType({type: () => [String]}) | |
city: string; | |
constructor(name: string, age: number, college: string, city: string) { | |
this.name = name; | |
this.age = age; | |
this.college = college; | |
this.city = city; | |
} | |
} | |
const student = new Student('Mohit', 30, 'ABCD', 'Varanasi'); | |
const objectMapper = new ObjectMapper(); | |
const jsonData = objectMapper.stringify<Student>(student, { | |
filters: { | |
studentFilter: { | |
type: JsonFilterType.SERIALIZE_ALL_EXCEPT, | |
values: ['stdName', 'city'] | |
} | |
} | |
}); | |
console.log(jsonData); | |
// {"age":30,"college":"ABCD"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment