Created
August 11, 2014 20:33
-
-
Save alimoeeny/54dce6d7c3c2adce6371 to your computer and use it in GitHub Desktop.
Empty things in json
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 Book struct { | |
Title string `json:"title,omitempty"` | |
Author string `json:"author,omitempty"` | |
ISBN string `json:"isbn,omitempty"` | |
} | |
type Library struct { | |
Name string `json:"name,omitempty"` | |
Books []Book `json:"books,omitempty"` | |
BestBook Book `json:"bestbook,omitempty"` | |
} | |
type StructWithString struct { | |
Name string `json:"name,omitempty"` | |
} | |
type StructWithStringPointer struct { | |
Name *string `json:"name,omitempty"` | |
} | |
type StructWithSlice struct { | |
Name []string `json:"name,omitempty"` | |
} | |
type InnerStruct struct { | |
Something string `json:"something,omitempty"` | |
} | |
type OuterStruct struct { | |
Field InnerStruct `json:"field,omitempty"` | |
} | |
type OuterStructWithPointer struct { | |
Field *InnerStruct `json:"field,omitempty"` | |
} | |
func main() { | |
book := Book{Title: "The hard thing about hard things", Author: "Ben Horowitz"} | |
jsonbook, err := json.Marshal(book) | |
if err != nil { | |
fmt.Printf("Error Marshaling book to json:%v\n", err) | |
} else { | |
fmt.Println(string(jsonbook)) | |
} | |
// ------------------------------------------------------ | |
library := Library{Name: "Ali's Library"} | |
jsonlib, err := json.Marshal(library) | |
if err != nil { | |
fmt.Printf("Error Marshaling library to json:%v\n", err) | |
} else { | |
fmt.Println(string(jsonlib)) | |
} | |
s := fmt.Sprintf("%s", string(jsonlib)) | |
var libback Library | |
err = json.Unmarshal([]byte(s), &libback) | |
if err != nil { | |
fmt.Printf("Error UnMarshalling library: %v\n", err) | |
} else { | |
fmt.Printf("Library Back is: %v\n", libback) | |
} | |
//// --------------------------------------- | |
stjson, err := json.Marshal(StructWithString{}) | |
if err != nil { | |
fmt.Printf("Error Marshaling to json:%v\n", err) | |
} else { | |
fmt.Println(string(stjson)) | |
} | |
stjson, err = json.Marshal(StructWithStringPointer{}) | |
if err != nil { | |
fmt.Printf("Error Marshaling to json:%v\n", err) | |
} else { | |
fmt.Println(string(stjson)) | |
} | |
stjson, err = json.Marshal(StructWithSlice{}) | |
if err != nil { | |
fmt.Printf("Error Marshaling to json:%v\n", err) | |
} else { | |
fmt.Println(string(stjson)) | |
} | |
stjson, err = json.Marshal(OuterStruct{}) | |
if err != nil { | |
fmt.Printf("Error Marshaling to json:%v\n", err) | |
} else { | |
fmt.Println(string(stjson)) | |
} | |
stjson, err = json.Marshal(OuterStruct{}) | |
if err != nil { | |
fmt.Printf("Error Marshaling to json:%v\n", err) | |
} else { | |
fmt.Println(string(stjson)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment