func (u *ror2Reader) readAndDecodePrimitiveFieldValue() (string, error) {
	startPos, err := u.unsafeReadPrimitiveFieldValue()
	if err != nil {
		return "", err
	}
	s, err := u.decoder(string(u.data[startPos:u.pos]))
	return s, u.wrapDeserializationError(err)
}
readAndDecodePrimitiveFieldValue calls unsafeReadPrimitiveFieldValue and decodes the field's value using the given
decoder.
package restlicodec
import (
	"fmt"
	"net/url"
	"strconv"
)
type ror2ReaderState int
const (
	noState = ror2ReaderState(iota)
	inMap
	inArray
)
const list = "List("
func (s *ror2ReaderState) location() string {
	switch *s {
	case inMap:
		return "map"
	case inArray:
		return "array"
	default:
		return "string"
	}
}
type ror2Reader struct {
	missingFieldsTracker
	decoder func(string) (string, error)
	data    []byte
	pos     int
	state   ror2ReaderState
}
func validateRor2Input(data string) error {
	parens := 0
	for i, c := range data {
		switch c {
		case '(':
			parens++
		case ')':
			parens--
			if parens < 0 {
				return &DeserializationError{
					Err: fmt.Errorf("illegal ROR2 string has unbalanced delimiters at %d: %q", i, data),
				}
			}
		}
	}
	return nil
}
func NewRor2Reader(data string) (Reader, error) {
	err := validateRor2Input(data)
	if err != nil {
		return nil, err
	}
	return &ror2Reader{
		decoder: url.PathUnescape,
		data:    []byte(data),
	}, nil
}
func (u *ror2Reader) readFieldName() (string, error) {
	if u.data[u.pos] == ')' {
		return ")", nil
	}
	startPos := u.pos
loop:
	for ; u.pos < len(u.data); u.pos++ {
		switch u.data[u.pos] {
		case ':':
			if u.pos == startPos {
				return "", u.errorf("invalid ROR2 %s string has empty field name at %d: %q", u.state.location(), u.pos, string(u.data))
			}
			break loop
		case ',', ')':
			return "", u.errorf("invalid ROR2 %s string has invalid field name at %d: %q", u.state.location(), u.pos, string(u.data))
		}
	}
	s := string(u.data[startPos:u.pos])
	u.pos++
	return s, nil
}
func (u *ror2Reader) ReadMap(mapReader MapReader) (err error) {
	u.state = inMap
	if !u.atMap() {
		return u.errorf("invalid ROR2 %s string does not start with '(': %q", u.state.location(), string(u.data))
	}
	u.pos++
	if len(u.data) < u.pos {
		return u.errorf("invalid ROR2 %s is unclosed: %q", u.state.location(), string(u.data))
	}
loop:
	for {
		var fieldName string
		fieldName, err = u.readFieldName()
		if err != nil {
			return err
		}
		switch fieldName {
		case ")":
			u.pos++
			break loop
		default:
			u.enterMapScope(fieldName)
			err = mapReader(u, fieldName)
			if err != nil {
				return err
			}
			u.exitScope()
			switch u.data[u.pos] {
			case ',':
				u.pos++
				continue loop
			case ')':
				u.pos++
				break loop
			default:
				return u.errorf("invalid ROR2 string does has incorrect object delimiter at %d: %q", u.pos, string(u.data))
			}
		}
	}
	return nil
}
func (u *ror2Reader) atMap() bool {
	return len(u.data) > u.pos && u.data[u.pos] == '('
}
func (u *ror2Reader) ReadArray(arrayReader ArrayReader) (err error) {
	u.state = inArray
	if !u.atArray() {
		return u.errorf("invalid ROR2 %s string does not start with "+list+": %q", u.state.location(), string(u.data))
	}
	u.pos += len(list)
	if len(u.data) < u.pos {
		return u.errorf("invalid ROR2 %s string is not closed %q", u.state.location(), string(u.data))
	}
	if u.data[u.pos] == ')' {
		u.pos++
		return nil
	}
	index := 0
loop:
	for {
		u.enterArrayScope(index)
		err = arrayReader(u)
		if err != nil {
			return err
		}
		u.exitScope()
		index++
		switch u.data[u.pos] {
		case ',':
			u.pos++
			continue loop
		case ')':
			u.pos++
			break loop
		default:
			return u.errorf("invalid ROR2 string has incorrect array delimiter at %d: %q", u.pos, string(u.data))
		}
	}
	return nil
}
func (u *ror2Reader) atArray() bool {
	return len(u.data)-u.pos > len(list) && string(u.data[u.pos:u.pos+len(list)]) == list
}
func (u *ror2Reader) ReadInterface() (interface{}, error) {
	switch {
	case u.atMap():
		m := make(map[string]interface{})
		err := u.ReadMap(func(reader Reader, k string) (err error) {
			var v interface{}
			v, err = reader.ReadInterface()
			if err != nil {
				return err
			}
			m[k] = v
			return nil
		})
		if err != nil {
			return nil, err
		}
		return m, nil
	case u.atArray():
		var a []interface{}
		err := u.ReadArray(func(reader Reader) (err error) {
			var v interface{}
			v, err = reader.ReadInterface()
			if err != nil {
				return err
			}
			a = append(a, v)
			return nil
		})
		if err != nil {
			return nil, err
		}
		return a, nil
	default:
		return u.ReadString()
	}
}
func (u *ror2Reader) Skip() error {
	if u.pos == 0 {
		u.pos = len(u.data)
		return nil
	}
	inMapOrArray := u.atArray() || u.atMap()
	parens := 0
	for ; u.pos < len(u.data); u.pos++ {
		switch u.data[u.pos] {
		case '(':
			if !inMapOrArray {
				return u.errorf("unescaped '(' at %d: %q", u.pos, string(u.data))
			} else {
				parens++
			}
		case ',':
			if !inMapOrArray {
				return nil
			} else {
				if parens == 0 {
					return nil
				}
			}
		case ')':
			if !inMapOrArray {
				return nil
			} else {
				if parens == 0 {
					return nil
				}
				parens--
			}
		}
	}
	return u.errorf("invalid ROR2 string has incorrect object delimiters: %q", string(u.data))
}
func (u *ror2Reader) ReadRawBytes() ([]byte, error) {
	startPos := u.pos
	err := u.Skip()
	if err != nil {
		return nil, u.wrapDeserializationError(err)
	} else {
		return u.data[startPos:u.pos], nil
	}
}
func (u *ror2Reader) unsafeReadPrimitiveFieldValue() (startPos int, err error) {
	startPos = u.pos
	if startPos == 0 {
		u.pos = len(u.data)
	} else {
		for ; u.pos < len(u.data); u.pos++ {
			if c := u.data[u.pos]; c == ',' || c == ')' {
				break
			}
		}
		if u.pos == len(u.data) {
			return 0, u.errorf("invalid ROR2 string has incorrect field delimiters: %q", string(u.data))
		}
	}
	for i := startPos; i < u.pos; i++ {
		if c := u.data[i]; c == '(' || c == ',' || c == ')' {
			return 0, u.errorf("illegal unescaped '%s' in primitive field at %d: %q", string(c), i, string(u.data))
		}
	}
	return startPos, nil
}