-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjson.go
More file actions
49 lines (44 loc) · 1.06 KB
/
Copy pathjson.go
File metadata and controls
49 lines (44 loc) · 1.06 KB
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
package libjson
import (
"io"
"os"
)
func NewReader(r io.Reader) (JSON, error) {
data, err := io.ReadAll(r)
if err != nil {
return JSON{}, err
}
return newJSON(data, nil)
}
// New consumes data and deserializes it into a JSON object
//
// data is consumed and possibly mutated, DO NOT REUSE
func New(data []byte) (JSON, error) {
return newJSON(data, nil)
}
func newJSON(data []byte, cleanup func() error) (JSON, error) {
p := parser{l: lexer{data: data, len: len(data)}}
obj, err := p.parse(data)
if err != nil {
return JSON{}, err
}
return JSON{obj: obj, arena: p.arena, cleanup: cleanup}, nil
}
// FromFile is the same as New but zero copy via mmap
//
// The returned JSON value owns the memory mapping; call Close once the parsed
// data and any zero-copy strings derived from it are no longer needed.
func FromFile(f *os.File) (JSON, error) {
data, cleanup, err := mmapFile(f)
if err != nil {
return JSON{}, err
}
obj, err := newJSON(data, cleanup)
if err != nil {
if cleanup != nil {
_ = cleanup()
}
return JSON{}, err
}
return obj, nil
}