import { Long } from 'bson' ; import * as LosslessJSON from 'lossless-json' ; export namespace JsonUtil { const bsonLongReviver = ( key , value ) => { if ( typeof value === 'object' && value && 'isLosslessNumber' in value ) { try { return value . valueOf ( ) ; } catch ( e ) { return Long . fromString ( value . toString ( ) ) ; } } return value ; } ; const bsonLongReplacer = ( key , value ) => { if ( typeof value === 'bigint' || Long . isLong ( value ) ) { return new LosslessJSON . LosslessNumber ( value . toString ( ) ) ; } return value ; } ; export function parseLoseless ( obj ) { return LosslessJSON . parse ( obj , bsonLongReviver ) ; } export function stringifyLoseless ( obj ) { return LosslessJSON . stringify ( obj , bsonLongReplacer ) ; } }