// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Represents JSON data structure using native Go types: booleans, floats,
// strings, arrays, and maps.

package json

import (
	
	
	
	
	
	
	
	
	
)

// 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.
//
// Unmarshal uses the inverse of the encodings that
// Marshal uses, allocating maps, slices, and pointers as necessary,
// with the following additional rules:
//
// To unmarshal JSON into a pointer, Unmarshal first handles the case of
// the JSON being the JSON literal null. In that case, Unmarshal sets
// the pointer to nil. Otherwise, Unmarshal unmarshals the JSON into
// the value pointed at by the pointer. If the pointer is nil, Unmarshal
// allocates a new value for it to point to.
//
// To unmarshal JSON into a value implementing the Unmarshaler interface,
// Unmarshal calls that value's UnmarshalJSON method, including
// when the input is a JSON null.
// Otherwise, if the value implements encoding.TextUnmarshaler
// and the input is a JSON quoted string, Unmarshal calls that value's
// UnmarshalText method with the unquoted form of the string.
//
// To unmarshal JSON into a struct, Unmarshal matches incoming object
// keys to the keys used by Marshal (either the struct field name or its tag),
// preferring an exact match but also accepting a case-insensitive match. By
// default, object keys which don't have a corresponding struct field are
// ignored (see Decoder.DisallowUnknownFields for an alternative).
//
// To unmarshal JSON into an interface value,
// Unmarshal stores one of these in the interface value:
//
//	bool, for JSON booleans
//	float64, for JSON numbers
//	string, for JSON strings
//	[]interface{}, for JSON arrays
//	map[string]interface{}, for JSON objects
//	nil for JSON null
//
// To unmarshal a JSON array into a slice, Unmarshal resets the slice length
// to zero and then appends each element to the slice.
// As a special case, to unmarshal an empty JSON array into a slice,
// Unmarshal replaces the slice with a new empty slice.
//
// To unmarshal a JSON array into a Go array, Unmarshal decodes
// JSON array elements into corresponding Go array elements.
// If the Go array is smaller than the JSON array,
// the additional JSON array elements are discarded.
// If the JSON array is smaller than the Go array,
// the additional Go array elements are set to zero values.
//
// To unmarshal a JSON object into a map, Unmarshal first establishes a map to
// use. If the map is nil, Unmarshal allocates a new map. Otherwise Unmarshal
// reuses the existing map, keeping existing entries. Unmarshal then stores
// key-value pairs from the JSON object into the map. The map's key type must
// either be any string type, an integer, implement json.Unmarshaler, or
// implement encoding.TextUnmarshaler.
//
// If a JSON value is not appropriate for a given target type,
// or if a JSON number overflows the target type, Unmarshal
// skips that field and completes the unmarshaling as best it can.
// If no more serious errors are encountered, Unmarshal returns
// an UnmarshalTypeError describing the earliest such error. In any
// case, it's not guaranteed that all the remaining fields following
// the problematic one will be unmarshaled into the target object.
//
// The JSON null value unmarshals into an interface, map, pointer, or slice
// by setting that Go value to nil. Because null is often used in JSON to mean
// ``not present,'' unmarshaling a JSON null into any other Go type has no effect
// on the value and produces no error.
//
// When unmarshaling quoted strings, invalid UTF-8 or
// invalid UTF-16 surrogate pairs are not treated as an error.
// Instead, they are replaced by the Unicode replacement
// character U+FFFD.
//
func ( []byte,  interface{}) error {
	// Check for well-formedness.
	// Avoids filling out half a data structure
	// before discovering a JSON syntax error.
	var  decodeState
	 := checkValid(, &.scan)
	if  != nil {
		return 
	}

	.init()
	return .unmarshal()
}

// Unmarshaler is the interface implemented by types
// that can unmarshal a JSON description of themselves.
// The input can be assumed to be a valid encoding of
// a JSON value. UnmarshalJSON must copy the JSON data
// if it wishes to retain the data after returning.
//
// By convention, to approximate the behavior of Unmarshal itself,
// Unmarshalers implement UnmarshalJSON([]byte("null")) as a no-op.
type Unmarshaler interface {
	UnmarshalJSON([]byte) error
}

// An UnmarshalTypeError describes a JSON value that was
// not appropriate for a value of a specific Go type.
type UnmarshalTypeError struct {
	Value  string       // description of JSON value - "bool", "array", "number -5"
	Type   reflect.Type // type of Go value it could not be assigned to
	Offset int64        // error occurred after reading Offset bytes
	Struct string       // name of the struct type containing the field
	Field  string       // the full path from root node to the field
}

