forked from spyzhov/ajson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode.go
90 lines (86 loc) · 2 KB
/
encode.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
package ajson
import (
"strconv"
)
// Marshal returns slice of bytes, marshaled from current value
func Marshal(node *Node) (result []byte, err error) {
result = make([]byte, 0)
var (
sValue string
bValue bool
nValue float64
oValue []byte
)
if node == nil {
return nil, errorUnparsed()
} else if node.dirty {
switch node._type {
case Null:
result = append(result, _null...)
case Numeric:
nValue, err = node.GetNumeric()
if err != nil {
return nil, err
}
result = append(result, strconv.FormatFloat(nValue, 'g', -1, 64)...)
case String:
sValue, err = node.GetString()
if err != nil {
return nil, err
}
result = append(result, quotes)
result = append(result, quoteString(sValue, true)...)
result = append(result, quotes)
case Bool:
bValue, err = node.GetBool()
if err != nil {
return nil, err
} else if bValue {
result = append(result, _true...)
} else {
result = append(result, _false...)
}
case Array:
result = append(result, bracketL)
for i := 0; i < len(node.children); i++ {
if i != 0 {
result = append(result, coma)
}
child, ok := node.children[strconv.Itoa(i)]
if !ok {
return nil, errorRequest("wrong length of array")
}
oValue, err = Marshal(child)
if err != nil {
return nil, err
}
result = append(result, oValue...)
}
result = append(result, bracketR)
case Object:
result = append(result, bracesL)
bValue = false
for key, child := range node.children {
if bValue {
result = append(result, coma)
} else {
bValue = true
}
result = append(result, quotes)
result = append(result, quoteString(key, true)...)
result = append(result, quotes, colon)
oValue, err = Marshal(child)
if err != nil {
return nil, err
}
result = append(result, oValue...)
}
result = append(result, bracesR)
}
} else if node.ready() {
result = append(result, node.Source()...)
} else {
return nil, errorUnparsed()
}
return
}