// Code generated by ogen, DO NOT EDIT.

package gsn

import (
	"math/bits"
	"strconv"
	"time"

	"github.com/go-faster/errors"
	"github.com/go-faster/jx"

	"github.com/ogen-go/ogen/json"
	"github.com/ogen-go/ogen/validate"
)

// Encode implements json.Marshaler.
func (s *Error) Encode(e *jx.Encoder) {
	e.ObjStart()
	s.encodeFields(e)
	e.ObjEnd()
}

// encodeFields encodes fields.
func (s *Error) encodeFields(e *jx.Encoder) {
	{
		e.FieldStart("message")
		e.Str(s.Message)
	}
	{
		if s.Details.Set {
			e.FieldStart("details")
			s.Details.Encode(e)
		}
	}
}

var jsonFieldsNameOfError = [2]string{
	0: "message",
	1: "details",
}

// Decode decodes Error from json.
func (s *Error) Decode(d *jx.Decoder) error {
	if s == nil {
		return errors.New("invalid: unable to decode Error to nil")
	}
	var requiredBitSet [1]uint8

	if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error {
		switch string(k) {
		case "message":
			requiredBitSet[0] |= 1 << 0
			if err := func() error {
				v, err := d.Str()
				s.Message = string(v)
				if err != nil {
					return err
				}
				return nil
			}(); err != nil {
				return errors.Wrap(err, "decode field \"message\"")
			}
		case "details":
			if err := func() error {
				s.Details.Reset()
				if err := s.Details.Decode(d); err != nil {
					return err
				}
				return nil
			}(); err != nil {
				return errors.Wrap(err, "decode field \"details\"")
			}
		default:
			return d.Skip()
		}
		return nil
	}); err != nil {
		return errors.Wrap(err, "decode Error")
	}
	// Validate required fields.
	var failures []validate.FieldError
	for i, mask := range [1]uint8{
		0b00000001,
	} {
		if result := (requiredBitSet[i] & mask) ^ mask; result != 0 {
			// Mask only required fields and check equality to mask using XOR.
			//
			// If XOR result is not zero, result is not equal to expected, so some fields are missed.
			// Bits of fields which would be set are actually bits of missed fields.
			missed := bits.OnesCount8(result)
			for bitN := 0; bitN < missed; bitN++ {
				bitIdx := bits.TrailingZeros8(result)
				fieldIdx := i*8 + bitIdx
				var name string
				if fieldIdx < len(jsonFieldsNameOfError) {
					name = jsonFieldsNameOfError[fieldIdx]
				} else {
					name = strconv.Itoa(fieldIdx)
				}
				failures = append(failures, validate.FieldError{
					Name:  name,
					Error: validate.ErrFieldRequired,
				})
				// Reset bit.
				result &^= 1 << bitIdx
			}
		}
	}
	if len(failures) > 0 {
		return &validate.Error{Fields: failures}
	}

	return nil
}

// MarshalJSON implements stdjson.Marshaler.
func (s *Error) MarshalJSON() ([]byte, error) {
	e := jx.Encoder{}
	s.Encode(&e)
	return e.Bytes(), nil
}

// UnmarshalJSON implements stdjson.Unmarshaler.
func (s *Error) UnmarshalJSON(data []byte) error {
	d := jx.DecodeBytes(data)
	return s.Decode(d)
}

// Encode encodes bool as json.
func (o OptBool) Encode(e *jx.Encoder) {
	if !o.Set {
		return
	}
	e.Bool(bool(o.Value))
}

// Decode decodes bool from json.
func (o *OptBool) Decode(d *jx.Decoder) error {
	if o == nil {
		return errors.New("invalid: unable to decode OptBool to nil")
	}
	o.Set = true
	v, err := d.Bool()
	if err != nil {
		return err
	}
	o.Value = bool(v)
	return nil
}

// MarshalJSON implements stdjson.Marshaler.
func (s OptBool) MarshalJSON() ([]byte, error) {
	e := jx.Encoder{}
	s.Encode(&e)
	return e.Bytes(), nil
}

// UnmarshalJSON implements stdjson.Unmarshaler.
func (s *OptBool) UnmarshalJSON(data []byte) error {
	d := jx.DecodeBytes(data)
	return s.Decode(d)
}

