package jx

import (
	
)

// ObjBytes calls f for every key in object, using byte slice as key.
//
// The key value is valid only until f is not returned.
func ( *Decoder) ( func( *Decoder,  []byte) error) error {
	if  := .consume('{');  != nil {
		return errors.Wrap(, `"{" expected`)
	}
	if  == nil {
		return .skipObj()
	}
	if  := .incDepth();  != nil {
		return 
	}
	,  := .more()
	if  != nil {
		return errors.Wrap(, `'"' or "}" expected`)
	}
	if  == '}' {
		return .decDepth()
	}
	.unread()
	// Do not reference internal buffer for key if decoder is not buffered.
	//
	// Otherwise, subsequent reads may overwrite the key.
	//
	// See https://github.com/go-faster/jx/pull/62.
	 := .reader == nil

	,  := .str(value{raw: })
	if  != nil {
		return errors.Wrap(, "field name")
	}
	if  := .consume(':');  != nil {
		return errors.Wrap(, `":" expected`)
	}
	// Skip whitespace.
	if _,  = .more();  != nil {
		return 
	}
	.unread()
	if  := (, .buf);  != nil {
		return errors.Wrap(, "callback")
	}

	,  = .more()
	if  != nil {
		return errors.Wrap(, `"," or "}" expected`)
	}
	for  == ',' {
		,  := .str(value{raw: })
		if  != nil {
			return errors.Wrap(, "field name")
		}
		if  := .consume(':');  != nil {
			return errors.Wrap(, `":" expected`)
		}
		// Check that value exists.
		if _,  = .more();  != nil {
			return 
		}
		.unread()
		if  := (, .buf);  != nil {
			return errors.Wrap(, "callback")
		}
		if ,  = .more();  != nil {
			return 
		}
	}
	if  != '}' {
		 := badToken(, .offset()-1)
		return errors.Wrap(, `"}" expected`)
	}
	return .decDepth()
}

// Obj reads json object, calling f on each field.
//
// Use ObjBytes to reduce heap allocations for keys.
func ( *Decoder) ( func( *Decoder,  string) error) error {
	if  == nil {
		// Skipping object.
		return .ObjBytes(nil)
	}
	return .ObjBytes(func( *Decoder,  []byte) error {
		return (, string())
	})
}