func ( *UnmarshalTypeError) () string {
	if .Struct != "" || .Field != "" {
		return "json: cannot unmarshal " + .Value + " into Go struct field " + .Struct + "." + .Field + " of type " + .Type.String()
	}
	return "json: cannot unmarshal " + .Value + " into Go value of type " + .Type.String()
}

// An UnmarshalFieldError describes a JSON object key that
// led to an unexported (and therefore unwritable) struct field.
//
// Deprecated: No longer used; kept for compatibility.
type UnmarshalFieldError struct {
	Key   string
	Type  reflect.Type
	Field reflect.StructField
}

func ( *UnmarshalFieldError) () string {
	return "json: cannot unmarshal object key " + strconv.Quote(.Key) + " into unexported field " + .Field.Name + " of type " + .Type.String()
}

// An InvalidUnmarshalError describes an invalid argument passed to Unmarshal.
// (The argument to Unmarshal must be a non-nil pointer.)
type InvalidUnmarshalError struct {
	Type reflect.Type
}

func ( *InvalidUnmarshalError) () string {
	if .Type == nil {
		return "json: Unmarshal(nil)"
	}

	if .Type.Kind() != reflect.Ptr {
		return "json: Unmarshal(non-pointer " + .Type.String() + ")"
	}
	return "json: Unmarshal(nil " + .Type.String() + ")"
}

func ( *decodeState) ( interface{}) error {
	 := reflect.ValueOf()
	if .Kind() != reflect.Ptr || .IsNil() {
		return &InvalidUnmarshalError{reflect.TypeOf()}
	}

	.scan.reset()
	.scanWhile(scanSkipSpace)
	// We decode rv not rv.Elem because the Unmarshaler interface
	// test must be applied at the top level of the value.
	 := .value()
	if  != nil {
		return .addErrorContext()
	}
	return .savedError
}

// A Number represents a JSON number literal.
type Number string

// String returns the literal text of the number.
func ( Number) () string { return string() }

// Float64 returns the number as a float64.
func ( Number) () (float64, error) {
	return strconv.ParseFloat(string(), 64)
}

// Int64 returns the number as an int64.
func ( Number) () (int64, error) {
	return strconv.ParseInt(string(), 10, 64)
}

// decodeState represents the state while decoding a JSON value.
type decodeState struct {
	data         []byte
	off          int // next read offset in data
	opcode       int // last read result
	scan         scanner
	errorContext struct { // provides context for type errors
		Struct     reflect.Type
		FieldStack []string
	}
	savedError            error
	useNumber             bool
	disallowUnknownFields bool
}

// readIndex returns the position of the last byte read.
func ( *decodeState) () int {
	return .off - 1
}

// phasePanicMsg is used as a panic message when we end up with something that
// shouldn't happen. It can indicate a bug in the JSON decoder, or that
// something is editing the data slice while the decoder executes.
const phasePanicMsg = "JSON decoder out of sync - data changing underfoot?"

func ( *decodeState) ( []byte) *decodeState {
	.data = 
	.off = 0
	.savedError = nil
	.errorContext.Struct = nil

	// Reuse the allocated space for the FieldStack slice.
	.errorContext.FieldStack = .errorContext.FieldStack[:0]
	return 
}

// saveError saves the first err it is called with,
// for reporting at the end of the unmarshal.
func ( *decodeState) ( error) {
	if .savedError == nil {
		.savedError = .addErrorContext()
	}
}

// addErrorContext returns a new error enhanced with information from d.errorContext
func ( *decodeState) ( error) error {
	if .errorContext.Struct != nil || len(.errorContext.FieldStack) > 0 {
		switch err := .(type) {
		case *UnmarshalTypeError:
			.Struct = .errorContext.Struct.Name()
			.Field = strings.Join(.errorContext.FieldStack, ".")
			return 
		}
	}
	return 
}

// skip scans to the end of what was started.
func ( *decodeState) () {
	, ,  := &.scan, .data, .off
	 := len(.parseState)
	for {
		 := .step(, [])
		++
		if len(.parseState) <  {
			.off = 
			.opcode = 
			return
		}
	}
}

// scanNext processes the byte at d.data[d.off].
func ( *decodeState) () {
	if .off < len(.data) {
		.opcode = .scan.step(&.scan, .data[.off])
		.off++
	} else {
		.opcode = .scan.eof()
		.off = len(.data) + 1 // mark processed EOF with len+1
	}
}

