Skip to content

Instantly share code, notes, and snippets.

@bradclawsie
Created August 1, 2012 06:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bradclawsie/3224171 to your computer and use it in GitHub Desktop.
Save bradclawsie/3224171 to your computer and use it in GitHub Desktop.
make it TRUE
package main
import (
"fmt"
"encoding/json"
)
type T struct {
I int
E interface{}
}
type t T
func (x *T) UnmarshalJSON(b []byte) error {
var y t
// unmarshal to a shadow type to prevent an inifinite loop
err := json.Unmarshal(b,&y)
if err != nil {
return err
}
if y.E == nil {
x.E = true
} else {
x.E = y.E
}
x.I = y.I
return nil
}
func main() {
s := []byte(`{"I":5,"E":false}`)
var z T
err := json.Unmarshal(s,&z)
if err != nil {
panic(err.Error())
}
json,_ := json.Marshal(z)
fmt.Printf("%v\n",string(json))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment