Source File
unmarshal.go
Belonging Package
github.com/aws/aws-sdk-go/private/protocol/json/jsonutil
package jsonutil
import (
)
var millisecondsFloat = new(big.Float).SetInt64(1e3)
func ( interface{}, io.Reader) error {
var interface{}
:= json.NewDecoder()
.UseNumber()
:= .Decode(&)
if == io.EOF {
return nil
} else if != nil {
return
}
return unmarshaler{}.unmarshalAny(reflect.ValueOf(), , "")
}
func ( interface{}, io.Reader) error {
var interface{}
:= json.NewDecoder()
.UseNumber()
:= .Decode(&)
if == io.EOF {
return nil
} else if != nil {
return
}
return unmarshaler{
caseInsensitive: true,
}.unmarshalAny(reflect.ValueOf(), , "")
}
type unmarshaler struct {
caseInsensitive bool
}
func ( unmarshaler) ( reflect.Value, interface{}, reflect.StructTag) error {
:= .Type()
if .Kind() == reflect.Ptr {
= .Elem() // check kind of actual element type
}
:= .Get("type")
if == "" {
switch .Kind() {
if , := .Interface().(aws.JSONValue); ! {
= "map"
}
}
}
switch {
case "structure":
if , := .FieldByName("_"); {
= .Tag
}
return .unmarshalStruct(, , )
case "list":
return .unmarshalList(, , )
case "map":
return .unmarshalMap(, , )
default:
return .unmarshalScalar(, , )
}
}
func ( unmarshaler) ( reflect.Value, interface{}, reflect.StructTag) error {
if == nil {
return nil
}
, := .(map[string]interface{})
if ! {
return fmt.Errorf("JSON value is not a structure (%#v)", )
}
:= .Type()
if .Kind() == reflect.Ptr {
if .IsNil() { // create the structure if it's nil
:= reflect.New(.Type().Elem())
.Set()
=
}
= .Elem()
= .Elem()
}
if := .Get("payload"); != "" {
, := .FieldByName()
return .unmarshalAny(.FieldByName(), , .Tag)
}
for := 0; < .NumField(); ++ {
:= .Field()
if .PkgPath != "" {
continue // ignore unexported fields
}
:= .Name
if := .Tag.Get("locationName"); != "" {
=
}
if .caseInsensitive {
for , := range {
if strings.EqualFold(, ) {
[] =
}
}
}
}
:= .FieldByIndex(.Index)
:= .unmarshalAny(, [], .Tag)
if != nil {
return
}
}
return nil
}
func ( unmarshaler) ( reflect.Value, interface{}, reflect.StructTag) error {
if == nil {
return nil
}
, := .([]interface{})
if ! {
return fmt.Errorf("JSON value is not a list (%#v)", )
}
if .IsNil() {
:= len()
.Set(reflect.MakeSlice(.Type(), , ))
}
for , := range {
:= .unmarshalAny(.Index(), , "")
if != nil {
return
}
}
return nil
}
func ( unmarshaler) ( reflect.Value, interface{}, reflect.StructTag) error {
if == nil {
return nil
}
, := .(map[string]interface{})
if ! {
return fmt.Errorf("JSON value is not a map (%#v)", )
}
if .IsNil() {
.Set(reflect.MakeMap(.Type()))
}
for , := range {
:= reflect.ValueOf()
:= reflect.New(.Type().Elem()).Elem()
.unmarshalAny(, , "")
.SetMapIndex(, )
}
return nil
}
func ( unmarshaler) ( reflect.Value, interface{}, reflect.StructTag) error {
switch d := .(type) {
case nil:
return nil // nothing to do here
case string:
switch .Interface().(type) {
case *string:
.Set(reflect.ValueOf(&))
case []byte:
, := base64.StdEncoding.DecodeString()
if != nil {
return
}
.Set(reflect.ValueOf())
case *time.Time:
:= .Get("timestampFormat")
if len() == 0 {
= protocol.ISO8601TimeFormatName
}
, := protocol.ParseTime(, )
if != nil {
return
}
.Set(reflect.ValueOf(&))
, := .Float64()
if != nil {
return
}
:= int64()
.Set(reflect.ValueOf(&))
case *float64:
, := .Float64()
if != nil {
return
}
.Set(reflect.ValueOf(&))
case *time.Time:
, := new(big.Float).SetString(.String())
if ! {
return fmt.Errorf("unsupported float time representation: %v", .String())
}
= .Mul(, millisecondsFloat)
, := .Int64()
:= time.Unix(0, *1e6).UTC()
.Set(reflect.ValueOf(&))
default:
return fmt.Errorf("unsupported value: %v (%s)", .Interface(), .Type())
}
case bool:
switch .Interface().(type) {
case *bool:
.Set(reflect.ValueOf(&))
default:
return fmt.Errorf("unsupported value: %v (%s)", .Interface(), .Type())
}
default:
return fmt.Errorf("unsupported JSON value (%v)", )
}
return nil
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |