-
Notifications
You must be signed in to change notification settings - Fork 0
/
gojson.go
149 lines (136 loc) · 3.36 KB
/
gojson.go
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package gojson
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"gojson/internal/mutex"
"io"
"os"
)
type Json struct {
mu *mutex.RWMutex // 开启安全模式:有指针,关闭时:空指针
JsonContent *interface{} // 使用指针传递,效率更高
}
func New() *Json {
j := &Json{} // 默认为有效对象,后续遇到错误设置为无效对象
return j
}
func (j *Json) LoadContent(data interface{}) *Json {
nilOption := Options{}
return j.LoadContentWithOptions(data, nilOption)
}
// LoadContentWithOptions
// 目的将data转换成map[string]interface{}或,map[string][]interface{}的形式
// 使其能够递归调用json的数据
func (j *Json) LoadContentWithOptions(data interface{}, options Options) *Json {
if data == nil {
fmt.Printf("%v,err: %v\n", createErr, emptyContest)
return nil
}
content, err := j.convertContent(data, options)
if err != nil {
fmt.Printf("%v, err: %v", createErr, err)
return nil
}
j.JsonContent = &content
j.mu = mutex.New(options.Safe) // 创建读写锁
return j
}
func (j *Json) LoadFile(path string) *Json {
nilOption := Options{}
return j.LoadFileWithOptions(path, nilOption)
}
func (j *Json) LoadFileWithOptions(path string, options Options) *Json {
var content []byte
file, err := os.Open(path)
if err != nil {
fmt.Printf("%v, err: %v, %v", createErr, readFileErr, err)
return nil
}
r := bufio.NewReader(file)
for {
lineBytes, err := r.ReadBytes('\n')
if err != nil && err != io.EOF {
fmt.Printf("%v, err: %v, %v", createErr, readFileErr, err)
return nil
}
content = append(content, lineBytes...)
if err == io.EOF {
break
}
}
return j.LoadContentWithOptions(content, options)
}
func (j *Json) Unmarshal(dest interface{}) error {
if j == nil {
return errors.New(invalidJsonObject)
}
j.mu.Lock()
defer j.mu.Unlock()
bytes, err := json.Marshal(*j.JsonContent)
if err != nil {
fmt.Println(err)
return err
}
err = json.Unmarshal(bytes, dest)
if err != nil {
fmt.Println(err)
return err
}
return nil
}
// Get 输出json字符串指定路径的内容
func (j *Json) Get(pattern string) interface{} {
if j == nil {
fmt.Printf("%v, err: %v", getErr, invalidJsonObject)
return ""
}
j.mu.Lock()
defer j.mu.Unlock()
pointer := j.findContentPointer(pattern)
if pointer != nil {
return *pointer
}
fmt.Printf("%v, err: %v", getErr, invalidPattern)
return nil
}
// Set 支持数据替换,插入,删除 data为空为删除
func (j *Json) Set(pattern string, data interface{}) *Json {
nilOptions := Options{}
return j.SetWithOptions(pattern, data, nilOptions)
}
func (j *Json) SetWithOptions(pattern string, data interface{}, options Options) *Json {
if j == nil {
fmt.Printf("%v, err: %v", setErr, invalidJsonObject)
return nil
}
j.mu.Lock()
defer j.mu.Unlock()
err := j.setContentWithOptions(pattern, data, options)
if err != nil {
fmt.Printf("%v, err: %v", setErr, invalidPattern)
return nil
}
return j
}
func (j *Json) Dump() *Json {
nilOptions := DumpOption{}
j.DumpWithOptions(j, nilOptions)
return j
}
func (j *Json) DumpContent() *Json {
nilOptions := DumpOption{}
j.DumpWithOptions(j.JsonContent, nilOptions)
return j
}
func (j *Json) DumpWithOptions(data interface{}, options DumpOption) *Json {
j.mu.Lock()
defer j.mu.Lock()
if j == nil {
fmt.Printf("%v, err: %v", dumpErr, invalidContentType)
return j
}
dumpWithOption(data, options)
return j
}