Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Generated code from megajson on MStatus struct
package gobike
import (
"errors"
"fmt"
"github.com/benbjohnson/megajson/scanner"
"io"
)
type MStatusJSONDecoder struct {
s scanner.Scanner
}
func NewMStatusJSONDecoder(r io.Reader) *MStatusJSONDecoder {
return &MStatusJSONDecoder{s: scanner.NewScanner(r)}
}
func NewMStatusJSONScanDecoder(s scanner.Scanner) *MStatusJSONDecoder {
return &MStatusJSONDecoder{s: s}
}
func (e *MStatusJSONDecoder) Decode(ptr **MStatus) error {
s := e.s
if tok, tokval, err := s.Scan(); err != nil {
return err
} else if tok == scanner.TNULL {
*ptr = nil
return nil
} else if tok != scanner.TLBRACE {
return fmt.Errorf("Unexpected %s at %d: %s; expected '{'", scanner.TokenName(tok), s.Pos(), string(tokval))
}
// Create the object if it doesn't exist.
if *ptr == nil {
*ptr = &MStatus{}
}
v := *ptr
// Loop over key/value pairs.
index := 0
for {
// Read in key.
var key string
tok, tokval, err := s.Scan()
if err != nil {
return err
} else if tok == scanner.TRBRACE {
return nil
} else if tok == scanner.TCOMMA {
if index == 0 {
return fmt.Errorf("Unexpected comma at %d", s.Pos())
}
if tok, tokval, err = s.Scan(); err != nil {
return err
}
}
if tok != scanner.TSTRING {
return fmt.Errorf("Unexpected %s at %d: %s; expected '{' or string", scanner.TokenName(tok), s.Pos(), string(tokval))
} else {
key = string(tokval)
}
// Read in the colon.
if tok, tokval, err := s.Scan(); err != nil {
return err
} else if tok != scanner.TCOLON {
return fmt.Errorf("Unexpected %s at %d: %s; expected colon", scanner.TokenName(tok), s.Pos(), string(tokval))
}
switch key {
case "HTTPStatus":
v := &v.HTTPStatus
if err := s.ReadInt(v); err != nil {
return err
}
case "TimeLocal":
v := &v.TimeLocal
if err := s.ReadString(v); err != nil {
return err
}
case "TimeUTC":
v := &v.TimeUTC
if err := s.ReadString(v); err != nil {
return err
}
case "URL":
v := &v.URL
if err := NewURL_infoJSONScanDecoder(s).Decode(v); err != nil {
return err
}
case "CacheTimeoutVal":
v := &v.CacheTimeoutVal
if err := s.ReadInt(v); err != nil {
return err
}
}
index++
}
return nil
}
func (e *MStatusJSONDecoder) DecodeArray(ptr *[]*MStatus) error {
s := e.s
if tok, _, err := s.Scan(); err != nil {
return err
} else if tok != scanner.TLBRACKET {
return errors.New("Expected '['")
}
slice := make([]*MStatus, 0)
// Loop over items.
index := 0
for {
tok, tokval, err := s.Scan()
if err != nil {
return err
} else if tok == scanner.TRBRACKET {
*ptr = slice
return nil
} else if tok == scanner.TCOMMA {
if index == 0 {
return fmt.Errorf("Unexpected comma in array at %d", s.Pos())
}
if tok, tokval, err = s.Scan(); err != nil {
return err
}
}
s.Unscan(tok, tokval)
item := &MStatus{}
if err := e.Decode(&item); err != nil {
return err
}
slice = append(slice, item)
index++
}
}
type URL_infoJSONDecoder struct {
s scanner.Scanner
}
func NewURL_infoJSONDecoder(r io.Reader) *URL_infoJSONDecoder {
return &URL_infoJSONDecoder{s: scanner.NewScanner(r)}
}
func NewURL_infoJSONScanDecoder(s scanner.Scanner) *URL_infoJSONDecoder {
return &URL_infoJSONDecoder{s: s}
}
func (e *URL_infoJSONDecoder) Decode(ptr **URL_info) error {
s := e.s
if tok, tokval, err := s.Scan(); err != nil {
return err
} else if tok == scanner.TNULL {
*ptr = nil
return nil
} else if tok != scanner.TLBRACE {
return fmt.Errorf("Unexpected %s at %d: %s; expected '{'", scanner.TokenName(tok), s.Pos(), string(tokval))
}
// Create the object if it doesn't exist.
if *ptr == nil {
*ptr = &URL_info{}
}
v := *ptr
// Loop over key/value pairs.
index := 0
for {
// Read in key.
var key string
tok, tokval, err := s.Scan()
if err != nil {
return err
} else if tok == scanner.TRBRACE {
return nil
} else if tok == scanner.TCOMMA {
if index == 0 {
return fmt.Errorf("Unexpected comma at %d", s.Pos())
}
if tok, tokval, err = s.Scan(); err != nil {
return err
}
}
if tok != scanner.TSTRING {
return fmt.Errorf("Unexpected %s at %d: %s; expected '{' or string", scanner.TokenName(tok), s.Pos(), string(tokval))
} else {
key = string(tokval)
}
// Read in the colon.
if tok, tokval, err := s.Scan(); err != nil {
return err
} else if tok != scanner.TCOLON {
return fmt.Errorf("Unexpected %s at %d: %s; expected colon", scanner.TokenName(tok), s.Pos(), string(tokval))
}
switch key {
case "Scheme":
v := &v.Scheme
if err := s.ReadString(v); err != nil {
return err
}
case "Opaque":
v := &v.Opaque
if err := s.ReadString(v); err != nil {
return err
}
case "Host":
v := &v.Host
if err := s.ReadString(v); err != nil {
return err
}
case "Path":
v := &v.Path
if err := s.ReadString(v); err != nil {
return err
}
case "RawQuery":
v := &v.RawQuery
if err := s.ReadString(v); err != nil {
return err
}
case "Fragment":
v := &v.Fragment
if err := s.ReadString(v); err != nil {
return err
}
}
index++
}
return nil
}
func (e *URL_infoJSONDecoder) DecodeArray(ptr *[]*URL_info) error {
s := e.s
if tok, _, err := s.Scan(); err != nil {
return err
} else if tok != scanner.TLBRACKET {
return errors.New("Expected '['")
}
slice := make([]*URL_info, 0)
// Loop over items.
index := 0
for {
tok, tokval, err := s.Scan()
if err != nil {
return err
} else if tok == scanner.TRBRACKET {
*ptr = slice
return nil
} else if tok == scanner.TCOMMA {
if index == 0 {
return fmt.Errorf("Unexpected comma in array at %d", s.Pos())
}
if tok, tokval, err = s.Scan(); err != nil {
return err
}
}
s.Unscan(tok, tokval)
item := &URL_info{}
if err := e.Decode(&item); err != nil {
return err
}
slice = append(slice, item)
index++
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.