// Encode encodes time.Time as json.
func (o OptDateTime) Encode(e *jx.Encoder, format func(*jx.Encoder, time.Time)) {
	if !o.Set {
		return
	}
	format(e, o.Value)
}

// Decode decodes time.Time from json.
func (o *OptDateTime) Decode(d *jx.Decoder, format func(*jx.Decoder) (time.Time, error)) error {
	if o == nil {
		return errors.New("invalid: unable to decode OptDateTime to nil")
	}
	o.Set = true
	v, err := format(d)
	if err != nil {
		return err
	}
	o.Value = v
	return nil
}

// MarshalJSON implements stdjson.Marshaler.
func (s OptDateTime) MarshalJSON() ([]byte, error) {
	e := jx.Encoder{}
	s.Encode(&e, json.EncodeDateTime)
	return e.Bytes(), nil
}

// UnmarshalJSON implements stdjson.Unmarshaler.
func (s *OptDateTime) UnmarshalJSON(data []byte) error {
	d := jx.DecodeBytes(data)
	return s.Decode(d, json.DecodeDateTime)
}

// Encode encodes float64 as json.
func (o OptFloat64) Encode(e *jx.Encoder) {
	if !o.Set {
		return
	}
	e.Float64(float64(o.Value))
}

// Decode decodes float64 from json.
func (o *OptFloat64) Decode(d *jx.Decoder) error {
	if o == nil {
		return errors.New("invalid: unable to decode OptFloat64 to nil")
	}
	o.Set = true
	v, err := d.Float64()
	if err != nil {
		return err
	}
	o.Value = float64(v)
	return nil
}

// MarshalJSON implements stdjson.Marshaler.
func (s OptFloat64) MarshalJSON() ([]byte, error) {
	e := jx.Encoder{}
	s.Encode(&e)
	return e.Bytes(), nil
}

// UnmarshalJSON implements stdjson.Unmarshaler.
func (s *OptFloat64) UnmarshalJSON(data []byte) error {
	d := jx.DecodeBytes(data)
	return s.Decode(d)
}

// Encode encodes PredictionParameters as json.
func (o OptPredictionParameters) Encode(e *jx.Encoder) {
	if !o.Set {
		return
	}
	o.Value.Encode(e)
}

// Decode decodes PredictionParameters from json.
func (o *OptPredictionParameters) Decode(d *jx.Decoder) error {
	if o == nil {
		return errors.New("invalid: unable to decode OptPredictionParameters to nil")
	}
	o.Set = true
	if err := o.Value.Decode(d); err != nil {
		return err
	}
	return nil
}

// MarshalJSON implements stdjson.Marshaler.
func (s OptPredictionParameters) MarshalJSON() ([]byte, error) {
	e := jx.Encoder{}
	s.Encode(&e)
	return e.Bytes(), nil
}

// UnmarshalJSON implements stdjson.Unmarshaler.
func (s *OptPredictionParameters) UnmarshalJSON(data []byte) error {
	d := jx.DecodeBytes(data)
	return s.Decode(d)
}

// Encode encodes PredictionParametersFormat as json.
func (o OptPredictionParametersFormat) Encode(e *jx.Encoder) {
	if !o.Set {
		return
	}
	e.Str(string(o.Value))
}

// Decode decodes PredictionParametersFormat from json.
func (o *OptPredictionParametersFormat) Decode(d *jx.Decoder) error {
	if o == nil {
		return errors.New("invalid: unable to decode OptPredictionParametersFormat to nil")
	}
	o.Set = true
	if err := o.Value.Decode(d); err != nil {
		return err
	}
	return nil
}

// MarshalJSON implements stdjson.Marshaler.
func (s OptPredictionParametersFormat) MarshalJSON() ([]byte, error) {
	e := jx.Encoder{}
	s.Encode(&e)
	return e.Bytes(), nil
}

// UnmarshalJSON implements stdjson.Unmarshaler.
func (s *OptPredictionParametersFormat) UnmarshalJSON(data []byte) error {
	d := jx.DecodeBytes(data)
	return s.Decode(d)
}

// Encode encodes PredictionParametersProfile as json.
func (o OptPredictionParametersProfile) Encode(e *jx.Encoder) {
	if !o.Set {
		return
	}
	e.Str(string(o.Value))
}

