/**
 * All JSON must go through the metadata serializer so we can handle proper
 * serialization but also object validation once they are deserialized.
 */ import { SerializedObject } from './SerializedObject' ; import { Preconditions } from 'polar-shared/src/Preconditions' ; export class MetadataSerializer { public static serialize ( object , spacing = "" ) { return JSON . stringify ( object , null , spacing ) ; } param object { Object } param data public static deserialize ( obj , data ) { Preconditions . assertPresent ( data , 'data' ) ; if ( typeof data !== "string" ) { throw new Error ( "We can only deserialize strings: " + typeof data ) ; } const parsed = JSON . parse ( data ) ; Object . assign ( obj , parsed ) ; return obj ; } private static replacer ( key , value ) { if ( value instanceof SerializedObject ) { value . setup ( ) ; value . validate ( ) ; } return value ; } private static reviver ( key , value ) { if ( value instanceof SerializedObject ) { value . setup ( ) ; value . validate ( ) ; } return value ; } }