package pgtype

import (
	
	
	
	
)

type JSON struct {
	Bytes  []byte
	Status Status
}

func ( *JSON) ( interface{}) error {
	if  == nil {
		* = JSON{Status: Null}
		return nil
	}

	if ,  := .(interface{ () interface{} });  {
		 := .()
		if  !=  {
			return .()
		}
	}

	switch value := .(type) {
	case string:
		* = JSON{Bytes: []byte(), Status: Present}
	case *string:
		if  == nil {
			* = JSON{Status: Null}
		} else {
			* = JSON{Bytes: []byte(*), Status: Present}
		}
	case []byte:
		if  == nil {
			* = JSON{Status: Null}
		} else {
			* = JSON{Bytes: , Status: Present}
Encode* methods are defined on *JSON. If JSON is passed directly then the struct itself would be encoded instead of Bytes. This is clearly a footgun so detect and return an error. See https://github.com/jackc/pgx/issues/350.
	case JSON:
Same as above but for JSONB (because they share implementation)
	case JSONB:
		return errors.New("use pointer to pgtype.JSONB instead of value")

	default:
		,  := json.Marshal()
		if  != nil {
			return 
		}
		* = JSON{Bytes: , Status: Present}
	}

	return nil
}

func ( JSON) () interface{} {
	switch .Status {
	case Present:
		var  interface{}
		 := json.Unmarshal(.Bytes, &)
		if  != nil {
			return 
		}
		return 
	case Null:
		return nil
	default:
		return .Status
	}
}

func ( *JSON) ( interface{}) error {
	switch v := .(type) {
	case *string:
		if .Status == Present {
			* = string(.Bytes)
		} else {
			return fmt.Errorf("cannot assign non-present status to %T", )
		}
	case **string:
		if .Status == Present {
			 := string(.Bytes)
			* = &
			return nil
		} else {
			* = nil
			return nil
		}
	case *[]byte:
		if .Status != Present {
			* = nil
		} else {
			 := make([]byte, len(.Bytes))
			copy(, .Bytes)
			* = 
		}
	default:
		 := .Bytes
		if  == nil || .Status != Present {
			 = []byte("null")
		}

		return json.Unmarshal(, )
	}

	return nil
}

func (JSON) () int16 {
	return TextFormatCode
}

func ( *JSON) ( *ConnInfo,  []byte) error {
	if  == nil {
		* = JSON{Status: Null}
		return nil
	}

	* = JSON{Bytes: , Status: Present}
	return nil
}

func ( *JSON) ( *ConnInfo,  []byte) error {
	return .DecodeText(, )
}

func (JSON) () int16 {
	return TextFormatCode
}

func ( JSON) ( *ConnInfo,  []byte) ([]byte, error) {
	switch .Status {
	case Null:
		return nil, nil
	case Undefined:
		return nil, errUndefined
	}

	return append(, .Bytes...), nil
}

func ( JSON) ( *ConnInfo,  []byte) ([]byte, error) {
	return .EncodeText(, )
}
Scan implements the database/sql Scanner interface.
func ( *JSON) ( interface{}) error {
	if  == nil {
		* = JSON{Status: Null}
		return nil
	}

	switch src := .(type) {
	case string:
		return .DecodeText(nil, []byte())
	case []byte:
		 := make([]byte, len())
		copy(, )
		return .DecodeText(nil, )
	}

	return fmt.Errorf("cannot scan %T", )
}
Value implements the database/sql/driver Valuer interface.
func ( JSON) () (driver.Value, error) {
	switch .Status {
	case Present:
		return .Bytes, nil
	case Null:
		return nil, nil
	default:
		return nil, errUndefined
	}
}

func ( JSON) () ([]byte, error) {
	switch .Status {
	case Present:
		return .Bytes, nil
	case Null:
		return []byte("null"), nil
	case Undefined:
		return nil, errUndefined
	}

	return nil, errBadStatus
}

func ( *JSON) ( []byte) error {
	if  == nil || string() == "null" {
		* = JSON{Status: Null}
	} else {
		* = JSON{Bytes: , Status: Present}
	}
	return nil