// scanWhile processes bytes in d.data[d.off:] until it
// receives a scan code not equal to op.
func ( *decodeState) ( int) {
	, ,  := &.scan, .data, .off
	for  < len() {
		 := .step(, [])
		++
		if  !=  {
			.opcode = 
			.off = 
			return
		}
	}

	.off = len() + 1 // mark processed EOF with len+1
	.opcode = .scan.eof()
}

// rescanLiteral is similar to scanWhile(scanContinue), but it specialises the
// common case where we're decoding a literal. The decoder scans the input
// twice, once for syntax errors and to check the length of the value, and the
// second to perform the decoding.
//
// Only in the second step do we use decodeState to tokenize literals, so we
// know there aren't any syntax errors. We can take advantage of that knowledge,
// and scan a literal's bytes much more quickly.
func ( *decodeState) () {
	,  := .data, .off
:
	switch [-1] {
	case '"': // string
		for ;  < len(); ++ {
			switch [] {
			case '\\':
				++ // escaped char
			case '"':
				++ // tokenize the closing quote too
				break 
			}
		}
	case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-': // number
		for ;  < len(); ++ {
			switch [] {
			case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
				'.', 'e', 'E', '+', '-':
			default:
				break 
			}
		}
	case 't': // true
		 += len("rue")
	case 'f': // false
		 += len("alse")
	case 'n': // null
		 += len("ull")
	}
	if  < len() {
		.opcode = stateEndValue(&.scan, [])
	} else {
		.opcode = scanEnd
	}
	.off =  + 1
}

// value consumes a JSON value from d.data[d.off-1:], decoding into v, and
// reads the following byte ahead. If v is invalid, the value is discarded.
// The first byte of the value has been read already.
func ( *decodeState) ( reflect.Value) error {
	switch .opcode {
	default:
		panic(phasePanicMsg)

	case scanBeginArray:
		if .IsValid() {
			if  := .array();  != nil {
				return 
			}
		} else {
			.skip()
		}
		.scanNext()

	case scanBeginObject:
		if .IsValid() {
			if  := .object();  != nil {
				return 
			}
		} else {
			.skip()
		}
		.scanNext()

	case scanBeginLiteral:
		// All bytes inside literal return scanContinue op code.
		 := .readIndex()
		.rescanLiteral()

		if .IsValid() {
			if  := .literalStore(.data[:.readIndex()], , false);  != nil {
				return 
			}
		}
	}
	return nil
}

type unquotedValue struct{}

// valueQuoted is like value but decodes a
// quoted string literal or literal null into an interface value.
// If it finds anything other than a quoted string literal or null,
// valueQuoted returns unquotedValue{}.
func ( *decodeState) () interface{} {
	switch .opcode {
	default:
		panic(phasePanicMsg)

	case scanBeginArray, scanBeginObject:
		.skip()
		.scanNext()

	case scanBeginLiteral:
		 := .literalInterface()
		switch .(type) {
		case nil, string:
			return 
		}
	}
	return unquotedValue{}
}

// indirect walks down v allocating pointers as needed,
// until it gets to a non-pointer.
// If it encounters an Unmarshaler, indirect stops and returns that.
// If decodingNull is true, indirect stops at the first settable pointer so it
// can be set to nil.
func indirect( reflect.Value,  bool) (Unmarshaler, encoding.TextUnmarshaler, reflect.Value) {
	// Issue #24153 indicates that it is generally not a guaranteed property
	// that you may round-trip a reflect.Value by calling Value.Addr().Elem()
	// and expect the value to still be settable for values derived from
	// unexported embedded struct fields.
	//
	// The logic below effectively does this when it first addresses the value
	// (to satisfy possible pointer methods) and continues to dereference
	// subsequent pointers as necessary.
	//
	// After the first round-trip, we set v back to the original value to
	// preserve the original RW flags contained in reflect.Value.
	 := 
	 := false

	// If v is a named type and is addressable,
	// start with its address, so that if the type has pointer methods,
	// we find them.
	if .Kind() != reflect.Ptr && .Type().Name() != "" && .CanAddr() {
		 = true
		 = .Addr()
	}
	for {
		// Load value from interface, but only if the result will be
		// usefully addressable.
		if .Kind() == reflect.Interface && !.IsNil() {
			 := .Elem()
			if .Kind() == reflect.Ptr && !.IsNil() && (! || .Elem().Kind() == reflect.Ptr) {
				 = false
				 = 
				continue
			}
		}

		if .Kind() != reflect.Ptr {
			break
		}

		if  && .CanSet() {
			break
		}

		// Prevent infinite loop if v is an interface pointing to its own address:
		//     var v interface{}
		//     v = &v
		if .Elem().Kind() == reflect.Interface && .Elem().Elem() ==  {
			 = .Elem()
			break
		}
		if .IsNil() {
			.Set(reflect.New(.Type().Elem()))
		}
		if .Type().NumMethod() > 0 && .CanInterface() {
			if ,  := .Interface().(Unmarshaler);  {
				return , nil, reflect.Value{}
			}
			if ! {
				if ,  := .Interface().(encoding.TextUnmarshaler);  {
					return nil, , reflect.Value{}
				}
			}
		}

		if  {
			 =  // restore original value after round-trip Value.Addr().Elem()
			 = false
		} else {
			 = .Elem()
		}
	}
	return nil, nil, 
}