// Decode decodes PredictionParametersProfile from json.
func (o *OptPredictionParametersProfile) Decode(d *jx.Decoder) error {
	if o == nil {
		return errors.New("invalid: unable to decode OptPredictionParametersProfile to nil")
	}
	o.Set = true
	if err := o.Value.Decode(d); err != nil {
		return err
	}
	return nil
}

// MarshalJSON implements stdjson.Marshaler.
func (s OptPredictionParametersProfile) MarshalJSON() ([]byte, error) {
	e := jx.Encoder{}
	s.Encode(&e)
	return e.Bytes(), nil
}

// UnmarshalJSON implements stdjson.Unmarshaler.
func (s *OptPredictionParametersProfile) UnmarshalJSON(data []byte) error {
	d := jx.DecodeBytes(data)
	return s.Decode(d)
}

// Encode encodes string as json.
func (o OptString) Encode(e *jx.Encoder) {
	if !o.Set {
		return
	}
	e.Str(string(o.Value))
}

// Decode decodes string from json.
func (o *OptString) Decode(d *jx.Decoder) error {
	if o == nil {
		return errors.New("invalid: unable to decode OptString to nil")
	}
	o.Set = true
	v, err := d.Str()
	if err != nil {
		return err
	}
	o.Value = string(v)
	return nil
}

// MarshalJSON implements stdjson.Marshaler.
func (s OptString) MarshalJSON() ([]byte, error) {
	e := jx.Encoder{}
	s.Encode(&e)
	return e.Bytes(), nil
}

// UnmarshalJSON implements stdjson.Unmarshaler.
func (s *OptString) UnmarshalJSON(data []byte) error {
	d := jx.DecodeBytes(data)
	return s.Decode(d)
}

// Encode implements json.Marshaler.
func (s *PredictionParameters) Encode(e *jx.Encoder) {
	e.ObjStart()
	s.encodeFields(e)
	e.ObjEnd()
}

// encodeFields encodes fields.
func (s *PredictionParameters) encodeFields(e *jx.Encoder) {
	{
		if s.LaunchLatitude.Set {
			e.FieldStart("launch_latitude")
			s.LaunchLatitude.Encode(e)
		}
	}
	{
		if s.LaunchLongitude.Set {
			e.FieldStart("launch_longitude")
			s.LaunchLongitude.Encode(e)
		}
	}
	{
		if s.LaunchDatetime.Set {
			e.FieldStart("launch_datetime")
			s.LaunchDatetime.Encode(e, json.EncodeDateTime)
		}
	}
	{
		if s.LaunchAltitude.Set {
			e.FieldStart("launch_altitude")
			s.LaunchAltitude.Encode(e)
		}
	}
	{
		if s.Profile.Set {
			e.FieldStart("profile")
			s.Profile.Encode(e)
		}
	}
	{
		if s.AscentRate.Set {
			e.FieldStart("ascent_rate")
			s.AscentRate.Encode(e)
		}
	}
	{
		if s.BurstAltitude.Set {
			e.FieldStart("burst_altitude")
			s.BurstAltitude.Encode(e)
		}
	}
	{
		if s.DescentRate.Set {
			e.FieldStart("descent_rate")
			s.DescentRate.Encode(e)
		}
	}
	{
		if s.FloatAltitude.Set {
			e.FieldStart("float_altitude")
			s.FloatAltitude.Encode(e)
		}
	}
	{
		if s.StopDatetime.Set {
			e.FieldStart("stop_datetime")
			s.StopDatetime.Encode(e, json.EncodeDateTime)
		}
	}
	{
		if s.AscentCurve.Set {
			e.FieldStart("ascent_curve")
			s.AscentCurve.Encode(e)
		}
	}
	{
		if s.DescentCurve.Set {
			e.FieldStart("descent_curve")
			s.DescentCurve.Encode(e)
		}
	}
	{
		if s.Interpolate.Set {
			e.FieldStart("interpolate")
			s.Interpolate.Encode(e)
		}
	}
	{
		if s.Format.Set {
			e.FieldStart("format")
			s.Format.Encode(e)
		}
	}
	{
		if s.Dataset.Set {
			e.FieldStart("dataset")
			s.Dataset.Encode(e, json.EncodeDateTime)
		}
	}
}

var jsonFieldsNameOfPredictionParameters = [15]string{
	0:  "launch_latitude",
	1:  "launch_longitude",
	2:  "launch_datetime",
	3:  "launch_altitude",
	4:  "profile",
	5:  "ascent_rate",
	6:  "burst_altitude",
	7:  "descent_rate",
	8:  "float_altitude",
	9:  "stop_datetime",
	10: "ascent_curve",
	11: "descent_curve",
	12: "interpolate",
	13: "format",
	14: "dataset",
}

// Decode decodes PredictionParameters from json.
func (s *PredictionParameters) Decode(d *jx.Decoder) error {
	if s == nil {
		return errors.New("invalid: unable to decode PredictionParameters to nil")
	}
	s.setDefaults()

	if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error {
		switch string(k) {
		case "launch_latitude":
			if err := func() error {
				s.LaunchLatitude.Reset()
				if err := s.LaunchLatitude.Decode(d); err != nil {
					return err
				}
				return nil
			}(); err != nil {
				return errors.Wrap(err, "decode field \"launch_latitude\"")
			}
		case "launch_longitude":
			if err := func() error {
				s.LaunchLongitude.Reset()
				if err := s.LaunchLongitude.Decode(d); err != nil {
					return err
				}
				return nil
			}(); err != nil {
				return errors.Wrap(err, "decode field \"launch_longitude\"")
			}
		case "launch_datetime":
			if err := func() error {
				s.LaunchDatetime.Reset()
				if err := s.LaunchDatetime.Decode(d, json.DecodeDateTime); err != nil {
					return err
				}
				return nil
			}(); err != nil {
				return errors.Wrap(err, "decode field \"launch_datetime\"")
			}
		case "launch_altitude":
			if err := func() error {
				s.LaunchAltitude.Reset()
				if err := s.LaunchAltitude.Decode(d); err != nil {
					return err
				}
				return nil
			}(); err != nil {
				return errors.Wrap(err, "decode field \"launch_altitude\"")
			}
		case "profile":
			if err := func() error {
				s.Profile.Reset()
				if err := s.Profile.Decode(d); err != nil {
					return err
				}
				return nil
			}(); err != nil {
				return errors.Wrap(err, "decode field \"profile\"")
			}
		case "ascent_rate":
			if err := func() error {
				s.AscentRate.Reset()
				if err := s.AscentRate.Decode(d); err != nil {
					return err
				}
				return nil
			}(); err != nil {
				return errors.Wrap(err, "decode field \"ascent_rate\"")
			}
		case "burst_altitude":
			if err := func() error {
				s.BurstAltitude.Reset()
				if err := s.BurstAltitude.Decode(d); err != nil {
					return err
				}
				return nil
			}(); err != nil {
				return errors.Wrap(err, "decode field \"burst_altitude\"")
			}
		case "descent_rate":
			if err := func() error {
				s.DescentRate.Reset()
				if err := s.DescentRate.Decode(d); err != nil {
					return err
				}
				return nil
			}(); err != nil {
				return errors.Wrap(err, "decode field \"descent_rate\"")
			}
		case "float_altitude":
			if err := func() error {
				s.FloatAltitude.Reset()
				if err := s.FloatAltitude.Decode(d); err != nil {
					return err
				}
				return nil
			}(); err != nil {
				return errors.Wrap(err, "decode field \"float_altitude\"")
			}
		case "stop_datetime":
			if err := func() error {
				s.StopDatetime.Reset()
				if err := s.StopDatetime.Decode(d, json.DecodeDateTime); err != nil {
					return err
				}
				return nil
			}(); err != nil {
				return errors.Wrap(err, "decode field \"stop_datetime\"")
			}
		case "ascent_curve":
			if err := func() error {
				s.AscentCurve.Reset()
				if err := s.AscentCurve.Decode(d); err != nil {
					return err
				}
				return nil
			}(); err != nil {
				return errors.Wrap(err, "decode field \"ascent_curve\"")
			}
		case "descent_curve":
			if err := func() error {
				s.DescentCurve.Reset()
				if err := s.DescentCurve.Decode(d); err != nil {
					return err
				}
				return nil
			}(); err != nil {
				return errors.Wrap(err, "decode field \"descent_curve\"")
			}
		case "interpolate":
			if err := func() error {
				s.Interpolate.Reset()
				if err := s.Interpolate.Decode(d); err != nil {
					return err
				}
				return nil
			}(); err != nil {
				return errors.Wrap(err, "decode field \"interpolate\"")
			}
		case "format":
			if err := func() error {
				s.Format.Reset()
				if err := s.Format.Decode(d); err != nil {
					return err
				}
				return nil
			}(); err != nil {
				return errors.Wrap(err, "decode field \"format\"")
			}
		case "dataset":
			if err := func() error {
				s.Dataset.Reset()
				if err := s.Dataset.Decode(d, json.DecodeDateTime); err != nil {
					return err
				}
				return nil
			}(); err != nil {
				return errors.Wrap(err, "decode field \"dataset\"")
			}
		default:
			return d.Skip()
		}
		return nil
	}); err != nil {
		return errors.Wrap(err, "decode PredictionParameters")
	}

	return nil
}

// MarshalJSON implements stdjson.Marshaler.
func (s *PredictionParameters) MarshalJSON() ([]byte, error) {
	e := jx.Encoder{}
	s.Encode(&e)
	return e.Bytes(), nil
}

// UnmarshalJSON implements stdjson.Unmarshaler.
func (s *PredictionParameters) UnmarshalJSON(data []byte) error {
	d := jx.DecodeBytes(data)
	return s.Decode(d)
}

// Encode encodes PredictionParametersFormat as json.
func (s PredictionParametersFormat) Encode(e *jx.Encoder) {
	e.Str(string(s))
}

// Decode decodes PredictionParametersFormat from json.
func (s *PredictionParametersFormat) Decode(d *jx.Decoder) error {
	if s == nil {
		return errors.New("invalid: unable to decode PredictionParametersFormat to nil")
	}
	v, err := d.StrBytes()
	if err != nil {
		return err
	}
	// Try to use constant string.
	switch PredictionParametersFormat(v) {
	case PredictionParametersFormatJSON:
		*s = PredictionParametersFormatJSON
	default:
		*s = PredictionParametersFormat(v)
	}

	return nil
}

// MarshalJSON implements stdjson.Marshaler.
func (s PredictionParametersFormat) MarshalJSON() ([]byte, error) {
	e := jx.Encoder{}
	s.Encode(&e)
	return e.Bytes(), nil
}

// UnmarshalJSON implements stdjson.Unmarshaler.
func (s *PredictionParametersFormat) UnmarshalJSON(data []byte) error {
	d := jx.DecodeBytes(data)
	return s.Decode(d)
}

// Encode encodes PredictionParametersProfile as json.
func (s PredictionParametersProfile) Encode(e *jx.Encoder) {
	e.Str(string(s))
}

// Decode decodes PredictionParametersProfile from json.
func (s *PredictionParametersProfile) Decode(d *jx.Decoder) error {
	if s == nil {
		return errors.New("invalid: unable to decode PredictionParametersProfile to nil")
	}
	v, err := d.StrBytes()
	if err != nil {
		return err
	}
	// Try to use constant string.
	switch PredictionParametersProfile(v) {
	case PredictionParametersProfileStandardProfile:
		*s = PredictionParametersProfileStandardProfile
	case PredictionParametersProfileFloatProfile:
		*s = PredictionParametersProfileFloatProfile
	case PredictionParametersProfileReverseProfile:
		*s = PredictionParametersProfileReverseProfile
	case PredictionParametersProfileCustomProfile:
		*s = PredictionParametersProfileCustomProfile
	default:
		*s = PredictionParametersProfile(v)
	}

	return nil
}

// MarshalJSON implements stdjson.Marshaler.
func (s PredictionParametersProfile) MarshalJSON() ([]byte, error) {
	e := jx.Encoder{}
	s.Encode(&e)
	return e.Bytes(), nil
}

// UnmarshalJSON implements stdjson.Unmarshaler.
func (s *PredictionParametersProfile) UnmarshalJSON(data []byte) error {
	d := jx.DecodeBytes(data)
	return s.Decode(d)
}

// Encode implements json.Marshaler.
func (s *PredictionResult) Encode(e *jx.Encoder) {
	e.ObjStart()
	s.encodeFields(e)
	e.ObjEnd()
}

// encodeFields encodes fields.
func (s *PredictionResult) encodeFields(e *jx.Encoder) {
	{
		e.FieldStart("metadata")
		s.Metadata.Encode(e)
	}
	{
		e.FieldStart("prediction")
		e.ArrStart()
		for _, elem := range s.Prediction {
			elem.Encode(e)
		}
		e.ArrEnd()
	}
}

var jsonFieldsNameOfPredictionResult = [2]string{
	0: "metadata",
	1: "prediction",
}

// Decode decodes PredictionResult from json.
func (s *PredictionResult) Decode(d *jx.Decoder) error {
	if s == nil {
		return errors.New("invalid: unable to decode PredictionResult to nil")
	}
	var requiredBitSet [1]uint8

	if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error {
		switch string(k) {
		case "metadata":
			requiredBitSet[0] |= 1 << 0
			if err := func() error {
				if err := s.Metadata.Decode(d); err != nil {
					return err
				}
				return nil
			}(); err != nil {
				return errors.Wrap(err, "decode field \"metadata\"")
			}
		case "prediction":
			requiredBitSet[0] |= 1 << 1
			if err := func() error {
				s.Prediction = make([]PredictionResultPredictionItem, 0)
				if err := d.Arr(func(d *jx.Decoder) error {
					var elem PredictionResultPredictionItem
					if err := elem.Decode(d); err != nil {
						return err
					}
					s.Prediction = append(s.Prediction, elem)
					return nil
				}); err != nil {
					return err
				}
				return nil
			}(); err != nil {
				return errors.Wrap(err, "decode field \"prediction\"")
			}
		default:
			return d.Skip()
		}
		return nil
	}); err != nil {
		return errors.Wrap(err, "decode PredictionResult")
	}
	// Validate required fields.
	var failures []validate.FieldError
	for i, mask := range [1]uint8{
		0b00000011,
	} {
		if result := (requiredBitSet[i] & mask) ^ mask; result != 0 {
			// Mask only required fields and check equality to mask using XOR.
			//
			// If XOR result is not zero, result is not equal to expected, so some fields are missed.
			// Bits of fields which would be set are actually bits of missed fields.
			missed := bits.OnesCount8(result)
			for bitN := 0; bitN < missed; bitN++ {
				bitIdx := bits.TrailingZeros8(result)
				fieldIdx := i*8 + bitIdx
				var name string
				if fieldIdx < len(jsonFieldsNameOfPredictionResult) {
					name = jsonFieldsNameOfPredictionResult[fieldIdx]
				} else {
					name = strconv.Itoa(fieldIdx)
				}
				failures = append(failures, validate.FieldError{
					Name:  name,
					Error: validate.ErrFieldRequired,
				})
				// Reset bit.
				result &^= 1 << bitIdx
			}
		}
	}
	if len(failures) > 0 {
		return &validate.Error{Fields: failures}
	}

	return nil
}

// MarshalJSON implements stdjson.Marshaler.
func (s *PredictionResult) MarshalJSON() ([]byte, error) {
	e := jx.Encoder{}
	s.Encode(&e)
	return e.Bytes(), nil
}

// UnmarshalJSON implements stdjson.Unmarshaler.
func (s *PredictionResult) UnmarshalJSON(data []byte) error {
	d := jx.DecodeBytes(data)
	return s.Decode(d)
}

// Encode implements json.Marshaler.
func (s *PredictionResultMetadata) Encode(e *jx.Encoder) {
	e.ObjStart()
	s.encodeFields(e)
	e.ObjEnd()
}

// encodeFields encodes fields.
func (s *PredictionResultMetadata) encodeFields(e *jx.Encoder) {
	{
		e.FieldStart("complete_datetime")
		json.EncodeDateTime(e, s.CompleteDatetime)
	}
	{
		e.FieldStart("start_datetime")
		json.EncodeDateTime(e, s.StartDatetime)
	}
}

var jsonFieldsNameOfPredictionResultMetadata = [2]string{
	0: "complete_datetime",
	1: "start_datetime",
}

