import { SuperJSONResult , SuperJSONValue , Class , JSONValue } from './types' ; import { ClassRegistry , RegisterOptions } from './class-registry' ; import { SymbolRegistry } from './symbol-registry' ; import { CustomTransfomer , CustomTransformerRegistry , } from './custom-transformer-registry' ; import { allowErrorProps } from './error-props' ; import { walker , applyReferentialEqualityAnnotations , applyValueAnnotations , generateReferentialEqualityAnnotations , } from './plainer' ; import cloneDeep from 'lodash.clonedeep' ; export const serialize = ( object ) : SuperJSONResult => { const identities = new Map < any , any [ ] [ ] > ( ) ; const output = walker ( object , identities ) ; const res = { json : output . transformedValue } ; if ( output . annotations ) { res . meta = { ... res . meta , values : output . annotations } ; } const equalityAnnotations = generateReferentialEqualityAnnotations ( identities ) ; if ( equalityAnnotations ) { res . meta = { ... res . meta , referentialEqualities : equalityAnnotations } ; } return res ; } ; export const deserialize = < T = unknown > ( payload ) : T => { const { json , meta } = payload ; let result = cloneDeep ( json ) as any ; if ( meta ?. values ) { result = applyValueAnnotations ( result , meta . values ) ; } if ( meta ?. referentialEqualities ) { result = applyReferentialEqualityAnnotations ( result , meta . referentialEqualities ) ; } return result ; } ; const stringify = ( object ) : string => JSON . stringify ( serialize ( object ) ) ; export const parse = < T = unknown > ( string ) : T => deserialize ( JSON . parse ( string ) ) ; const registerClass = ( v , options ? ) => ClassRegistry . register ( v , options ) ; const registerSymbol = ( v , identifier ? ) => SymbolRegistry . register ( v , identifier ) ; const registerCustom = < I , O extends JSONValue > ( transformer , name ) => CustomTransformerRegistry . register ( { name , ... transformer } ) ; export default { stringify , parse , serialize , deserialize , registerClass , registerSymbol , registerCustom , allowErrorProps } ;