Skip to content

Instantly share code, notes, and snippets.

@icexin
Created June 17, 2022 10:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save icexin/c7bfaef1f8cb9a773158d4da1a1a30b5 to your computer and use it in GitHub Desktop.
Save icexin/c7bfaef1f8cb9a773158d4da1a1a30b5 to your computer and use it in GitHub Desktop.
package main
import (
"encoding/json"
"fmt"
)
type BinaryInt int
func (b BinaryInt) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("\"%b\"", b)), nil
}
type Home struct {
Number int
District string
}
type Person struct {
Name string
Age int
Home Home
}
type MyHome struct {
*Home
Number BinaryInt
}
type MyPerson struct {
*Person
Age BinaryInt
Home MyHome
}
func Encode(p *Person) ([]byte, error) {
mypersion := MyPerson{
Person: p,
Age: BinaryInt(p.Age),
Home: MyHome{
Home: &p.Home,
Number: BinaryInt(p.Home.Number),
},
}
return json.Marshal(mypersion)
}
func main() {
p := Person{
Name: "John",
Age: 30,
Home: Home{
Number: 123,
District: "Sofia",
},
}
buf, _ := Encode(&p)
fmt.Println(string(buf))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment