Created
May 3, 2020 19:08
-
-
Save pichillilorenzo/18e8cb5082d77a06e7d83979c2f9dd23 to your computer and use it in GitHub Desktop.
@JsonAnySetter() 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, 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