// array consumes an array from d.data[d.off-1:], decoding into v.
// The first byte of the array ('[') has been read already.
func ( *decodeState) ( reflect.Value) error {
	// Check for unmarshaler.
	, ,  := indirect(, false)
	if  != nil {
		 := .readIndex()
		.skip()
		return .UnmarshalJSON(.data[:.off])
	}
	if  != nil {
		.saveError(&UnmarshalTypeError{Value: "array", Type: .Type(), Offset: int64(.off)})
		.skip()
		return nil
	}
	 = 

	// Check type of target.
	switch .Kind() {
	case reflect.Interface:
		if .NumMethod() == 0 {
			// Decoding into nil interface? Switch to non-reflect code.
			 := .arrayInterface()
			.Set(reflect.ValueOf())
			return nil
		}
		// Otherwise it's invalid.
		fallthrough
	default:
		.saveError(&UnmarshalTypeError{Value: "array", Type: .Type(), Offset: int64(.off)})
		.skip()
		return nil
	case reflect.Array, reflect.Slice:
		break
	}

	 := 0
	for {
		// Look ahead for ] - can only happen on first iteration.
		.scanWhile(scanSkipSpace)
		if .opcode == scanEndArray {
			break
		}

		// Get element of array, growing if necessary.
		if .Kind() == reflect.Slice {
			// Grow slice if necessary
			if  >= .Cap() {
				 := .Cap() + .Cap()/2
				if  < 4 {
					 = 4
				}
				 := reflect.MakeSlice(.Type(), .Len(), )
				reflect.Copy(, )
				.Set()
			}
			if  >= .Len() {
				.SetLen( + 1)
			}
		}

		if  < .Len() {
			// Decode into element.
			if  := .value(.Index());  != nil {
				return 
			}
		} else {
			// Ran out of fixed array: skip.
			if  := .value(reflect.Value{});  != nil {
				return 
			}
		}
		++

		// Next token must be , or ].
		if .opcode == scanSkipSpace {
			.scanWhile(scanSkipSpace)
		}
		if .opcode == scanEndArray {
			break
		}
		if .opcode != scanArrayValue {
			panic(phasePanicMsg)
		}
	}

	if  < .Len() {
		if .Kind() == reflect.Array {
			// Array. Zero the rest.
			 := reflect.Zero(.Type().Elem())
			for ;  < .Len(); ++ {
				.Index().Set()
			}
		} else {
			.SetLen()
		}
	}
	if  == 0 && .Kind() == reflect.Slice {
		.Set(reflect.MakeSlice(.Type(), 0, 0))
	}
	return nil
}

var nullLiteral = []byte("null")
var textUnmarshalerType = reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem()

