-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsman.go
42 lines (33 loc) · 806 Bytes
/
jsman.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
package jsman
// This class can pack and unpack JSON, but jsonstr and struct of JSON must be provided
// in advance
import (
"encoding/json"
)
type JsonParser struct {
JsonStr string
packedData []byte
useDefaultData bool
}
func (t *JsonParser) Pack(i interface{}) (string, error) {
data, err := json.Marshal(i)
if err != nil {
return "", err
}
return string(data[:]), nil
}
// Unpack the json string into "Any" interface
func (t *JsonParser) Unpack(i interface{}) error {
// Go's marshal library requires jsonstr as byte slice
data := []byte(t.JsonStr)
err := json.Unmarshal(data, i)
if err != nil {
return err
}
return nil
}
// If you use type of pointer like below
func (t *JsonParser) UpdateJSONstr(jsonStr string) {
t.JsonStr = jsonStr
}
// *** Usage sample ****