import { IJsonSerializer, jsonSerializer } from './IJsonSerializer'; import { Json, JsonArray } from './Json'; import { Type } from '../../Type'; import { UnexpectedJsonError } from './UnexpectedJsonError'; /** * Json serializer for the native Map class */ export class MapJsonSerializer implements IJsonSerializer> { public readonly [jsonSerializer] = true; public serialize(object: ReadonlyMap): Json { return [...object] as JsonArray; } public deserialize(json: Json): ReadonlyMap { if (!Type.isArray(json)) { throw new UnexpectedJsonError(json); } return new Map(json as Iterable<[K, V]>); } }