package gojay

import (
	
	
)

// UnmarshalJSONArray parses the JSON-encoded data and stores the result in the value pointed to by v.
//
// v must implement UnmarshalerJSONArray.
//
// If a JSON value is not appropriate for a given target type, or if a JSON number
// overflows the target type, UnmarshalJSONArray skips that field and completes the unmarshaling as best it can.
func ( []byte,  UnmarshalerJSONArray) error {
	 := borrowDecoder(nil, 0)
	defer .Release()
	.data = make([]byte, len())
	copy(.data, )
	.length = len()
	,  := .decodeArray()
	if  != nil {
		return 
	}
	if .err != nil {
		return .err
	}
	return nil
}

// UnmarshalJSONObject parses the JSON-encoded data and stores the result in the value pointed to by v.
//
// v must implement UnmarshalerJSONObject.
//
// If a JSON value is not appropriate for a given target type, or if a JSON number
// overflows the target type, UnmarshalJSONObject skips that field and completes the unmarshaling as best it can.
func ( []byte,  UnmarshalerJSONObject) error {
	 := borrowDecoder(nil, 0)
	defer .Release()
	.data = make([]byte, len())
	copy(.data, )
	.length = len()
	,  := .decodeObject()
	if  != nil {
		return 
	}
	if .err != nil {
		return .err
	}
	return nil
}

// Unmarshal parses the JSON-encoded data and stores the result in the value pointed to by v.
// If v is nil, not an implementation of UnmarshalerJSONObject or UnmarshalerJSONArray or not one of the following types:
// 	*string, **string, *int, **int, *int8, **int8, *int16, **int16, *int32, **int32, *int64, **int64, *uint8, **uint8, *uint16, **uint16,
// 	*uint32, **uint32, *uint64, **uint64, *float64, **float64, *float32, **float32, *bool, **bool
// Unmarshal returns an InvalidUnmarshalError.
//
//
// 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.
func ( []byte,  interface{}) error {
	var  error
	var  *Decoder
	switch vt := .(type) {
	case *string:
		 = borrowDecoder(nil, 0)
		.length = len()
		.data = 
		 = .decodeString()
	case **string:
		 = borrowDecoder(nil, 0)
		.length = len()
		.data = 
		 = .decodeStringNull()
	case *int:
		 = borrowDecoder(nil, 0)
		.length = len()
		.data = 
		 = .decodeInt()
	case **int:
		 = borrowDecoder(nil, 0)
		.length = len()
		.data = 
		 = .decodeIntNull()
	case *int8:
		 = borrowDecoder(nil, 0)
		.length = len()
		.data = 
		 = .decodeInt8()
	case **int8:
		 = borrowDecoder(nil, 0)
		.length = len()
		.data = 
		 = .decodeInt8Null()
	case *int16:
		 = borrowDecoder(nil, 0)
		.length = len()
		.data = 
		 = .decodeInt16()
	case **int16:
		 = borrowDecoder(nil, 0)
		.length = len()
		.data = 
		 = .decodeInt16Null()
	case *int32:
		 = borrowDecoder(nil, 0)
		.length = len()
		.data = 
		 = .decodeInt32()
	case **int32:
		 = borrowDecoder(nil, 0)
		.length = len()
		.data = 
		 = .decodeInt32Null()
	case *int64:
		 = borrowDecoder(nil, 0)
		.length = len()
		.data = 
		 = .decodeInt64()
	case **int64:
		 = borrowDecoder(nil, 0)
		.length = len()
		.data = 
		 = .decodeInt64Null()
	case *uint8:
		 = borrowDecoder(nil, 0)
		.length = len()
		.data = 
		 = .decodeUint8()
	case **uint8:
		 = borrowDecoder(nil, 0)
		.length = len()
		.data = 
		 = .decodeUint8Null()
	case *uint16:
		 = borrowDecoder(nil, 0)
		.length = len()
		.data = 
		 = .decodeUint16()
	case **uint16:
		 = borrowDecoder(nil, 0)
		.length = len()
		.data = 
		 = .decodeUint16Null()
	case *uint32:
		 = borrowDecoder(nil, 0)
		.length = len()
		.data = 
		 = .decodeUint32()
	case **uint32:
		 = borrowDecoder(nil, 0)
		.length = len()
		.data = 
		 = .decodeUint32Null()
	case *uint64:
		 = borrowDecoder(nil, 0)
		.length = len()
		.data = 
		 = .decodeUint64()
	case **uint64:
		 = borrowDecoder(nil, 0)
		.length = len()
		.data = 
		 = .decodeUint64Null()
	case *float64:
		 = borrowDecoder(nil, 0)
		.length = len()
		.data = 
		 = .decodeFloat64()
	case **float64:
		 = borrowDecoder(nil, 0)
		.length = len()
		.data = 
		 = .decodeFloat64Null()
	case *float32:
		 = borrowDecoder(nil, 0)
		.length = len()
		.data = 
		 = .decodeFloat32()
	case **float32:
		 = borrowDecoder(nil, 0)
		.length = len()
		.data = 
		 = .decodeFloat32Null()
	case *bool:
		 = borrowDecoder(nil, 0)
		.length = len()
		.data = 
		 = .decodeBool()
	case **bool:
		 = borrowDecoder(nil, 0)
		.length = len()
		.data = 
		 = .decodeBoolNull()
	case UnmarshalerJSONObject:
		 = borrowDecoder(nil, 0)
		.length = len()
		.data = make([]byte, len())
		copy(.data, )
		_,  = .decodeObject()
	case UnmarshalerJSONArray:
		 = borrowDecoder(nil, 0)
		.length = len()
		.data = make([]byte, len())
		copy(.data, )
		_,  = .decodeArray()
	case *interface{}:
		 = borrowDecoder(nil, 0)
		.length = len()
		.data = make([]byte, len())
		copy(.data, )
		 = .decodeInterface()
	default:
		return InvalidUnmarshalError(fmt.Sprintf(invalidUnmarshalErrorMsg, ))
	}
	defer .Release()
	if  != nil {
		return 
	}
	return .err
}

