Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Marshal ¶
Marshal returns the PHP serialized bytes of i.
Example ¶
package main import ( "fmt" phpserialize "github.com/kamiaka/go-phpserialize" ) func main() { bs, _ := phpserialize.Marshal([]string{"a", "bbb"}) fmt.Println(string(bs)) }
Output: a:2:{i:0;s:1:"a";i:1;s:3:"bbb";}
func Unmarshal ¶
Unmarshal returns the PHP unserialized Value of data.
Example ¶
package main import ( "fmt" phpserialize "github.com/kamiaka/go-phpserialize" ) func main() { s := `a:2:{i:0;s:1:"a";i:1;s:3:"bbb";}` arr, _ := phpserialize.Unmarshal([]byte(s)) for _, k := range arr.Keys() { fmt.Printf("%#v: %s\n", k.Interface(), arr.Index(k).String()) } }
Output: 0: a 1: bbb
Example (Map) ¶
package main import ( "fmt" phpserialize "github.com/kamiaka/go-phpserialize" ) func main() { s := `a:2:{s:4:"key1";s:1:"a";s:4:"key2";s:3:"bbb";}` arr, _ := phpserialize.Unmarshal([]byte(s)) fmt.Printf("%s\n", arr.IndexByName("key2").String()) fmt.Printf("%s\n", arr.IndexByName("key1").String()) }
Output: bbb a
Example (Panic) ¶
package main import ( "fmt" phpserialize "github.com/kamiaka/go-phpserialize" "github.com/kamiaka/go-phpserialize/php" ) func main() { defer func() { if r := recover(); r != nil { if e, ok := r.(*php.ValueError); ok { fmt.Println(e.Error()) } else { panic(r) } } }() s := `a:4:{s:4:"key1";s:4:"aaaa";s:4:"key2";N;s:4:"key3";i:42;s:4:"key4";N;}` arr, _ := phpserialize.Unmarshal([]byte(s)) fmt.Printf("%v\n", arr.IndexByName("key1").String()) fmt.Printf("%v\n", arr.IndexByName("key2").String()) // no panic fmt.Printf("%v\n", arr.IndexByName("key3").Int()) fmt.Printf("%v\n", arr.IndexByName("key4").Int()) // panic }
Output: aaaa <null value> 42 php: call of php.Value.Int on null Value
Types ¶
type Encoder ¶
type Encoder struct {
// contains filtered or unexported fields
}
An Encoder writes PHP serialize values to an output stream.
type Marshaler ¶
Marshaler is the interface implemented by types that can marshal themselves
into valid PHP serialize.
type UnsupportedMapKeyTypeError ¶
UnsupportedMapKeyTypeError is returned when attempting to encode an unsupported map key.
func (*UnsupportedMapKeyTypeError) Error ¶
func (e *UnsupportedMapKeyTypeError) Error() string
type UnsupportedTypeError ¶
UnsupportedTypeError is returned when attempting to encode an unsupported value.
func (*UnsupportedTypeError) Error ¶
func (e *UnsupportedTypeError) Error() string
Click to show internal directories.
Click to hide internal directories.