// object consumes an object from d.data[d.off-1:], decoding into v.
// The first byte ('{') of the object has been read already.
func ( *decodeState) ( reflect.Value) error {
	// Check for unmarshaler.
	, ,  := indirect(, false)
	if  != nil {
		 := .readIndex()
		.skip()
		return .UnmarshalJSON(.data[:.off])
	}
	if  != nil {
		.saveError(&UnmarshalTypeError{Value: "object", Type: .Type(), Offset: int64(.off)})
		.skip()
		return nil
	}
	 = 
	 := .Type()

	// Decoding into nil interface? Switch to non-reflect code.
	if .Kind() == reflect.Interface && .NumMethod() == 0 {
		 := .objectInterface()
		.Set(reflect.ValueOf())
		return nil
	}

	var  structFields

	// Check type of target:
	//   struct or
	//   map[T1]T2 where T1 is string, an integer type,
	//             or an encoding.TextUnmarshaler
	switch .Kind() {
	case reflect.Map:
		// Map key must either have string kind, have an integer kind,
		// or be an encoding.TextUnmarshaler.
		switch .Key().Kind() {
		case reflect.String,
			reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
			reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
		default:
			if !reflect.PtrTo(.Key()).Implements(textUnmarshalerType) {
				.saveError(&UnmarshalTypeError{Value: "object", Type: , Offset: int64(.off)})
				.skip()
				return nil
			}
		}
		if .IsNil() {
			.Set(reflect.MakeMap())
		}
	case reflect.Struct:
		 = cachedTypeFields()
		// ok
	default:
		.saveError(&UnmarshalTypeError{Value: "object", Type: , Offset: int64(.off)})
		.skip()
		return nil
	}

	var  reflect.Value
	 := .errorContext

	for {
		// Read opening " of string key or closing }.
		.scanWhile(scanSkipSpace)
		if .opcode == scanEndObject {
			// closing } - can only happen on first iteration.
			break
		}
		if .opcode != scanBeginLiteral {
			panic(phasePanicMsg)
		}

		// Read key.
		 := .readIndex()
		.rescanLiteral()
		 := .data[:.readIndex()]
		,  := unquoteBytes()
		if ! {
			panic(phasePanicMsg)
		}

		// Figure out field corresponding to key.
		var  reflect.Value
		 := false // whether the value is wrapped in a string to be decoded first

		if .Kind() == reflect.Map {
			 := .Elem()
			if !.IsValid() {
				 = reflect.New().Elem()
			} else {
				.Set(reflect.Zero())
			}
			 = 
		} else {
			var  *field
			if ,  := .nameIndex[string()];  {
				// Found an exact name match.
				 = &.list[]
			} else {
				// Fall back to the expensive case-insensitive
				// linear search.
				for  := range .list {
					 := &.list[]
					if .equalFold(.nameBytes, ) {
						 = 
						break
					}
				}
			}
			if  != nil {
				 = 
				 = .quoted
				for ,  := range .index {
					if .Kind() == reflect.Ptr {
						if .IsNil() {
							// If a struct embeds a pointer to an unexported type,
							// it is not possible to set a newly allocated value
							// since the field is unexported.
							//
							// See https://golang.org/issue/21357
							if !.CanSet() {
								.saveError(fmt.Errorf("json: cannot set embedded pointer to unexported struct: %v", .Type().Elem()))
								// Invalidate subv to ensure d.value(subv) skips over
								// the JSON value without assigning it to subv.
								 = reflect.Value{}
								 = false
								break
							}
							.Set(reflect.New(.Type().Elem()))
						}
						 = .Elem()
					}
					 = .Field()
				}
				.errorContext.FieldStack = append(.errorContext.FieldStack, .name)
				.errorContext.Struct = 
			} else if .disallowUnknownFields {
				.saveError(fmt.Errorf("json: unknown field %q", ))
			}
		}

		// Read : before value.
		if .opcode == scanSkipSpace {
			.scanWhile(scanSkipSpace)
		}
		if .opcode != scanObjectKey {
			panic(phasePanicMsg)
		}
		.scanWhile(scanSkipSpace)

		if  {
			switch qv := .valueQuoted().(type) {
			case nil:
				if  := .literalStore(nullLiteral, , false);  != nil {
					return 
				}
			case string:
				if  := .literalStore([]byte(), , true);  != nil {
					return 
				}
			default:
				.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal unquoted value into %v", .Type()))
			}
		} else {
			if  := .value();  != nil {
				return 
			}
		}

		// Write value back to map;
		// if using struct, subv points into struct already.
		if .Kind() == reflect.Map {
			 := .Key()
			var  reflect.Value
			switch {
			case reflect.PtrTo().Implements(textUnmarshalerType):
				 = reflect.New()
				if  := .literalStore(, , true);  != nil {
					return 
				}
				 = .Elem()
			case .Kind() == reflect.String:
				 = reflect.ValueOf().Convert()
			default:
				switch .Kind() {
				case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
					 := string()
					,  := strconv.ParseInt(, 10, 64)
					if  != nil || reflect.Zero().OverflowInt() {
						.saveError(&UnmarshalTypeError{Value: "number " + , Type: , Offset: int64( + 1)})
						break
					}
					 = reflect.ValueOf().Convert()
				case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
					 := string()
					,  := strconv.ParseUint(, 10, 64)
					if  != nil || reflect.Zero().OverflowUint() {
						.saveError(&UnmarshalTypeError{Value: "number " + , Type: , Offset: int64( + 1)})
						break
					}
					 = reflect.ValueOf().Convert()
				default:
					panic("json: Unexpected key type") // should never occur
				}
			}
			if .IsValid() {
				.SetMapIndex(, )
			}
		}

		// Next token must be , or }.
		if .opcode == scanSkipSpace {
			.scanWhile(scanSkipSpace)
		}
		// Reset errorContext to its original state.
		// Keep the same underlying array for FieldStack, to reuse the
		// space and avoid unnecessary allocs.
		.errorContext.FieldStack = .errorContext.FieldStack[:len(.FieldStack)]
		.errorContext.Struct = .Struct
		if .opcode == scanEndObject {
			break
		}
		if .opcode != scanObjectValue {
			panic(phasePanicMsg)
		}
	}
	return nil
}