// UnmarshalerJSONObject is the interface to implement to decode a JSON Object.
type UnmarshalerJSONObject interface {
	UnmarshalJSONObject(*Decoder, string) error
	NKeys() int
}

// UnmarshalerJSONArray is the interface to implement to decode a JSON Array.
type UnmarshalerJSONArray interface {
	UnmarshalJSONArray(*Decoder) error
}

// A Decoder reads and decodes JSON values from an input stream.
type Decoder struct {
	r          io.Reader
	data       []byte
	err        error
	isPooled   byte
	called     byte
	child      byte
	cursor     int
	length     int
	keysDone   int
	arrayIndex int
}

// Decode reads the next JSON-encoded value from the decoder's input (io.Reader) and stores it in the value pointed to by v.
//
// See the documentation for Unmarshal for details about the conversion of JSON into a Go value.
// The differences between Decode and Unmarshal are:
// 	- Decode reads from an io.Reader in the Decoder, whereas Unmarshal reads from a []byte
// 	- Decode leaves to the user the option of borrowing and releasing a Decoder, whereas Unmarshal internally always borrows a Decoder and releases it when the unmarshaling is completed
func ( *Decoder) ( interface{}) error {
	if .isPooled == 1 {
		panic(InvalidUsagePooledDecoderError("Invalid usage of pooled decoder"))
	}
	var  error
	switch vt := .(type) {
	case *string:
		 = .decodeString()
	case **string:
		 = .decodeStringNull()
	case *int:
		 = .decodeInt()
	case **int:
		 = .decodeIntNull()
	case *int8:
		 = .decodeInt8()
	case **int8:
		 = .decodeInt8Null()
	case *int16:
		 = .decodeInt16()
	case **int16:
		 = .decodeInt16Null()
	case *int32:
		 = .decodeInt32()
	case **int32:
		 = .decodeInt32Null()
	case *int64:
		 = .decodeInt64()
	case **int64:
		 = .decodeInt64Null()
	case *uint8:
		 = .decodeUint8()
	case **uint8:
		 = .decodeUint8Null()
	case *uint16:
		 = .decodeUint16()
	case **uint16:
		 = .decodeUint16Null()
	case *uint32:
		 = .decodeUint32()
	case **uint32:
		 = .decodeUint32Null()
	case *uint64:
		 = .decodeUint64()
	case **uint64:
		 = .decodeUint64Null()
	case *float64:
		 = .decodeFloat64()
	case **float64:
		 = .decodeFloat64Null()
	case *float32:
		 = .decodeFloat32()
	case **float32:
		 = .decodeFloat32Null()
	case *bool:
		 = .decodeBool()
	case **bool:
		 = .decodeBoolNull()
	case UnmarshalerJSONObject:
		_,  = .decodeObject()
	case UnmarshalerJSONArray:
		_,  = .decodeArray()
	case *EmbeddedJSON:
		 = .decodeEmbeddedJSON()
	case *interface{}:
		 = .decodeInterface()
	default:
		return InvalidUnmarshalError(fmt.Sprintf(invalidUnmarshalErrorMsg, ))
	}
	if  != nil {
		return 
	}
	return .err
}

// Non exported

func isDigit( byte) bool {
	switch  {
	case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
		return true
	default:
		return false
	}
}

func ( *Decoder) () bool {
	if .r != nil {
		// if we reach the end, double the buffer to ensure there's always more space
		if len(.data) == .length {
			 := .length * 2
			if  == 0 {
				 = 512
			}
			 := make([]byte, , )
			copy(, .data)
			.data = 
		}
		var  int
		var  error
		for  == 0 {
			,  = .r.Read(.data[.length:])
			if  != nil {
				if  != io.EOF {
					.err = 
					return false
				}
				if  == 0 {
					return false
				}
				.length = .length + 
				return true
			}
		}
		.length = .length + 
		return true
	}
	return false
}

func ( *Decoder) () byte {
	for ; .cursor < .length || .read(); .cursor++ {
		switch .data[.cursor] {
		case ' ', '\n', '\t', '\r', ',':
			continue
		}
		 := .data[.cursor]
		return 
	}
	return 0
}