Skip to content

File I/O Methods

Processor provides JSON file read/write and streaming load methods, covering files, io.Reader, and io.Writer.

File Loading

LoadFromFile

Signature: func (p *Processor) LoadFromFile(filePath string, cfg ...Config) (string, error)

Loads JSON data from a file and returns the raw string.

go
data, err := p.LoadFromFile("config.json")
if err != nil {
    panic(err)
}
fmt.Println(data) // Raw JSON string

LoadFromFileAsData (Now Private)

API Change Notice

LoadFromFileAsData has been converted to an internal method (loadFromFileAsData) and is no longer exported as a public API. Please use the LoadFromFile + Parse combination instead:

go
jsonStr, err := p.LoadFromFile("data.json")
if err != nil {
    panic(err)
}
var data any
err = p.Parse(jsonStr, &data)
// data type is map[string]any or []any
if obj, ok := data.(map[string]any); ok {
    fmt.Println(obj["name"])
}

Reader Loading

LoadFromReader

Signature: func (p *Processor) LoadFromReader(reader io.Reader, cfg ...Config) (string, error)

Loads JSON data from a Reader and returns the raw string.

go
file, _ := os.Open("data.json")
defer file.Close()

data, err := p.LoadFromReader(file)
if err != nil {
    panic(err)
}

LoadFromReaderAsData (Now Private)

API Change Notice

LoadFromReaderAsData has been converted to an internal method (loadFromReaderAsData) and is no longer exported as a public API. Please use the LoadFromReader + Parse combination instead:

go
file, _ := os.Open("data.json")
defer file.Close()

jsonStr, err := p.LoadFromReader(file)
if err != nil {
    panic(err)
}
var data any
err = p.Parse(jsonStr, &data)

File Writing

SaveToFile

Signature: func (p *Processor) SaveToFile(filePath string, data any, cfg ...Config) error

Saves data as a JSON file. Automatically creates parent directories.

go
err := p.SaveToFile("data.json", map[string]any{"name": "CyberGo"})

// Save with pretty-print configuration
err = p.SaveToFile("data.json", data, json.PrettyConfig())

MarshalToFile

Signature: func (p *Processor) MarshalToFile(path string, data any, cfg ...Config) error

Encodes data to JSON and writes to a file. Automatically creates parent directories.

go
err := p.MarshalToFile("output.json", data)

// Save with formatting
err = p.MarshalToFile("output.json", data, json.PrettyConfig())

UnmarshalFromFile

Signature: func (p *Processor) UnmarshalFromFile(path string, v any, cfg ...Config) error

Reads JSON from a file and decodes into the target variable.

go
var config Config
err := p.UnmarshalFromFile("config.json", &config)
if err != nil {
    panic(err)
}

SaveToWriter

Signature: func (p *Processor) SaveToWriter(writer io.Writer, data any, cfg ...Config) error

Encodes data to JSON and writes to an io.Writer.

go
var buf bytes.Buffer
err := p.SaveToWriter(&buf, data, json.PrettyConfig())

Method Selection

ScenarioRecommended Method
Need raw stringLoadFromFile / LoadFromReader
Need parsed dataLoadFromFile + Parse / LoadFromReader + Parse
Save data to fileSaveToFile / MarshalToFile
Write to WriterSaveToWriter
Read and decode from fileUnmarshalFromFile

See Also