Created
May 3, 2020 19:01
-
-
Save pichillilorenzo/abd542494c141d90784bc864a159e512 to your computer and use it in GitHub Desktop.
@JsonAnyGetter 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, 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