Skip to content

Instantly share code, notes, and snippets.

@pichillilorenzo
Created May 3, 2020 19:01
Show Gist options
  • Save pichillilorenzo/abd542494c141d90784bc864a159e512 to your computer and use it in GitHub Desktop.
Save pichillilorenzo/abd542494c141d90784bc864a159e512 to your computer and use it in GitHub Desktop.
@JsonAnyGetter example
import { JsonProperty, JsonClassType, ObjectMapper, JsonAnyGetter } 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>();
@JsonAnyGetter()
public getOtherInfo(): Map<string, any> {
return this.otherInfo;
}
}
const objectMapper = new ObjectMapper();
const screenInfo = new ScreenInfo();
screenInfo.id = 'TradeDetails';
screenInfo.title = 'Trade Details';
screenInfo.width = 500;
screenInfo.height = 300;
screenInfo.otherInfo.set('xLocation', 400);
screenInfo.otherInfo.set('yLocation', 200);
const jsonData = objectMapper.stringify<ScreenInfo>(screenInfo);
console.log(jsonData);
// {"id":"TradeDetails","title":"Trade Details","width":500,"height":300,"xLocation":400,"yLocation":200}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment