1// Copyright (c) 2021, Peter Ohler, All rights reserved. 2 3package sen_test 4 5import ( 6 "fmt" 7 "strings" 8 9 "github.com/ohler55/ojg" 10 "github.com/ohler55/ojg/pretty" 11 "github.com/ohler55/ojg/sen" 12) 13 14func ExampleParse() { 15 val, err := sen.Parse([]byte("[true false [3 2 1] {a:1 b:2 c:3 d:[x y z []]}]")) 16 if err != nil { 17 panic(err) 18 } 19 fmt.Println(pretty.SEN(val, 80.3)) 20 21 // Output: 22 // [ 23 // true 24 // false 25 // [3 2 1] 26 // {a: 1 b: 2 c: 3 d: [x y z []]} 27 // ] 28} 29 30func ExampleMustParse() { 31 val := sen.MustParse([]byte("[true false [3 2 1] {a:1 b:2 c:3 d:[x y z []]}]")) 32 fmt.Println(pretty.SEN(val, 80.3)) 33 34 // Output: 35 // [ 36 // true 37 // false 38 // [3 2 1] 39 // {a: 1 b: 2 c: 3 d: [x y z []]} 40 // ] 41} 42 43func ExampleParseReader() { 44 r := strings.NewReader("[true false [3 2 1] {a:1 b:2 c:3 d:[x y z []]}]") 45 val, err := sen.ParseReader(r) 46 if err != nil { 47 panic(err) 48 } 49 fmt.Println(pretty.SEN(val, 80.3)) 50 51 // Output: 52 // [ 53 // true 54 // false 55 // [3 2 1] 56 // {a: 1 b: 2 c: 3 d: [x y z []]} 57 // ] 58} 59 60func ExampleMustParseReader() { 61 r := strings.NewReader("[true false [3 2 1] {a:1 b:2 c:3 d:[x y z []]}]") 62 val := sen.MustParseReader(r) 63 fmt.Println(pretty.SEN(val, 80.3)) 64 65 // Output: 66 // [ 67 // true 68 // false 69 // [3 2 1] 70 // {a: 1 b: 2 c: 3 d: [x y z []]} 71 // ] 72} 73 74func ExampleUnmarshal() { 75 type Sample struct { 76 X int 77 Y string 78 } 79 var sample Sample 80 if err := sen.Unmarshal([]byte("{x: 3 y: why}"), &sample); err != nil { 81 panic(err) 82 } 83 fmt.Printf("sample.X: %d sample.Y: %s\n", sample.X, sample.Y) 84 85 // Output: sample.X: 3 sample.Y: why 86} 87 88func ExampleString() { 89 type Sample struct { 90 X int 91 Y string 92 } 93 s := sen.String(&Sample{X: 3, Y: "why"}) 94 fmt.Println(s) 95 96 // Output: {x:3 y:why} 97} 98 99func ExampleBytes() { 100 type Sample struct { 101 X int 102 Y string 103 } 104 b := sen.Bytes(&Sample{X: 3, Y: "why"}) 105 fmt.Println(string(b)) 106 107 // Output: {x:3 y:why} 108} 109 110func ExampleWrite() { 111 type Sample struct { 112 X int 113 Y string 114 } 115 var buf strings.Builder 116 if err := sen.Write(&buf, &Sample{X: 3, Y: "why"}); err != nil { 117 panic(err) 118 } 119 fmt.Println(buf.String()) 120 121 // Output: {x:3 y:why} 122} 123 124func ExampleMustWrite() { 125 type Sample struct { 126 X int 127 Y string 128 } 129 var buf strings.Builder 130 sen.MustWrite(&buf, &Sample{X: 3, Y: "why"}) 131 fmt.Println(buf.String()) 132 133 // Output: {x:3 y:why} 134} 135 136func ExampleParser_Parse() { 137 p := sen.Parser{} 138 // An invalid JSON but valid SEN. 139 simple, err := p.Parse([]byte(`{abc: [{"x": {"y": [{b: true}]} z: 7}]}`)) 140 if err != nil { 141 panic(err) 142 } 143 fmt.Println(sen.String(simple, &ojg.Options{Sort: true})) 144 145 // Output: {abc:[{x:{y:[{b:true}]} z:7}]} 146} 147 148func ExampleParser_MustParse() { 149 p := sen.Parser{} 150 // An invalid JSON but valid SEN. 151 simple := p.MustParse([]byte(`{abc: [{"x": {"y": [{b: true}]} z: 7}]}`)) 152 fmt.Println(sen.String(simple, &ojg.Options{Sort: true})) 153 154 // Output: {abc:[{x:{y:[{b:true}]} z:7}]} 155} 156 157func ExampleParser_ParseReader() { 158 p := sen.Parser{} 159 // An invalid JSON but valid SEN. 160 r := strings.NewReader(`{abc: [{"x": {"y": [{b: true}]} z: 7}]}`) 161 simple, err := p.ParseReader(r) 162 if err != nil { 163 panic(err) 164 } 165 fmt.Println(sen.String(simple, &ojg.Options{Sort: true})) 166 167 // Output: {abc:[{x:{y:[{b:true}]} z:7}]} 168} 169 170func ExampleParser_MustParseReader() { 171 p := sen.Parser{} 172 // An invalid JSON but valid SEN. 173 r := strings.NewReader(`{abc: [{"x": {"y": [{b: true}]} z: 7}]}`) 174 simple := p.MustParseReader(r) 175 fmt.Println(sen.String(simple, &ojg.Options{Sort: true})) 176 177 // Output: {abc:[{x:{y:[{b:true}]} z:7}]} 178} 179 180func ExampleParser_Unmarshal() { 181 type Sample struct { 182 X int 183 Y string 184 } 185 p := sen.Parser{} 186 var sample Sample 187 if err := p.Unmarshal([]byte("{x: 3 y: why}"), &sample); err != nil { 188 panic(err) 189 } 190 fmt.Printf("sample.X: %d sample.Y: %s\n", sample.X, sample.Y) 191 192 // Output: sample.X: 3 sample.Y: why 193} 194 195func ExampleWriter_SEN() { 196 type Sample struct { 197 X int 198 Y string 199 } 200 wr := sen.Writer{} 201 s := wr.SEN(&Sample{X: 3, Y: "why"}) 202 fmt.Println(s) 203 204 // Output: {x:3 y:why} 205} 206 207func ExampleWriter_MustSEN() { 208 type Sample struct { 209 X int 210 Y string 211 } 212 wr := sen.Writer{} 213 b := wr.MustSEN(&Sample{X: 3, Y: "why"}) 214 fmt.Println(string(b)) 215 216 // Output: {x:3 y:why} 217} 218 219func ExampleWriter_Write() { 220 type Sample struct { 221 X int 222 Y string 223 } 224 wr := sen.Writer{} 225 var buf strings.Builder 226 if err := wr.Write(&buf, &Sample{X: 3, Y: "why"}); err != nil { 227 panic(err) 228 } 229 fmt.Println(buf.String()) 230 231 // Output: {x:3 y:why} 232} 233 234func ExampleWriter_MustWrite() { 235 type Sample struct { 236 X int 237 Y string 238 } 239 wr := sen.Writer{} 240 var buf strings.Builder 241 wr.MustWrite(&buf, &Sample{X: 3, Y: "why"}) 242 fmt.Println(buf.String()) 243 244 // Output: {x:3 y:why} 245} 246