// convertNumber converts the number literal s to a float64 or a Number
// depending on the setting of d.useNumber.
func ( *decodeState) ( string) (interface{}, error) {
	if .useNumber {
		return Number(), nil
	}
	,  := strconv.ParseFloat(, 64)
	if  != nil {
		return nil, &UnmarshalTypeError{Value: "number " + , Type: reflect.TypeOf(0.0), Offset: int64(.off)}
	}
	return , nil
}

var numberType = reflect.TypeOf(Number(""))

// literalStore decodes a literal stored in item into v.
//
// fromQuoted indicates whether this literal came from unwrapping a
// string from the ",string" struct tag option. this is used only to
// produce more helpful error messages.
func ( *decodeState) ( []byte,  reflect.Value,  bool) error {
	// Check for unmarshaler.
	if len() == 0 {
		//Empty string given
		.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", , .Type()))
		return nil
	}
	 := [0] == 'n' // null
	, ,  := indirect(, )
	if  != nil {
		return .UnmarshalJSON()
	}
	if  != nil {
		if [0] != '"' {
			if  {
				.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", , .Type()))
				return nil
			}
			 := "number"
			switch [0] {
			case 'n':
				 = "null"
			case 't', 'f':
				 = "bool"
			}
			.saveError(&UnmarshalTypeError{Value: , Type: .Type(), Offset: int64(.readIndex())})
			return nil
		}
		,  := unquoteBytes()
		if ! {
			if  {
				return fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", , .Type())
			}
			panic(phasePanicMsg)
		}
		return .UnmarshalText()
	}

	 = 

	switch  := [0];  {
	case 'n': // null
		// The main parser checks that only true and false can reach here,
		// but if this was a quoted string input, it could be anything.
		if  && string() != "null" {
			.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", , .Type()))
			break
		}
		switch .Kind() {
		case reflect.Interface, reflect.Ptr, reflect.Map, reflect.Slice:
			.Set(reflect.Zero(.Type()))
			// otherwise, ignore null for primitives/string
		}
	case 't', 'f': // true, false
		 := [0] == 't'
		// The main parser checks that only true and false can reach here,
		// but if this was a quoted string input, it could be anything.
		if  && string() != "true" && string() != "false" {
			.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", , .Type()))
			break
		}
		switch .Kind() {
		default:
			if  {
				.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", , .Type()))
			} else {
				.saveError(&UnmarshalTypeError{Value: "bool", Type: .Type(), Offset: int64(.readIndex())})
			}
		case reflect.Bool:
			.SetBool()
		case reflect.Interface:
			if .NumMethod() == 0 {
				.Set(reflect.ValueOf())
			} else {
				.saveError(&UnmarshalTypeError{Value: "bool", Type: .Type(), Offset: int64(.readIndex())})
			}
		}

	case '"': // string
		,  := unquoteBytes()
		if ! {
			if  {
				return fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", , .Type())
			}
			panic(phasePanicMsg)
		}
		switch .Kind() {
		default:
			.saveError(&UnmarshalTypeError{Value: "string", Type: .Type(), Offset: int64(.readIndex())})
		case reflect.Slice:
			if .Type().Elem().Kind() != reflect.Uint8 {
				.saveError(&UnmarshalTypeError{Value: "string", Type: .Type(), Offset: int64(.readIndex())})
				break
			}
			 := make([]byte, base64.StdEncoding.DecodedLen(len()))
			,  := base64.StdEncoding.Decode(, )
			if  != nil {
				.saveError()
				break
			}
			.SetBytes([:])
		case reflect.String:
			if .Type() == numberType && !isValidNumber(string()) {
				return fmt.Errorf("json: invalid number literal, trying to unmarshal %q into Number", )
			}
			.SetString(string())
		case reflect.Interface:
			if .NumMethod() == 0 {
				.Set(reflect.ValueOf(string()))
			} else {
				.saveError(&UnmarshalTypeError{Value: "string", Type: .Type(), Offset: int64(.readIndex())})
			}
		}

	default: // number
		if  != '-' && ( < '0' ||  > '9') {
			if  {
				return fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", , .Type())
			}
			panic(phasePanicMsg)
		}
		 := string()
		switch .Kind() {
		default:
			if .Kind() == reflect.String && .Type() == numberType {
				// s must be a valid number, because it's
				// already been tokenized.
				.SetString()
				break
			}
			if  {
				return fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", , .Type())
			}
			.saveError(&UnmarshalTypeError{Value: "number", Type: .Type(), Offset: int64(.readIndex())})
		case reflect.Interface:
			,  := .convertNumber()
			if  != nil {
				.saveError()
				break
			}
			if .NumMethod() != 0 {
				.saveError(&UnmarshalTypeError{Value: "number", Type: .Type(), Offset: int64(.readIndex())})
				break
			}
			.Set(reflect.ValueOf())

		case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
			,  := strconv.ParseInt(, 10, 64)
			if  != nil || .OverflowInt() {
				.saveError(&UnmarshalTypeError{Value: "number " + , Type: .Type(), Offset: int64(.readIndex())})
				break
			}
			.SetInt()

		case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
			,  := strconv.ParseUint(, 10, 64)
			if  != nil || .OverflowUint() {
				.saveError(&UnmarshalTypeError{Value: "number " + , Type: .Type(), Offset: int64(.readIndex())})
				break
			}
			.SetUint()

		case reflect.Float32, reflect.Float64:
			,  := strconv.ParseFloat(, .Type().Bits())
			if  != nil || .OverflowFloat() {
				.saveError(&UnmarshalTypeError{Value: "number " + , Type: .Type(), Offset: int64(.readIndex())})
				break
			}
			.SetFloat()
		}
	}
	return nil
}

