Last active
March 14, 2018 17:14
-
-
Save ahaverty/f9cc718e43ecaf2d49f57e473b92ab64 to your computer and use it in GitHub Desktop.
Json2Typescript Map type converter
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 { JsonConverter } from 'json2typescript'; | |
import { MapConvert } from './map-converter'; | |
import { Category } from '../index'; | |
@JsonConverter | |
export class CategoryConvert extends MapConvert<Category> { | |
deserialize(data: any): Map<string, Category> { | |
return this.deserializeMap<Category>(data, Category); | |
} | |
} |
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 { Category } from "./category"; | |
import { JsonProperty, JsonObject } from "json2typescript"; | |
import { CategoryConvert } from "./converters/category-converter"; | |
import { BooleanMapConvert } from "./converters/primitive-converters"; | |
@JsonObject | |
export class Inventory { | |
@JsonProperty("examplePrimitiveBooleanMapProperty", BooleanMapConvert, true) | |
private _examplePrimitiveBooleanMapProperty: Map<string, boolean> = new Map<string, boolean>(); | |
@JsonProperty("categories", CategoryConvert, true) | |
private _categories: Map<string, Category> = new Map<string, Category>(); | |
getCategory(id: string): Category { | |
return this._categories.get(id); | |
} | |
isOkay(id: string): boolean { | |
return this._examplePrimitiveBooleanMapProperty.get(id); | |
} | |
}; |
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 { JsonConverter, JsonCustomConvert } from 'json2typescript'; | |
import _ = require('lodash'); | |
@JsonConverter | |
export abstract class MapConvert<T> implements JsonCustomConvert<Map<string, T>> { | |
private _convert = new JsonConvert(); | |
serialize(data: Map<string, T>): any { | |
return this.serializeMap(data); | |
} | |
deserialize(data: any): Map<string, T> { | |
throw new Error(`deserialize not implemented`); | |
} | |
protected serializeMap(data: Map<string, T>) { | |
let map: { [key: string]: any; } = {}; | |
data.forEach((v, k) => { | |
map[k] = this._convert.serialize(v); | |
}); | |
return map; | |
} | |
protected deserializeMap<T>(data: any, T: {new()}): Map<string, T> { | |
let map: Map<string, T> = new Map<string, T>(); | |
_.forEach(data, (v, k) => { | |
map.set(k, this._convert.deserialize(v, T)); | |
}); | |
return map; | |
} | |
} |
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 { JsonConverter, JsonCustomConvert } from 'json2typescript'; | |
import _ = require('lodash'); | |
@JsonConverter | |
abstract class PrimitiveMapConvert<T> implements JsonCustomConvert<Map<string, T>> { | |
private _convert = new JsonConvert(); | |
serialize(data: Map<string, T>): any { | |
return this.serializeMap(data); | |
} | |
deserialize(data: any): Map<string, T> { | |
return this.deserializeMap<T>(data); | |
} | |
protected serializeMap(data: Map<string, T>) { | |
let map: { [key: string]: any; } = {}; | |
_.forEach(data, (v, k) => { | |
map[k] = v; | |
}); | |
return map; | |
} | |
protected deserializeMap<T>(data: any): Map<string, T> { | |
let map: Map<string, T> = new Map<string, T>(); | |
_.forEach(data, (v, k) => { | |
map.set(k, v); | |
}); | |
return map; | |
} | |
} | |
@JsonConverter | |
export class NumberMapConvert extends PrimitiveMapConvert<number> {} | |
@JsonConverter | |
export class BooleanMapConvert extends PrimitiveMapConvert<boolean> {} | |
@JsonConverter | |
export class StringMapConvert extends PrimitiveMapConvert<string> {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment