Created
August 1, 2012 06:03
-
-
Save bradclawsie/3224171 to your computer and use it in GitHub Desktop.
make it TRUE
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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