// Custom JSON unmarshaler for the Beats struct that unmarshals times stored as (float) seconds.
Custom JSON unmarshaler for the Beats struct that unmarshals times stored as (float) seconds.
// Custom JSON unmarshaler for the Beats struct that unmarshals times stored as ( float ) seconds .
func (beats *Beats) UnmarshalJSON(bytes []byte) error {
	if beats != nil {
		type beat struct {
			At       instant   `json:"at"`
			Mean     instant   `json:"mean"`
			Variance instant   `json:"variance"`
			Taps     []instant `json:"taps"`
		}
		b := struct {
			BPM    uint    `json:"BPM"`
			Offset instant `json:"offset"`
			Beats  []beat  `json:"beats"`
		}{}
		if err := json.Unmarshal(bytes, &b); err != nil {
			return err
		}
		beats.BPM = b.BPM
		beats.Offset = time.Duration(b.Offset)
		beats.Beats = make([]Beat, len(b.Beats))
		for i, bb := range b.Beats {
			beats.Beats[i] = Beat{
				At:       time.Duration(bb.At),
				Mean:     time.Duration(bb.Mean),
				Variance: time.Duration(bb.Variance),
				Taps:     make([]time.Duration, len(bb.Taps)),
			}
			for j, tap := range bb.Taps {
				beats.Beats[i].Taps[j] = time.Duration(tap)
			}
		}
	}
	return nil
}
func ( beats * Beats ) UnmarshalJSON ( bytes [ ] byte ) error { if beats != nil { type beat struct { At instant `json:"at"` Mean instant `json:"mean"` Variance instant `json:"variance"` Taps [ ] instant `json:"taps"` } b := struct { BPM uint `json:"BPM"` Offset instant `json:"offset"` Beats [ ] beat `json:"beats"` } { } if err := json . Unmarshal ( bytes , & b ) ; err != nil { return err } beats . BPM = b . BPM beats . Offset = time . Duration ( b . Offset ) beats . Beats = make ( [ ] Beat , len ( b . Beats ) ) for i , bb := range b . Beats { beats . Beats [ i ] = Beat { At : time . Duration ( bb . At ) , Mean : time . Duration ( bb . Mean ) , Variance : time . Duration ( bb . Variance ) , Taps : make ( [ ] time . Duration , len ( bb . Taps ) ) , } for j , tap := range bb . Taps { beats . Beats [ i ] . Taps [ j ] = time . Duration ( tap ) } } } return nil }
Custom JSON unmarshaler for the Beats struct that unmarshals times stored as (float) seconds.
Custom JSON unmarshaler for the Beats struct that unmarshals times stored as ( float ) seconds .