-
-
Save icexin/c7bfaef1f8cb9a773158d4da1a1a30b5 to your computer and use it in GitHub Desktop.
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 ( | |
"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