-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring.go
61 lines (43 loc) · 915 Bytes
/
string.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
package unmarshal
import (
"fmt"
"strings"
"github.com/buger/jsonparser"
)
type String string
func (i *String) UnmarshalJSON(b []byte) error {
var data, dataType, _, err = jsonparser.Get(b)
if err != nil {
return err
}
if len(data) == 0 {
*i = ""
return nil
}
str := string(data)
switch dataType {
case jsonparser.Object:
if str == "{}" {
str = ""
}
*i = String(fmt.Sprint(str))
return nil
case jsonparser.String, jsonparser.Number, jsonparser.Boolean:
*i = String(str)
return nil
case jsonparser.Null:
*i = ""
return nil
case jsonparser.Array:
var slice []string
_, err = jsonparser.ArrayEach(data, func(value2 []byte, dataType2 jsonparser.ValueType, offset int, err error) {
slice = append(slice, string(value2))
})
if err != nil {
return err
}
*i = String(strings.Join(slice, ","))
return nil
}
return newError(dataType, "string")
}