github.com/shogo82148/goa-v1@v1.6.2/encoding/json/encoding_test.go (about) 1 package json_test 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "fmt" 7 8 . "github.com/onsi/ginkgo" 9 . "github.com/onsi/gomega" 10 "github.com/shogo82148/goa-v1/uuid" 11 ) 12 13 var _ = Describe("JsonEncoding", func() { 14 15 Describe("handle goa/uuid/UUID", func() { 16 name := "Test" 17 id, _ := uuid.FromString("c0586f01-87b5-462b-a673-3b2dcf619091") 18 19 type Payload struct { 20 ID uuid.UUID 21 Name string 22 } 23 24 It("encode", func() { 25 data := Payload{ 26 id, 27 name, 28 } 29 30 var b bytes.Buffer 31 encoder := json.NewEncoder(&b) 32 encoder.Encode(data) 33 s := b.String() 34 35 Ω(s).Should(ContainSubstring(id.String())) 36 Ω(s).Should(ContainSubstring(name)) 37 }) 38 39 It("decode", func() { 40 encoded := fmt.Sprintf(`{"ID":"%s","Name":"%s"}`, id, name) 41 42 var payload Payload 43 var b bytes.Buffer 44 b.WriteString(encoded) 45 46 decoder := json.NewDecoder(&b) 47 decoder.Decode(&payload) 48 49 Ω(payload.ID.String()).Should(Equal(id.String())) 50 Ω(payload.Name).Should(Equal(name)) 51 }) 52 53 It("round trip", func() { 54 data := Payload{ 55 id, 56 name, 57 } 58 59 var payload Payload 60 var b bytes.Buffer 61 62 encoder := json.NewEncoder(&b) 63 encoder.Encode(data) 64 65 decoder := json.NewDecoder(&b) 66 decoder.Decode(&payload) 67 68 Ω(payload.ID.String()).Should(Equal(data.ID.String())) 69 Ω(payload.Name).Should(Equal(data.Name)) 70 }) 71 72 }) 73 })