Skip to content

Instantly share code, notes, and snippets.

@pichillilorenzo
Created May 3, 2020 21:00
Show Gist options
  • Save pichillilorenzo/89ccabd454bee0aebb8f4e2af88ef630 to your computer and use it in GitHub Desktop.
Save pichillilorenzo/89ccabd454bee0aebb8f4e2af88ef630 to your computer and use it in GitHub Desktop.
@jsonfilter() example
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