phpserialize

package module
v0.0.0-...-1873489 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 21, 2019 License: MIT Imports: 8 Imported by: 0

README

go-phpserialize

GoDoc

Go PHP serialize library.

Examples

Serialize Example
func ExampleMarshal() {
  bs, _ := phpserialize.Marshal([]string{"a", "bbb"})
  fmt.Println(string(bs))

  // Output:
  // a:2:{i:0;s:1:"a";i:1;s:3:"bbb";}
}
Unserialize Example
Array
func ExampleUnmarshal() {
  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
}
Assoc
func ExampleUnmarshal_map() {
  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
}
Assoc (panic)
func ExampleUnmarshal_panic() (err error) {
  defer func() {
    if r := recover(); r != nil {
      if e, ok := r.(*php.ValueError); ok {
        fmt.Println(e.Error())
        err = e
      } 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
  return
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Marshal

func Marshal(i interface{}) ([]byte, error)

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

func Unmarshal(data []byte) (*php.Value, error)

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.

func NewEncoder

func NewEncoder(w io.Writer) *Encoder

NewEncoder returns a new encoder.

func (*Encoder) Encode

func (enc *Encoder) Encode(i interface{}) error

Encode writes the PHP serialized value to the stream.

type Marshaler

type Marshaler interface {
	MarshalPHPSerialize() ([]byte, error)
}

Marshaler is the interface implemented by types that can marshal themselves

into valid PHP serialize.

type UnsupportedMapKeyTypeError

type UnsupportedMapKeyTypeError struct {
	Type reflect.Type
}

UnsupportedMapKeyTypeError is returned when attempting to encode an unsupported map key.

func (*UnsupportedMapKeyTypeError) Error

type UnsupportedTypeError

type UnsupportedTypeError struct {
	Type reflect.Type
}

UnsupportedTypeError is returned when attempting to encode an unsupported value.

func (*UnsupportedTypeError) Error

func (e *UnsupportedTypeError) Error() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL