-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathexample_test.go
More file actions
55 lines (45 loc) · 1.19 KB
/
example_test.go
File metadata and controls
55 lines (45 loc) · 1.19 KB
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
package phpserialize_test
import (
"fmt"
"github.com/trim21/go-phpserialize"
)
func ExampleMarshal() {
type User struct {
ID uint32 `php:"id,string"`
Name string `php:"name"`
}
type Inner struct {
V int `php:"v"`
S string `php:"a long string name replace field name"`
}
type With struct {
Users []User `php:"users,omitempty"`
Obj Inner `php:"obj"`
Ignored bool `php:"-"`
}
var data = With{
Users: []User{
{ID: 1, Name: "sai"},
{ID: 2, Name: "trim21"},
},
Obj: Inner{V: 2, S: "vvv"},
}
var b, err = phpserialize.Marshal(data)
if err != nil {
panic(err)
}
fmt.Println(string(b))
// Output: a:2:{s:5:"users";a:2:{i:0;a:2:{s:2:"id";s:1:"1";s:4:"name";s:3:"sai";}i:1;a:2:{s:2:"id";s:1:"2";s:4:"name";s:6:"trim21";}}s:3:"obj";a:2:{s:1:"v";i:2;s:37:"a long string name replace field name";s:3:"vvv";}}
}
func ExampleUnmarshal() {
var v struct {
Value map[string]string `php:"value" json:"value"`
}
raw := `a:1:{s:5:"value";a:5:{s:3:"one";s:1:"1";s:3:"two";s:1:"2";s:5:"three";s:1:"3";s:4:"four";s:1:"4";s:4:"five";s:1:"5";}}`
err := phpserialize.Unmarshal([]byte(raw), &v)
if err != nil {
panic(err)
}
fmt.Println(v.Value["five"])
// Output: 5
}