Create a gist now

Instantly share code, notes, and snippets.

@svett /education.go
Last active Sep 22, 2015

What would you like to do?
//go:generate ffjson $GOFILE
package education
type Student struct {
FirstName string
LastName string
}
type University struct {
Name string
Students []Student
}
// DO NOT EDIT!
// Code generated by ffjson <https://github.com/pquerna/ffjson>
// source: education.go
// DO NOT EDIT!
package education
import (
"bytes"
"fmt"
fflib "github.com/pquerna/ffjson/fflib/v1"
)
func (mj *Student) MarshalJSON() ([]byte, error) {
var buf fflib.Buffer
if mj == nil {
buf.WriteString("null")
return buf.Bytes(), nil
}
err := mj.MarshalJSONBuf(&buf)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func (mj *Student) MarshalJSONBuf(buf fflib.EncodingBuffer) error {
if mj == nil {
buf.WriteString("null")
return nil
}
var err error
var obj []byte
_ = obj
_ = err
buf.WriteString(`{"FirstName":`)
fflib.WriteJsonString(buf, string(mj.FirstName))
buf.WriteString(`,"LastName":`)
fflib.WriteJsonString(buf, string(mj.LastName))
buf.WriteByte('}')
return nil
}
const (
ffj_t_Studentbase = iota
ffj_t_Studentno_such_key
ffj_t_Student_FirstName
ffj_t_Student_LastName
)
var ffj_key_Student_FirstName = []byte("FirstName")
var ffj_key_Student_LastName = []byte("LastName")
func (uj *Student) UnmarshalJSON(input []byte) error {
fs := fflib.NewFFLexer(input)
return uj.UnmarshalJSONFFLexer(fs, fflib.FFParse_map_start)
}
func (uj *Student) UnmarshalJSONFFLexer(fs *fflib.FFLexer, state fflib.FFParseState) error {
var err error = nil
currentKey := ffj_t_Studentbase
_ = currentKey
tok := fflib.FFTok_init
wantedTok := fflib.FFTok_init
mainparse:
for {
tok = fs.Scan()
// println(fmt.Sprintf("debug: tok: %v state: %v", tok, state))
if tok == fflib.FFTok_error {
goto tokerror
}
switch state {
case fflib.FFParse_map_start:
if tok != fflib.FFTok_left_bracket {
wantedTok = fflib.FFTok_left_bracket
goto wrongtokenerror
}
state = fflib.FFParse_want_key
continue
case fflib.FFParse_after_value:
if tok == fflib.FFTok_comma {
state = fflib.FFParse_want_key
} else if tok == fflib.FFTok_right_bracket {
goto done
} else {
wantedTok = fflib.FFTok_comma
goto wrongtokenerror
}
case fflib.FFParse_want_key:
// json {} ended. goto exit. woo.
if tok == fflib.FFTok_right_bracket {
goto done
}
if tok != fflib.FFTok_string {
wantedTok = fflib.FFTok_string
goto wrongtokenerror
}
kn := fs.Output.Bytes()
if len(kn) <= 0 {
// "" case. hrm.
currentKey = ffj_t_Studentno_such_key
state = fflib.FFParse_want_colon
goto mainparse
} else {
switch kn[0] {
case 'F':
if bytes.Equal(ffj_key_Student_FirstName, kn) {
currentKey = ffj_t_Student_FirstName
state = fflib.FFParse_want_colon
goto mainparse
}
case 'L':
if bytes.Equal(ffj_key_Student_LastName, kn) {
currentKey = ffj_t_Student_LastName
state = fflib.FFParse_want_colon
goto mainparse
}
}
if fflib.EqualFoldRight(ffj_key_Student_LastName, kn) {
currentKey = ffj_t_Student_LastName
state = fflib.FFParse_want_colon
goto mainparse
}
if fflib.EqualFoldRight(ffj_key_Student_FirstName, kn) {
currentKey = ffj_t_Student_FirstName
state = fflib.FFParse_want_colon
goto mainparse
}
currentKey = ffj_t_Studentno_such_key
state = fflib.FFParse_want_colon
goto mainparse
}
case fflib.FFParse_want_colon:
if tok != fflib.FFTok_colon {
wantedTok = fflib.FFTok_colon
goto wrongtokenerror
}
state = fflib.FFParse_want_value
continue
case fflib.FFParse_want_value:
if tok == fflib.FFTok_left_brace || tok == fflib.FFTok_left_bracket || tok == fflib.FFTok_integer || tok == fflib.FFTok_double || tok == fflib.FFTok_string || tok == fflib.FFTok_bool || tok == fflib.FFTok_null {
switch currentKey {
case ffj_t_Student_FirstName:
goto handle_FirstName
case ffj_t_Student_LastName:
goto handle_LastName
case ffj_t_Studentno_such_key:
err = fs.SkipField(tok)
if err != nil {
return fs.WrapErr(err)
}
state = fflib.FFParse_after_value
goto mainparse
}
} else {
goto wantedvalue
}
}
}
handle_FirstName:
/* handler: uj.FirstName type=string kind=string quoted=false*/
{
{
if tok != fflib.FFTok_string && tok != fflib.FFTok_null {
return fs.WrapErr(fmt.Errorf("cannot unmarshal %s into Go value for string", tok))
}
}
if tok == fflib.FFTok_null {
} else {
outBuf := fs.Output.Bytes()
uj.FirstName = string(string(outBuf))
}
}
state = fflib.FFParse_after_value
goto mainparse
handle_LastName:
/* handler: uj.LastName type=string kind=string quoted=false*/
{
{
if tok != fflib.FFTok_string && tok != fflib.FFTok_null {
return fs.WrapErr(fmt.Errorf("cannot unmarshal %s into Go value for string", tok))
}
}
if tok == fflib.FFTok_null {
} else {
outBuf := fs.Output.Bytes()
uj.LastName = string(string(outBuf))
}
}
state = fflib.FFParse_after_value
goto mainparse
wantedvalue:
return fs.WrapErr(fmt.Errorf("wanted value token, but got token: %v", tok))
wrongtokenerror:
return fs.WrapErr(fmt.Errorf("ffjson: wanted token: %v, but got token: %v output=%s", wantedTok, tok, fs.Output.String()))
tokerror:
if fs.BigError != nil {
return fs.WrapErr(fs.BigError)
}
err = fs.Error.ToError()
if err != nil {
return fs.WrapErr(err)
}
panic("ffjson-generated: unreachable, please report bug.")
done:
return nil
}
func (mj *University) MarshalJSON() ([]byte, error) {
var buf fflib.Buffer
if mj == nil {
buf.WriteString("null")
return buf.Bytes(), nil
}
err := mj.MarshalJSONBuf(&buf)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func (mj *University) MarshalJSONBuf(buf fflib.EncodingBuffer) error {
if mj == nil {
buf.WriteString("null")
return nil
}
var err error
var obj []byte
_ = obj
_ = err
buf.WriteString(`{"Name":`)
fflib.WriteJsonString(buf, string(mj.Name))
buf.WriteString(`,"Students":`)
if mj.Students != nil {
buf.WriteString(`[`)
for i, v := range mj.Students {
if i != 0 {
buf.WriteString(`,`)
}
{
err = v.MarshalJSONBuf(buf)
if err != nil {
return err
}
}
}
buf.WriteString(`]`)
} else {
buf.WriteString(`null`)
}
buf.WriteByte('}')
return nil
}
const (
ffj_t_Universitybase = iota
ffj_t_Universityno_such_key
ffj_t_University_Name
ffj_t_University_Students
)
var ffj_key_University_Name = []byte("Name")
var ffj_key_University_Students = []byte("Students")
func (uj *University) UnmarshalJSON(input []byte) error {
fs := fflib.NewFFLexer(input)
return uj.UnmarshalJSONFFLexer(fs, fflib.FFParse_map_start)
}
func (uj *University) UnmarshalJSONFFLexer(fs *fflib.FFLexer, state fflib.FFParseState) error {
var err error = nil
currentKey := ffj_t_Universitybase
_ = currentKey
tok := fflib.FFTok_init
wantedTok := fflib.FFTok_init
mainparse:
for {
tok = fs.Scan()
// println(fmt.Sprintf("debug: tok: %v state: %v", tok, state))
if tok == fflib.FFTok_error {
goto tokerror
}
switch state {
case fflib.FFParse_map_start:
if tok != fflib.FFTok_left_bracket {
wantedTok = fflib.FFTok_left_bracket
goto wrongtokenerror
}
state = fflib.FFParse_want_key
continue
case fflib.FFParse_after_value:
if tok == fflib.FFTok_comma {
state = fflib.FFParse_want_key
} else if tok == fflib.FFTok_right_bracket {
goto done
} else {
wantedTok = fflib.FFTok_comma
goto wrongtokenerror
}
case fflib.FFParse_want_key:
// json {} ended. goto exit. woo.
if tok == fflib.FFTok_right_bracket {
goto done
}
if tok != fflib.FFTok_string {
wantedTok = fflib.FFTok_string
goto wrongtokenerror
}
kn := fs.Output.Bytes()
if len(kn) <= 0 {
// "" case. hrm.
currentKey = ffj_t_Universityno_such_key
state = fflib.FFParse_want_colon
goto mainparse
} else {
switch kn[0] {
case 'N':
if bytes.Equal(ffj_key_University_Name, kn) {
currentKey = ffj_t_University_Name
state = fflib.FFParse_want_colon
goto mainparse
}
case 'S':
if bytes.Equal(ffj_key_University_Students, kn) {
currentKey = ffj_t_University_Students
state = fflib.FFParse_want_colon
goto mainparse
}
}
if fflib.EqualFoldRight(ffj_key_University_Students, kn) {
currentKey = ffj_t_University_Students
state = fflib.FFParse_want_colon
goto mainparse
}
if fflib.SimpleLetterEqualFold(ffj_key_University_Name, kn) {
currentKey = ffj_t_University_Name
state = fflib.FFParse_want_colon
goto mainparse
}
currentKey = ffj_t_Universityno_such_key
state = fflib.FFParse_want_colon
goto mainparse
}
case fflib.FFParse_want_colon:
if tok != fflib.FFTok_colon {
wantedTok = fflib.FFTok_colon
goto wrongtokenerror
}
state = fflib.FFParse_want_value
continue
case fflib.FFParse_want_value:
if tok == fflib.FFTok_left_brace || tok == fflib.FFTok_left_bracket || tok == fflib.FFTok_integer || tok == fflib.FFTok_double || tok == fflib.FFTok_string || tok == fflib.FFTok_bool || tok == fflib.FFTok_null {
switch currentKey {
case ffj_t_University_Name:
goto handle_Name
case ffj_t_University_Students:
goto handle_Students
case ffj_t_Universityno_such_key:
err = fs.SkipField(tok)
if err != nil {
return fs.WrapErr(err)
}
state = fflib.FFParse_after_value
goto mainparse
}
} else {
goto wantedvalue
}
}
}
handle_Name:
/* handler: uj.Name type=string kind=string quoted=false*/
{
{
if tok != fflib.FFTok_string && tok != fflib.FFTok_null {
return fs.WrapErr(fmt.Errorf("cannot unmarshal %s into Go value for string", tok))
}
}
if tok == fflib.FFTok_null {
} else {
outBuf := fs.Output.Bytes()
uj.Name = string(string(outBuf))
}
}
state = fflib.FFParse_after_value
goto mainparse
handle_Students:
/* handler: uj.Students type=[]education.Student kind=slice quoted=false*/
{
{
if tok != fflib.FFTok_left_brace && tok != fflib.FFTok_null {
return fs.WrapErr(fmt.Errorf("cannot unmarshal %s into Go value for ", tok))
}
}
if tok == fflib.FFTok_null {
uj.Students = nil
} else {
uj.Students = make([]Student, 0)
wantVal := true
for {
var v Student
tok = fs.Scan()
if tok == fflib.FFTok_error {
goto tokerror
}
if tok == fflib.FFTok_right_brace {
break
}
if tok == fflib.FFTok_comma {
if wantVal == true {
// TODO(pquerna): this isn't an ideal error message, this handles
// things like [,,,] as an array value.
return fs.WrapErr(fmt.Errorf("wanted value token, but got token: %v", tok))
}
continue
} else {
wantVal = true
}
/* handler: v type=education.Student kind=struct quoted=false*/
{
if tok == fflib.FFTok_null {
state = fflib.FFParse_after_value
goto mainparse
}
err = v.UnmarshalJSONFFLexer(fs, fflib.FFParse_want_key)
if err != nil {
return err
}
state = fflib.FFParse_after_value
}
uj.Students = append(uj.Students, v)
wantVal = false
}
}
}
state = fflib.FFParse_after_value
goto mainparse
wantedvalue:
return fs.WrapErr(fmt.Errorf("wanted value token, but got token: %v", tok))
wrongtokenerror:
return fs.WrapErr(fmt.Errorf("ffjson: wanted token: %v, but got token: %v output=%s", wantedTok, tok, fs.Output.String()))
tokerror:
if fs.BigError != nil {
return fs.WrapErr(fs.BigError)
}
err = fs.Error.ToError()
if err != nil {
return fs.WrapErr(err)
}
panic("ffjson-generated: unreachable, please report bug.")
done:
return nil
}
package main
import (
"spike/education"
"github.com/pquerna/ffjson/ffjson"
)
func main() {
student := education.Student{
FirstName: "John",
LastName: "Smith",
}
university := education.University{
Name: "MIT",
Students: []education.Student{student},
}
json, err := ffjson.Marshal(&university)
if err != nil {
panic(err)
}
var universityCopy education.University
err = ffjson.Unmarshal(json, &universityCopy)
if err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment