All files / src/serializer index.ts

91.3% Statements 21/23
83.33% Branches 10/12
80% Functions 4/5
90.9% Lines 20/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55                            1x 1x 1x   1x 3x     1x       1x 4x 78x       32x   46x     46x 18x   28x 18x   10x 10x 40x   10x       4x      
import {abiEncode} from './encoder'
import {abiDecode} from './decoder'
import {ABISerializable, ABISerializableConstructor, synthesizeABI} from './serializable'
 
export {ABIEncoder} from './encoder'
export {ABIDecoder} from './decoder'
 
export type {
    ABISerializable,
    ABISerializableType,
    ABISerializableObject,
    ABISerializableConstructor,
} from './serializable'
 
export namespace Serializer {
    export const encode = abiEncode
    export const decode = abiDecode
    /** Create an Antelope/EOSIO ABI definition for given core type. */
    export function synthesize(type: ABISerializableConstructor) {
        return synthesizeABI(type).abi
    }
    /** Create JSON representation of a core object. */
    export function stringify(object: ABISerializable) {
        return JSON.stringify(object)
    }
    /** Create a vanilla js representation of a core object. */
    export function objectify(object: ABISerializable) {
        const walk = (v: any) => {
            switch (typeof v) {
                case 'boolean':
                case 'number':
                case 'string':
                    return v
                case 'object': {
                    Iif (v === null) {
                        return v
                    }
                    if (typeof v.toJSON === 'function') {
                        return walk(v.toJSON())
                    }
                    if (Array.isArray(v)) {
                        return v.map(walk)
                    }
                    const rv: any = {}
                    for (const key of Object.keys(v)) {
                        rv[key] = walk(v[key])
                    }
                    return rv
                }
            }
        }
        return walk(object)
    }
}