Skip to content

Instantly share code, notes, and snippets.

@pichillilorenzo
Created May 3, 2020 19:08
Show Gist options
  • Save pichillilorenzo/18e8cb5082d77a06e7d83979c2f9dd23 to your computer and use it in GitHub Desktop.
Save pichillilorenzo/18e8cb5082d77a06e7d83979c2f9dd23 to your computer and use it in GitHub Desktop.
@JsonAnySetter() example
import { JsonProperty, JsonClassType, ObjectMapper, JsonAnySetter } from 'jackson-js';
class ScreenInfo {
@JsonProperty() @JsonClassType({type: () => [String]})
id: string;
@JsonProperty() @JsonClassType({type: () => [String]})
title: string;
@JsonProperty() @JsonClassType({type: () => [Number]})
width: number;
@JsonProperty() @JsonClassType({type: () => [Number]})
height: number;
@JsonProperty() @JsonClassType({type: () => [Map, [String, Object]]})
otherInfo: Map<string, any> = new Map<string, any>();
@JsonAnySetter()
public setOtherInfo(propertyKey: string, value: any) {
this.otherInfo.set(propertyKey, value);
}
}
const jsonData = '{"id":"TradeDetails","title":"Trade Details","width":500,"height":300,"xLocation":400,"yLocation":200}';
const objectMapper = new ObjectMapper();
const screenInfo = objectMapper.parse<ScreenInfo>(jsonData, {mainCreator: () => [ScreenInfo]});
console.log(screenInfo);
/*
ScreenInfo {
otherInfo: Map(2) { 'xLocation' => 400, 'yLocation' => 200 },
id: 'TradeDetails',
title: 'Trade Details',
width: 500,
height: 300
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment