UNPKG

@openhps/core

Version:

Open Hybrid Positioning System - Core component

32 lines 1.23 kB
import { jsonMapMember } from 'typedjson'; import { DataSerializerUtils } from '../DataSerializerUtils'; /** * @param {Serializable<any>} keyConstructor Map key constructor * @param {Serializable<any>} valueConstructor Map value constructor * @param {SerializableMapMemberOptions} [options] Member options * @returns {PropertyDecorator} Property decorator */ export function SerializableMapMember(keyConstructor, valueConstructor, options) { return (target, propertyKey) => { if (valueConstructor === Object && options === undefined) { options = {}; options.deserializer = json => { const map = new Map(); Object.keys(json).forEach(key => { map.set(key, JSON.parse(json[key])); }); return map; }; options.serializer = map => { const json = {}; map.forEach((value, key) => { json[key] = JSON.stringify(value); }); return json; }; } const finalOptions = DataSerializerUtils.mergeMemberOptions(target, propertyKey, options); jsonMapMember(keyConstructor, valueConstructor, finalOptions)(target, propertyKey); DataSerializerUtils.updateMemberOptions(target, propertyKey, finalOptions); }; }