// The xxxInterface routines build up a value to be stored
// in an empty interface. They are not strictly necessary,
// but they avoid the weight of reflection in this common case.

// valueInterface is like value but returns interface{}
func ( *decodeState) () ( interface{}) {
	switch .opcode {
	default:
		panic(phasePanicMsg)
	case scanBeginArray:
		 = .arrayInterface()
		.scanNext()
	case scanBeginObject:
		 = .objectInterface()
		.scanNext()
	case scanBeginLiteral:
		 = .literalInterface()
	}
	return
}

// arrayInterface is like array but returns []interface{}.
func ( *decodeState) () []interface{} {
	var  = make([]interface{}, 0)
	for {
		// Look ahead for ] - can only happen on first iteration.
		.scanWhile(scanSkipSpace)
		if .opcode == scanEndArray {
			break
		}

		 = append(, .valueInterface())

		// Next token must be , or ].
		if .opcode == scanSkipSpace {
			.scanWhile(scanSkipSpace)
		}
		if .opcode == scanEndArray {
			break
		}
		if .opcode != scanArrayValue {
			panic(phasePanicMsg)
		}
	}
	return 
}

// objectInterface is like object but returns map[string]interface{}.
func ( *decodeState) () map[string]interface{} {
	 := make(map[string]interface{})
	for {
		// Read opening " of string key or closing }.
		.scanWhile(scanSkipSpace)
		if .opcode == scanEndObject {
			// closing } - can only happen on first iteration.
			break
		}
		if .opcode != scanBeginLiteral {
			panic(phasePanicMsg)
		}

		// Read string key.
		 := .readIndex()
		.rescanLiteral()
		 := .data[:.readIndex()]
		,  := unquote()
		if ! {
			panic(phasePanicMsg)
		}

		// Read : before value.
		if .opcode == scanSkipSpace {
			.scanWhile(scanSkipSpace)
		}
		if .opcode != scanObjectKey {
			panic(phasePanicMsg)
		}
		.scanWhile(scanSkipSpace)

		// Read value.
		[] = .valueInterface()

		// Next token must be , or }.
		if .opcode == scanSkipSpace {
			.scanWhile(scanSkipSpace)
		}
		if .opcode == scanEndObject {
			break
		}
		if .opcode != scanObjectValue {
			panic(phasePanicMsg)
		}
	}
	return 
}

