solana-flux-aggregator/src/json.ts

52 lines
1.1 KiB
TypeScript
Raw Normal View History

2021-02-19 05:13:56 -08:00
import { PublicKey } from "solray"
import fs from "fs"
2021-02-20 01:05:05 -08:00
import BN from "bn.js"
2021-02-19 05:13:56 -08:00
export function jsonReviver(_key: string, val: any) {
if (val && typeof val == "object") {
if (val["type"] == "PublicKey") {
return new PublicKey(val.base58)
}
2021-02-24 18:52:20 -08:00
if (val["type"] == "Buffer") {
return Buffer.from(val.hex, "hex")
}
2021-02-19 05:13:56 -08:00
}
return val
}
export function jsonReplacer(key: string, value: any) {
2021-02-20 01:05:05 -08:00
if (value && typeof value == "object") {
if (value.constructor == PublicKey) {
return {
type: "PublicKey",
base58: value.toBase58(),
}
2021-02-19 05:13:56 -08:00
}
2021-02-24 18:52:20 -08:00
// The Buffer class defines a `toJSON` method that returns:
//
// {
// type: 'Buffer',
// data: [
// 100, 101, 97, 100,
// 98, 101, 97, 102
// ]
// }
//
// Convert this to an hex string
if (value.type == "Buffer") {
return {
type: "Buffer",
hex: Buffer.from(value).toString("hex"),
}
}
2021-02-19 05:13:56 -08:00
}
return value
}
export function loadJSONFile<T>(file: string): T {
return JSON.parse(fs.readFileSync(file, "utf8"), jsonReviver)
}