forked from digisan/json-tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
break.go
131 lines (124 loc) · 3.21 KB
/
break.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
package jsontool
var (
rName = rxMustCompile(`"[-\w]+":`) // "name":
rsVal = rxMustCompile(`^[-\d\.tfn]`) // non-string, simple value start
)
// BreakMulEleBlk : 'str' is LIKE {"1st-element": {...}, "2nd-element": {...}, "3rd-element": [...]}
// return one 'value' is like '{...}', OR like `[{...},{...},...]`
func BreakMulEleBlk(str string) (names, values []string) {
str = sTrim(str, " \t\n")
failOnErrWhen(str[0] != '{', "%v", fEf("error (format) json"))
failOnErrWhen(str[len(str)-1] != '}', "%v", fEf("error (format) json"))
NEXT:
if loc := rName.FindStringIndex(str); loc != nil { // find attr "name":
s, e := loc[0], loc[1]
root := str[s+1 : e-2]
// fPln(root)
names = append(names, root)
str = sTrimLeft(str[e:], " ") // start @ "{" or "[" or simple...
// Simple Non-String values
if loc := rsVal.FindStringIndex(str); loc != nil {
// fPln("non-string simple ele")
for i := 1; i < len(str); i++ { // skip the 1st char
c := str[i]
if c == ',' || c == '\n' {
values = append(values, str[:i])
str = str[i+1:]
goto NEXT
}
}
}
// Complex, Array or String value
for i, mark := range []string{"{", "[", "\""} {
if sHasPrefix(str, mark) {
var m1, m2 byte
switch i {
case 0:
m1, m2 = '{', '}'
case 1:
m1, m2 = '[', ']'
default:
m1, m2 = '"', '"'
}
L := 0
for i := 0; i < len(str); i++ {
c := str[i]
if m1 != m2 { // Complex, Array
if c == m1 { // { or [
L++
}
if c == m2 { // } or ]
L--
if L == 0 {
values = append(values, str[:i+1])
str = str[i+1:]
goto NEXT
}
}
} else { // String
if c == m1 { // "***"
L++
if L == 2 {
// values = append(values, str[1:i]) // remove '"' at start&end (string & other types mixed)
values = append(values, str[:i+1]) // remove '"' at start&end
str = str[i+1:]
goto NEXT
}
}
}
}
}
}
}
return
}
// BreakArr : 'str' is like [{...},{...}]
// i.e. [{...},{...}] => {...} AND {...}
// NO ele name could get here
func BreakArr(str string) (values []string, ok bool) {
str = sTrim(str, " ")
if str[0] != '[' || str[len(str)-1] != ']' {
return values, false
}
L, S := 0, -1
for i := 0; i < len(str); i++ {
c := str[i]
if c == '{' {
L++
if L == 1 {
S = i
}
}
if c == '}' {
L--
if L == 0 {
values = append(values, str[S:i+1])
}
}
}
return values, true
}
// BreakMulEleBlkV2 : 'str' LIKE { "1st-element": {...}, "2nd-element": {...}, "3rd-element": [...] }
// in return 'values', array types are broken into duplicated names & its single value block
// one 'value' is like '{...}', 'names' may have duplicated names
func BreakMulEleBlkV2(str string) (names, values []string) {
mIndElems := make(map[int][]string)
Names, Values := BreakMulEleBlk(str)
for i, Val := range Values {
if elements, ok := BreakArr(Val); ok {
mIndElems[i] = elements
}
}
for i, Val := range Values {
if elements, ok := mIndElems[i]; ok {
for _, ele := range elements {
names = append(names, Names[i])
values = append(values, ele)
}
} else {
names = append(names, Names[i])
values = append(values, Val)
}
}
return
}