• Adrian Sutton's avatar
    cannon: Support binary serialisation for snapshots (#11718) · 67ba188e
    Adrian Sutton authored
    * cannon: Add serialize utils for binary formats and automatic binary/json detection.
    
    * cannon: Support reading and writing states as binary or JSON
    
    * cannon: Generate mt prestate as gzipped binary.
    
    Use different versions for singlethreaded and multithreaded states.
    
    * cannon: Improve comments for serialization
    
    * cannon: Review feedback
    
    * cannon: Introduce reader and writer helpers to simplify code.
    67ba188e
detect.go 602 Bytes
package serialize

import (
	"os"
	"strings"

	"github.com/ethereum-optimism/optimism/op-service/jsonutil"
)

func Load[X any](inputPath string) (*X, error) {
	if isBinary(inputPath) {
		return LoadSerializedBinary[X](inputPath)
	}
	return jsonutil.LoadJSON[X](inputPath)
}

func Write[X Serializable](outputPath string, x X, perm os.FileMode) error {
	if isBinary(outputPath) {
		return WriteSerializedBinary(outputPath, x, perm)
	}
	return jsonutil.WriteJSON[X](outputPath, x, perm)
}

func isBinary(path string) bool {
	return strings.HasSuffix(path, ".bin") || strings.HasSuffix(path, ".bin.gz")
}