// Decode decodes PredictionResultMetadata from json.
func (s *PredictionResultMetadata) Decode(d *jx.Decoder) error {
	if s == nil {
		return errors.New("invalid: unable to decode PredictionResultMetadata to nil")
	}
	var requiredBitSet [1]uint8

	if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error {
		switch string(k) {
		case "complete_datetime":
			requiredBitSet[0] |= 1 << 0
			if err := func() error {
				v, err := json.DecodeDateTime(d)
				s.CompleteDatetime = v
				if err != nil {
					return err
				}
				return nil
			}(); err != nil {
				return errors.Wrap(err, "decode field \"complete_datetime\"")
			}
		case "start_datetime":
			requiredBitSet[0] |= 1 << 1
			if err := func() error {
				v, err := json.DecodeDateTime(d)
				s.StartDatetime = v
				if err != nil {
					return err
				}
				return nil
			}(); err != nil {
				return errors.Wrap(err, "decode field \"start_datetime\"")
			}
		default:
			return d.Skip()
		}
		return nil
	}); err != nil {
		return errors.Wrap(err, "decode PredictionResultMetadata")
	}
	// Validate required fields.
	var failures []validate.FieldError
	for i, mask := range [1]uint8{
		0b00000011,
	} {
		if result := (requiredBitSet[i] & mask) ^ mask; result != 0 {
			// Mask only required fields and check equality to mask using XOR.
			//
			// If XOR result is not zero, result is not equal to expected, so some fields are missed.
			// Bits of fields which would be set are actually bits of missed fields.
			missed := bits.OnesCount8(result)
			for bitN := 0; bitN < missed; bitN++ {
				bitIdx := bits.TrailingZeros8(result)
				fieldIdx := i*8 + bitIdx
				var name string
				if fieldIdx < len(jsonFieldsNameOfPredictionResultMetadata) {
					name = jsonFieldsNameOfPredictionResultMetadata[fieldIdx]
				} else {
					name = strconv.Itoa(fieldIdx)
				}
				failures = append(failures, validate.FieldError{
					Name:  name,
					Error: validate.ErrFieldRequired,
				})
				// Reset bit.
				result &^= 1 << bitIdx
			}
		}
	}
	if len(failures) > 0 {
		return &validate.Error{Fields: failures}
	}

	return nil
}

// MarshalJSON implements stdjson.Marshaler.
func (s *PredictionResultMetadata) MarshalJSON() ([]byte, error) {
	e := jx.Encoder{}
	s.Encode(&e)
	return e.Bytes(), nil
}

// UnmarshalJSON implements stdjson.Unmarshaler.
func (s *PredictionResultMetadata) UnmarshalJSON(data []byte) error {
	d := jx.DecodeBytes(data)
	return s.Decode(d)
}

// Encode implements json.Marshaler.
func (s *PredictionResultPredictionItem) Encode(e *jx.Encoder) {
	e.ObjStart()
	s.encodeFields(e)
	e.ObjEnd()
}

// encodeFields encodes fields.
func (s *PredictionResultPredictionItem) encodeFields(e *jx.Encoder) {
	{
		e.FieldStart("stage")
		s.Stage.Encode(e)
	}
	{
		e.FieldStart("trajectory")
		e.ArrStart()
		for _, elem := range s.Trajectory {
			elem.Encode(e)
		}
		e.ArrEnd()
	}
}

var jsonFieldsNameOfPredictionResultPredictionItem = [2]string{
	0: "stage",
	1: "trajectory",
}

// Decode decodes PredictionResultPredictionItem from json.
func (s *PredictionResultPredictionItem) Decode(d *jx.Decoder) error {
	if s == nil {
		return errors.New("invalid: unable to decode PredictionResultPredictionItem to nil")
	}
	var requiredBitSet [1]uint8

	if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error {
		switch string(k) {
		case "stage":
			requiredBitSet[0] |= 1 << 0
			if err := func() error {
				if err := s.Stage.Decode(d); err != nil {
					return err
				}
				return nil
			}(); err != nil {
				return errors.Wrap(err, "decode field \"stage\"")
			}
		case "trajectory":
			requiredBitSet[0] |= 1 << 1
			if err := func() error {
				s.Trajectory = make([]PredictionResultPredictionItemTrajectoryItem, 0)
				if err := d.Arr(func(d *jx.Decoder) error {
					var elem PredictionResultPredictionItemTrajectoryItem
					if err := elem.Decode(d); err != nil {
						return err
					}
					s.Trajectory = append(s.Trajectory, elem)
					return nil
				}); err != nil {
					return err
				}
				return nil
			}(); err != nil {
				return errors.Wrap(err, "decode field \"trajectory\"")
			}
		default:
			return d.Skip()
		}
		return nil
	}); err != nil {
		return errors.Wrap(err, "decode PredictionResultPredictionItem")
	}
	// Validate required fields.
	var failures []validate.FieldError
	for i, mask := range [1]uint8{
		0b00000011,
	} {
		if result := (requiredBitSet[i] & mask) ^ mask; result != 0 {
			// Mask only required fields and check equality to mask using XOR.
			//
			// If XOR result is not zero, result is not equal to expected, so some fields are missed.
			// Bits of fields which would be set are actually bits of missed fields.
			missed := bits.OnesCount8(result)
			for bitN := 0; bitN < missed; bitN++ {
				bitIdx := bits.TrailingZeros8(result)
				fieldIdx := i*8 + bitIdx
				var name string
				if fieldIdx < len(jsonFieldsNameOfPredictionResultPredictionItem) {
					name = jsonFieldsNameOfPredictionResultPredictionItem[fieldIdx]
				} else {
					name = strconv.Itoa(fieldIdx)
				}
				failures = append(failures, validate.FieldError{
					Name:  name,
					Error: validate.ErrFieldRequired,
				})
				// Reset bit.
				result &^= 1 << bitIdx
			}
		}
	}
	if len(failures) > 0 {
		return &validate.Error{Fields: failures}
	}

	return nil
}

