-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathreader2j.go
executable file
·135 lines (112 loc) · 2.84 KB
/
reader2j.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
// io.Reader --> map[string]interface{} or JSON string
// nothing magic - just implements generic Go case
package x2j
import (
"encoding/json"
"encoding/xml"
"io"
)
// ToTree() - parse a XML io.Reader to a tree of Nodes
func ToTree(rdr io.Reader) (*Node, error) {
// We need to put an *os.File reader in a ByteReader or the xml.NewDecoder
// will wrap it in a bufio.Reader and seek on the file beyond where the
// xml.Decoder parses!
if _, ok := rdr.(io.ByteReader); !ok {
rdr = myByteReader(rdr) // see code at EOF
}
p := xml.NewDecoder(rdr)
p.CharsetReader = X2jCharsetReader
n, perr := xmlToTree("", nil, p)
if perr != nil {
return nil, perr
}
return n, nil
}
// ToMap() - parse a XML io.Reader to a map[string]interface{}
func ToMap(rdr io.Reader, recast ...bool) (map[string]interface{}, error) {
var r bool
if len(recast) == 1 {
r = recast[0]
}
n, err := ToTree(rdr)
if err != nil {
return nil, err
}
m := make(map[string]interface{})
m[n.key] = n.treeToMap(r)
return m, nil
}
// ToJson() - parse a XML io.Reader to a JSON string
func ToJson(rdr io.Reader, recast ...bool) (string, error) {
var r bool
if len(recast) == 1 {
r = recast[0]
}
m, merr := ToMap(rdr, r)
if m == nil || merr != nil {
return "", merr
}
b, berr := json.Marshal(m)
if berr != nil {
return "", berr
}
return string(b), nil
}
// ToJsonIndent - the pretty form of ReaderToJson
func ToJsonIndent(rdr io.Reader, recast ...bool) (string, error) {
var r bool
if len(recast) == 1 {
r = recast[0]
}
m, merr := ToMap(rdr, r)
if m == nil || merr != nil {
return "", merr
}
b, berr := json.MarshalIndent(m, "", " ")
if berr != nil {
return "", berr
}
// NOTE: don't have to worry about safe JSON marshaling with json.Marshal, since '<' and '>" are reservedin XML.
return string(b), nil
}
// ReaderValuesFromTagPath - io.Reader version of ValuesFromTagPath()
func ReaderValuesFromTagPath(rdr io.Reader, path string, getAttrs ...bool) ([]interface{}, error) {
var a bool
if len(getAttrs) == 1 {
a = getAttrs[0]
}
m, err := ToMap(rdr)
if err != nil {
return nil, err
}
return ValuesFromKeyPath(m, path, a), nil
}
// ReaderValuesForTag - io.Reader version of ValuesForTag()
func ReaderValuesForTag(rdr io.Reader, tag string) ([]interface{}, error) {
m, err := ToMap(rdr)
if err != nil {
return nil, err
}
return ValuesForKey(m, tag), nil
}
//============================ from github.com/clbanning/mxj/mxl.go ==========================
type byteReader struct {
r io.Reader
b []byte
}
func myByteReader(r io.Reader) io.Reader {
b := make([]byte, 1)
return &byteReader{r, b}
}
// need for io.Reader - but we don't use it ...
func (b *byteReader) Read(p []byte) (int, error) {
return 0, nil
}
func (b *byteReader) ReadByte() (byte, error) {
_, err := b.r.Read(b.b)
if len(b.b) > 0 {
return b.b[0], nil
}
var c byte
return c, err
}