// literalInterface consumes and returns a literal from d.data[d.off-1:] and
// it reads the following byte ahead. The first byte of the literal has been
// read already (that's how the caller knows it's a literal).
func ( *decodeState) () interface{} {
	// All bytes inside literal return scanContinue op code.
	 := .readIndex()
	.rescanLiteral()

	 := .data[:.readIndex()]

	switch  := [0];  {
	case 'n': // null
		return nil

	case 't', 'f': // true, false
		return  == 't'

	case '"': // string
		,  := unquote()
		if ! {
			panic(phasePanicMsg)
		}
		return 

	default: // number
		if  != '-' && ( < '0' ||  > '9') {
			panic(phasePanicMsg)
		}
		,  := .convertNumber(string())
		if  != nil {
			.saveError()
		}
		return 
	}
}

// getu4 decodes \uXXXX from the beginning of s, returning the hex value,
// or it returns -1.
func getu4( []byte) rune {
	if len() < 6 || [0] != '\\' || [1] != 'u' {
		return -1
	}
	var  rune
	for ,  := range [2:6] {
		switch {
		case '0' <=  &&  <= '9':
			 =  - '0'
		case 'a' <=  &&  <= 'f':
			 =  - 'a' + 10
		case 'A' <=  &&  <= 'F':
			 =  - 'A' + 10
		default:
			return -1
		}
		 = *16 + rune()
	}
	return 
}

// unquote converts a quoted JSON string literal s into an actual string t.
// The rules are different than for Go, so cannot use strconv.Unquote.
func unquote( []byte) ( string,  bool) {
	,  = unquoteBytes()
	 = string()
	return
}

func unquoteBytes( []byte) ( []byte,  bool) {
	if len() < 2 || [0] != '"' || [len()-1] != '"' {
		return
	}
	 = [1 : len()-1]

	// Check for unusual characters. If there are none,
	// then no unquoting is needed, so return a slice of the
	// original bytes.
	 := 0
	for  < len() {
		 := []
		if  == '\\' ||  == '"' ||  < ' ' {
			break
		}
		if  < utf8.RuneSelf {
			++
			continue
		}
		,  := utf8.DecodeRune([:])
		if  == utf8.RuneError &&  == 1 {
			break
		}
		 += 
	}
	if  == len() {
		return , true
	}

	 := make([]byte, len()+2*utf8.UTFMax)
	 := copy(, [0:])
	for  < len() {
		// Out of room? Can only happen if s is full of
		// malformed UTF-8 and we're replacing each
		// byte with RuneError.
		if  >= len()-2*utf8.UTFMax {
			 := make([]byte, (len()+utf8.UTFMax)*2)
			copy(, [0:])
			 = 
		}
		switch  := []; {
		case  == '\\':
			++
			if  >= len() {
				return
			}
			switch [] {
			default:
				return
			case '"', '\\', '/', '\'':
				[] = []
				++
				++
			case 'b':
				[] = '\b'
				++
				++
			case 'f':
				[] = '\f'
				++
				++
			case 'n':
				[] = '\n'
				++
				++
			case 'r':
				[] = '\r'
				++
				++
			case 't':
				[] = '\t'
				++
				++
			case 'u':
				--
				 := getu4([:])
				if  < 0 {
					return
				}
				 += 6
				if utf16.IsSurrogate() {
					 := getu4([:])
					if  := utf16.DecodeRune(, );  != unicode.ReplacementChar {
						// A valid pair; consume.
						 += 6
						 += utf8.EncodeRune([:], )
						break
					}
					// Invalid surrogate; fall back to replacement rune.
					 = unicode.ReplacementChar
				}
				 += utf8.EncodeRune([:], )
			}

		// Quote, control characters are invalid.
		case  == '"',  < ' ':
			return

		// ASCII
		case  < utf8.RuneSelf:
			[] = 
			++
			++

		// Coerce to well-formed UTF-8.
		default:
			,  := utf8.DecodeRune([:])
			 += 
			 += utf8.EncodeRune([:], )
		}
	}
	return [0:], true
}