// MarshalJSON implements stdjson.Marshaler.
func (s *PredictionResultPredictionItem) MarshalJSON() ([]byte, error) {
	e := jx.Encoder{}
	s.Encode(&e)
	return e.Bytes(), nil
}

// UnmarshalJSON implements stdjson.Unmarshaler.
func (s *PredictionResultPredictionItem) UnmarshalJSON(data []byte) error {
	d := jx.DecodeBytes(data)
	return s.Decode(d)
}

// Encode encodes PredictionResultPredictionItemStage as json.
func (s PredictionResultPredictionItemStage) Encode(e *jx.Encoder) {
	e.Str(string(s))
}

// Decode decodes PredictionResultPredictionItemStage from json.
func (s *PredictionResultPredictionItemStage) Decode(d *jx.Decoder) error {
	if s == nil {
		return errors.New("invalid: unable to decode PredictionResultPredictionItemStage to nil")
	}
	v, err := d.StrBytes()
	if err != nil {
		return err
	}
	// Try to use constant string.
	switch PredictionResultPredictionItemStage(v) {
	case PredictionResultPredictionItemStageAscent:
		*s = PredictionResultPredictionItemStageAscent
	case PredictionResultPredictionItemStageDescent:
		*s = PredictionResultPredictionItemStageDescent
	default:
		*s = PredictionResultPredictionItemStage(v)
	}

	return nil
}

// MarshalJSON implements stdjson.Marshaler.
func (s PredictionResultPredictionItemStage) MarshalJSON() ([]byte, error) {
	e := jx.Encoder{}
	s.Encode(&e)
	return e.Bytes(), nil
}

// UnmarshalJSON implements stdjson.Unmarshaler.
func (s *PredictionResultPredictionItemStage) UnmarshalJSON(data []byte) error {
	d := jx.DecodeBytes(data)
	return s.Decode(d)
}

// Encode implements json.Marshaler.
func (s *PredictionResultPredictionItemTrajectoryItem) Encode(e *jx.Encoder) {
	e.ObjStart()
	s.encodeFields(e)
	e.ObjEnd()
}

// encodeFields encodes fields.
func (s *PredictionResultPredictionItemTrajectoryItem) encodeFields(e *jx.Encoder) {
}

var jsonFieldsNameOfPredictionResultPredictionItemTrajectoryItem = [0]string{}

// Decode decodes PredictionResultPredictionItemTrajectoryItem from json.
func (s *PredictionResultPredictionItemTrajectoryItem) Decode(d *jx.Decoder) error {
	if s == nil {
		return errors.New("invalid: unable to decode PredictionResultPredictionItemTrajectoryItem to nil")
	}

	if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error {
		switch string(k) {
		default:
			return d.Skip()
		}
	}); err != nil {
		return errors.Wrap(err, "decode PredictionResultPredictionItemTrajectoryItem")
	}

	return nil
}

// MarshalJSON implements stdjson.Marshaler.
func (s *PredictionResultPredictionItemTrajectoryItem) MarshalJSON() ([]byte, error) {
	e := jx.Encoder{}
	s.Encode(&e)
	return e.Bytes(), nil
}

// UnmarshalJSON implements stdjson.Unmarshaler.
func (s *PredictionResultPredictionItemTrajectoryItem) UnmarshalJSON(data []byte) error {
	d := jx.DecodeBytes(data)
	return s.Decode(d)
}
