Skip to content

Instantly share code, notes, and snippets.

@jordanorelli
Created October 26, 2012 15:45
Show Gist options
  • Save jordanorelli/3959521 to your computer and use it in GitHub Desktop.
Save jordanorelli/3959521 to your computer and use it in GitHub Desktop.
compute a field in json output
package main
import (
"encoding/json"
"os"
)
type Point struct {
X int `json:"x"`
Y int `json:"y"`
Z int `json:"z"`
}
func (p Point) MarshalJSON() ([]byte, error) {
type doc Point
d := doc(p)
d.Z = 3
return json.Marshal(d)
}
func main() {
p := &Point{X: 1, Y: 2}
if err := json.NewEncoder(os.Stdout).Encode(p); err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment