This repository has been archived by the owner on Sep 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathreader_json.go
115 lines (97 loc) · 2.55 KB
/
reader_json.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
/*
Copyright © 2017 Henry Huang <hhh@rutcode.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package config
import (
"bytes"
"github.com/iTrellis/common/json"
)
type defJSONReader struct {
opts ReaderOptions
}
// NewJSONReader return a json reader
func NewJSONReader(opts ...ReaderOptionFunc) Reader {
r := &defJSONReader{}
for _, o := range opts {
o(&r.opts)
}
return r
}
func (p *defJSONReader) Read(model interface{}) error {
return ReadJSONFile(p.opts.filename, model)
}
func (*defJSONReader) Dump(v interface{}) ([]byte, error) {
return json.Marshal(v)
}
func (*defJSONReader) ParseData(data []byte, model interface{}) error {
return ParseJSONConfig(data, model)
}
// ReadJSONFile 读取Json文件数据到Models
func ReadJSONFile(filepath string, model interface{}) error {
data, _, err := filesRepo.Read(filepath)
if err != nil {
return err
}
return ParseJSONConfig(data, model)
}
// ParseJSONConfig 解析Json配置
func ParseJSONConfig(data []byte, model interface{}) error {
var escaped bool // string value flag, " appear times, odd is false, even is true
var comments int // 0 nothing; 1 line; 2 multi line
var result []byte
length := len(data)
for i, w := 0, 0; i < length; i += w {
w = 1
switch comments {
case 1:
if data[i] == '\n' {
comments, escaped = 0, false
}
continue
case 2:
// */
if data[i] == '*' && length != i+1 && data[i+1] == '/' {
w = 2
comments, escaped = 0, false
}
continue
}
switch data[i] {
case '"':
escaped = !escaped
result = append(result, data[i])
case '/':
if escaped || length == i+1 {
result = append(result, data[i])
continue
}
switch data[i+1] {
case '/':
w = 2
comments = 1
case '*':
w = 2
comments = 2
default:
result = append(result, data[i])
}
default:
if escaped || !isWhitespace(data[i]) {
result = append(result, data[i])
}
}
}
decoder := json.NewDecoder(bytes.NewBuffer(result))
decoder.UseNumber()
return decoder.Decode(model)
}