Documentation ¶
Overview ¶
Package phperjson is PHP flavored encoding/json package.
Index ¶
- func Compact(dst *bytes.Buffer, src []byte) error
- func HTMLEscape(dst *bytes.Buffer, src []byte)
- func Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error
- func Marshal(v interface{}) ([]byte, error)
- func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error)
- func Unmarshal(data []byte, v interface{}) error
- func Valid(data []byte) bool
- type Decoder
- type Delim
- type Encoder
- type InvalidUTF8Error
- type InvalidUnmarshalError
- type Marshaler
- type MarshalerError
- type Number
- type RawMessage
- type SyntaxError
- type Token
- type UnmarshalFieldError
- type UnmarshalTypeError
- type Unmarshaler
- type UnsupportedTypeError
- type UnsupportedValueError
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func HTMLEscape ¶
HTMLEscape is an alias for json.HTMLEscape.
func Marshal ¶
Marshal is an alias for json.Marshal.
Example ¶
package main import ( "fmt" "os" phperjson "github.com/shogo82148/go-phper-json" ) func main() { type ColorGroup struct { ID int Name string Colors []string } group := ColorGroup{ ID: 1, Name: "Reds", Colors: []string{"Crimson", "Red", "Ruby", "Maroon"}, } // phperjson.Marshal is compatible with json.Marshal. b, err := phperjson.Marshal(group) if err != nil { fmt.Println("error:", err) } os.Stdout.Write(b) }
Output: {"ID":1,"Name":"Reds","Colors":["Crimson","Red","Ruby","Maroon"]}
func MarshalIndent ¶
MarshalIndent is an alias for json.MarshalIndent.
func Unmarshal ¶
Unmarshal parses the JSON-encoded data and stores the result in the value pointed to by v. If v is nil or not a pointer, Unmarshal returns an InvalidUnmarshalError.
phperjson.Unmashal works in the same way as json.Unmashal, but it is useful for dealing with PHP-encoded JSON. http://php.net/manual/en/function.json-encode.php
Unlike json.Unmarshal, phperjson.Unmarshal can unmashal a JSON object into a slice. The key of the object is interpreted as an index of the slice. It is use for decoding PHP-encoded JSON with JSON_FORCE_OBJECT option.
And more, you can use “Type Juggling” of PHP. For example, phperjson.Unmarshal can unmashal a JSON string into int, if the string can be parsed as number. See http://php.net/manual/en/language.types.type-juggling.php for more detail.
Example ¶
package main import ( "fmt" "reflect" phperjson "github.com/shogo82148/go-phper-json" ) func main() { var jsonBlob = []byte(`[ {"Name": "Platypus", "Order": "Monotremata"}, {"Name": "Quoll", "Order": "Dasyuromorphia"} ]`) type Animal struct { Name string Order string } // phperjson.Unmarshal is compatible with json.Unmarshal. var animals1 []Animal if err := phperjson.Unmarshal(jsonBlob, &animals1); err != nil { fmt.Println("error:", err) } fmt.Printf("%+v\n", animals1) // JSON encoded by PHP with JSON_FORCE_OBJECT option. var phpJSONBlob = []byte(`{ "0": {"Name": "Platypus", "Order": "Monotremata"}, "1": {"Name": "Quoll", "Order": "Dasyuromorphia"} }`) var animals2 []Animal if err := phperjson.Unmarshal(phpJSONBlob, &animals2); err != nil { fmt.Println("error:", err) } fmt.Printf("%+v\n", animals2) // jsonBlob and phperJSONBlob are equal for PHP fmt.Println(reflect.DeepEqual(animals1, animals2)) }
Output: [{Name:Platypus Order:Monotremata} {Name:Quoll Order:Dasyuromorphia}] [{Name:Platypus Order:Monotremata} {Name:Quoll Order:Dasyuromorphia}] true
Example (TypeJaggling) ¶
package main import ( "fmt" phperjson "github.com/shogo82148/go-phper-json" ) func main() { var jsonBlob = []byte(`{ "R": 98, "G": "218", "B": 255.0 }`) type RGB struct { R uint8 G uint8 B uint8 } // phperjson.Unmarshal is compatible with json.Unmarshal. var color RGB if err := phperjson.Unmarshal(jsonBlob, &color); err != nil { fmt.Println("error:", err) } fmt.Printf("%+v\n", color) }
Output: {R:98 G:218 B:255}
Types ¶
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
A Decoder reads and decodes JSON values from an input stream.
func NewDecoder ¶
NewDecoder returns a new decoder that reads from r.
func (*Decoder) Buffered ¶
Buffered returns a reader of the data remaining in the Decoder's buffer. The reader is valid until the next call to Decode.
func (*Decoder) Decode ¶
Decode reads the next JSON-encoded value from its input and stores it in the value pointed to by v.
func (*Decoder) DisallowUnknownFields ¶
func (dec *Decoder) DisallowUnknownFields()
DisallowUnknownFields causes the Decoder to return an error when the destination is a struct and the input contains object keys which do not match any non-ignored, exported fields in the destination.
func (*Decoder) More ¶
More reports whether there is another element in the current array or object being parsed.
type InvalidUTF8Error ¶
type InvalidUTF8Error = json.InvalidUTF8Error
InvalidUTF8Error is an alias for json.InvalidUTF8Error.
type InvalidUnmarshalError ¶
type InvalidUnmarshalError = json.InvalidUnmarshalError
InvalidUnmarshalError is an alias for json.InvalidUnmarshalError.
type MarshalerError ¶
type MarshalerError = json.MarshalerError
MarshalerError is an alias for json.MarshalerError.
type SyntaxError ¶
type SyntaxError = json.SyntaxError
SyntaxError is an alias for json.SyntaxError.
type UnmarshalFieldError ¶
type UnmarshalFieldError = json.UnmarshalFieldError
UnmarshalFieldError is an alias for json.UnmarshalFieldError.
type UnmarshalTypeError ¶
type UnmarshalTypeError = json.UnmarshalTypeError
UnmarshalTypeError is an alias for json.UnmarshalTypeError.
type Unmarshaler ¶
type Unmarshaler = json.Unmarshaler
Unmarshaler is an alias for json.Unmarshaler.
type UnsupportedTypeError ¶
type UnsupportedTypeError = json.UnsupportedTypeError
UnsupportedTypeError is an alias for json.UnsupportedTypeError.
type UnsupportedValueError ¶
type UnsupportedValueError = json.UnsupportedValueError
UnsupportedValueError is an alias for json.UnsupportedValueError.