@@ -8,15 +8,13 @@ import (
8
8
"log"
9
9
"os"
10
10
"reflect"
11
- "strings"
12
11
13
12
"github.com/mstgnz/transformer/node"
14
13
"github.com/pkg/errors"
15
14
)
16
15
17
16
var (
18
- Knot * node.Node
19
- Parent * node.Node
17
+ Knot * node.Node
20
18
21
19
key string
22
20
objStart bool
@@ -46,7 +44,7 @@ func ReadJson(filename string) ([]byte, error) {
46
44
// DecodeJson
47
45
// Converts a byte slice to a `node.Node` structure.
48
46
func DecodeJson (data []byte ) (* node.Node , error ) {
49
- dec := json .NewDecoder (strings .NewReader (string ( data ) ))
47
+ dec := json .NewDecoder (bytes .NewReader (data ))
50
48
for {
51
49
token , err := dec .Token ()
52
50
if err == io .EOF || err != nil {
@@ -56,109 +54,94 @@ func DecodeJson(data []byte) (*node.Node, error) {
56
54
value := fmt .Sprintf ("%v" , token )
57
55
// If the type of the object is json.Delim
58
56
if reflect .TypeOf (token ).String () == "json.Delim" {
59
- jsonDelim (token , value )
57
+ jsonDelim (value )
60
58
} else {
61
- nonJsonDelim (token , value )
59
+ nonJsonDelim (value )
62
60
}
63
61
}
64
62
}
65
63
66
64
// jsonDelim
67
- func jsonDelim (token json.Token , value string ) {
68
- // If no Node has been created yet, don't enter here, skip JSON start -> {
65
+ func jsonDelim (value string ) {
69
66
if ! Knot .Exists () {
67
+ objStart = true
68
+ Knot = & node.Node {}
70
69
return
71
70
}
72
- // If the value of object token is a JSON object or array
73
71
switch value {
74
72
case "{" : // set open object - {
75
- // If the key is not empty; this is an object. A new Node will be added next to the existing Node, and the newly added Node will return.
76
- // If the key is empty; There is an array object, and the Node will be created in the array and the newly added Node will be returned.
77
73
if arrStart && len (key ) == 0 {
78
- // An object only starts without a key in an array.
79
- Knot = Knot .AddToValue (Knot , node.Value {Node : & node.Node {Prev : Knot }})
74
+ sub := & node.Node {}
75
+ Knot .Value .Array = append (Knot .Value .Array , & node.Value {Node : sub })
76
+ sub .Parent = Knot
77
+ Knot = sub
80
78
} else {
81
- Knot = Knot .AddToNext (Knot , Parent , key )
79
+ sub := & node.Node {}
80
+ Knot .Next = & node.Node {Key : key , Value : & node.Value {Node : sub }, Prev : Knot , Parent : Knot .Parent }
81
+ sub .Parent = Knot .Next
82
+ Knot = sub
82
83
}
83
- Parent = Knot
84
84
objStart = true
85
85
arrStart = false
86
86
key = ""
87
87
case "[" : // set open array - [
88
- // If the key is not null and objStart is true; The initial value of the Node will be set.
89
- // If the key is not empty and objStart is false; A new Node will be created next to the Node.
90
- // If the key is empty; this is a nested array object. will be added directly to the current Node's array.
91
88
if len (key ) > 0 {
92
- // If objStart is true, then the initial value of the Node is set.
93
89
if objStart {
94
- Knot = Knot .AddToValue (Knot , node.Value {Array : []node.Value {}})
90
+ Knot .Key = key
91
+ Knot .Value = & node.Value {Array : []* node.Value {}}
95
92
} else {
96
- // If objStart is false, a new Node is created next to the current Node.
97
- Knot = Knot .AddToNext ( Knot , Parent , key )
93
+ Knot . Next = & node. Node { Key : key , Value : & node. Value {}, Parent : Knot . Parent , Prev : Knot }
94
+ Knot = Knot .Next
98
95
}
99
- Parent = Knot
100
96
arrStart = true
101
97
objStart = false
102
98
arrCount ++
103
99
key = ""
104
- } else {
105
- // If there is no key, it is a nested array.
106
- _ = append (Knot .Value .Array , node.Value {Worth : value })
107
100
}
108
101
case "]" : // set close array
109
- arrCount --
110
102
arrStart = false
103
+ if arrCount > 0 {
104
+ arrCount --
105
+ }
111
106
case "}" : // set close object and set parent Node
112
- Parent = nil
113
- // if the parent is not empty
107
+ objStart = false
108
+ if arrCount > 0 {
109
+ arrStart = true
110
+ }
114
111
if Knot .Parent != nil {
115
- // if there is an unclosed array and there is no key
116
- if arrCount > 0 && len (Knot .Key ) == 0 {
117
- arrStart = true
118
- }
119
- // use parent as a node
120
112
Knot = Knot .Parent
121
- // assign the parent of the node as parent, it can be nil or node
122
- Parent = Knot .Parent
123
113
}
124
114
default : // shouldn't go here
125
- log .Println ("default not set -> " , token )
115
+ log .Println ("default not set" )
126
116
}
127
117
}
128
118
129
119
// nonJsonDelim
130
- func nonJsonDelim (_ json.Token , value string ) {
131
- // If the loop object is not a json.Delim, the key and value fields will be set.
132
- // Since the JSON object is a key-value pair, first the key will be set and then the value will be set.
120
+ func nonJsonDelim (value string ) {
133
121
if len (key ) == 0 {
134
- // If an array object is open, this key-value is essentially an array object.
135
- // If the array is not empty
136
122
if arrStart {
137
- _ = append (Knot .Value .Array , node.Value {Worth : value })
123
+ Knot . Value . Array = append (Knot .Value .Array , & node.Value {Worth : value })
138
124
} else {
139
125
key = value
140
126
}
141
127
} else {
142
- // If objStart is true, then the initial value of the Node is set.
143
128
if objStart {
144
- Knot = Knot .AddToValue (Knot , node.Value {Worth : value })
129
+ Knot .Key = key
130
+ Knot .Value = & node.Value {Worth : value }
145
131
} else {
146
- Knot = Knot .AddToNext (Knot , Parent , key )
132
+ Knot .Next = & node.Node {Key : key , Parent : Knot .Parent , Prev : Knot }
133
+ Knot .Next .Value = & node.Value {Worth : value }
134
+ Knot = Knot .Next
147
135
}
148
- // Here objStart and key values are reset.
149
136
objStart = false
150
137
key = ""
151
138
}
152
139
}
153
140
154
141
// NodeToJson
155
- // TODO test
156
142
func NodeToJson (node * node.Node ) ([]byte , error ) {
157
- var (
158
- buf bytes.Buffer
159
- enc * json.Encoder
160
- )
161
- enc = json .NewEncoder (& buf )
143
+ var buf bytes.Buffer
144
+ enc := json .NewEncoder (& buf )
162
145
enc .SetIndent (" " , " " )
163
146
err := enc .Encode (node )
164
147
if err != nil {
0 commit comments