gojay_test.go (8056B)
1 package gojay 2 3 type testObject struct { 4 testStr string 5 testStrNull *string 6 testInt int 7 testIntNull *int 8 testInt64 int64 9 testInt64Null *int64 10 testInt32 int32 11 testInt32Null *int32 12 testInt16 int16 13 testInt16Null *int16 14 testInt8 int8 15 testInt8Null *int8 16 testUint64 uint64 17 testUint64Null *uint64 18 testUint32 uint32 19 testUint32Null *uint32 20 testUint16 uint16 21 testUint16Null *uint16 22 testUint8 uint8 23 testUint8Null *uint8 24 testFloat64 float64 25 testFloat64Null *float64 26 testFloat32 float32 27 testFloat32Null *float32 28 testBool bool 29 testBoolNull *bool 30 testSubObject *testObject 31 testSubArray testSliceInts 32 testInterface interface{} 33 } 34 35 // make sure it implements interfaces 36 var _ MarshalerJSONObject = &testObject{} 37 var _ UnmarshalerJSONObject = &testObject{} 38 39 func (t *testObject) IsNil() bool { 40 return t == nil 41 } 42 43 func (t *testObject) MarshalJSONObject(enc *Encoder) { 44 enc.AddStringKey("testStr", t.testStr) 45 enc.AddIntKey("testInt", t.testInt) 46 enc.AddIntKey("testInt64", int(t.testInt64)) 47 enc.AddIntKey("testInt32", int(t.testInt32)) 48 enc.AddIntKey("testInt16", int(t.testInt16)) 49 enc.AddIntKey("testInt8", int(t.testInt8)) 50 enc.AddIntKey("testUint64", int(t.testUint64)) 51 enc.AddIntKey("testUint32", int(t.testUint32)) 52 enc.AddIntKey("testUint16", int(t.testUint16)) 53 enc.AddIntKey("testUint8", int(t.testUint8)) 54 enc.AddFloatKey("testFloat64", t.testFloat64) 55 enc.AddFloat32Key("testFloat32", t.testFloat32) 56 enc.AddBoolKey("testBool", t.testBool) 57 } 58 59 func (t *testObject) UnmarshalJSONObject(dec *Decoder, k string) error { 60 switch k { 61 case "testStr": 62 return dec.AddString(&t.testStr) 63 case "testStrNull": 64 return dec.AddStringNull(&t.testStrNull) 65 case "testInt": 66 return dec.AddInt(&t.testInt) 67 case "testIntNull": 68 return dec.AddIntNull(&t.testIntNull) 69 case "testInt64": 70 return dec.AddInt64(&t.testInt64) 71 case "testInt64Null": 72 return dec.AddInt64Null(&t.testInt64Null) 73 case "testInt32": 74 return dec.AddInt32(&t.testInt32) 75 case "testInt32Null": 76 return dec.AddInt32Null(&t.testInt32Null) 77 case "testInt16": 78 return dec.AddInt16(&t.testInt16) 79 case "testInt16Null": 80 return dec.AddInt16Null(&t.testInt16Null) 81 case "testInt8": 82 return dec.AddInt8(&t.testInt8) 83 case "testInt8Null": 84 return dec.AddInt8Null(&t.testInt8Null) 85 case "testUint64": 86 return dec.AddUint64(&t.testUint64) 87 case "testUint64Null": 88 return dec.AddUint64Null(&t.testUint64Null) 89 case "testUint32": 90 return dec.AddUint32(&t.testUint32) 91 case "testUint32Null": 92 return dec.AddUint32Null(&t.testUint32Null) 93 case "testUint16": 94 return dec.AddUint16(&t.testUint16) 95 case "testUint16Null": 96 return dec.AddUint16Null(&t.testUint16Null) 97 case "testUint8": 98 return dec.AddUint8(&t.testUint8) 99 case "testUint8Null": 100 return dec.AddUint8Null(&t.testUint8Null) 101 case "testFloat64": 102 return dec.AddFloat(&t.testFloat64) 103 case "testFloat64Null": 104 return dec.AddFloatNull(&t.testFloat64Null) 105 case "testFloat32": 106 return dec.AddFloat32(&t.testFloat32) 107 case "testFloat32Null": 108 return dec.AddFloat32Null(&t.testFloat32Null) 109 case "testBool": 110 return dec.AddBool(&t.testBool) 111 case "testBoolNull": 112 return dec.AddBoolNull(&t.testBoolNull) 113 case "testInterface": 114 return dec.AddInterface(&t.testInterface) 115 } 116 return nil 117 } 118 119 func (t *testObject) NKeys() int { 120 return 29 121 } 122 123 type testObject0Keys struct { 124 testStr string 125 testInt int 126 testInt64 int64 127 testInt32 int32 128 testInt16 int16 129 testInt8 int8 130 testUint64 uint64 131 testUint32 uint32 132 testUint16 uint16 133 testUint8 uint8 134 testFloat64 float64 135 testFloat32 float32 136 testBool bool 137 testSubObject *testObject0Keys 138 testSubArray testSliceInts 139 testInterface interface{} 140 } 141 142 // make sure it implements interfaces 143 var _ MarshalerJSONObject = &testObject0Keys{} 144 var _ UnmarshalerJSONObject = &testObject0Keys{} 145 146 func (t *testObject0Keys) IsNil() bool { 147 return t == nil 148 } 149 150 func (t *testObject0Keys) MarshalJSONObject(enc *Encoder) { 151 enc.AddStringKey("testStr", t.testStr) 152 enc.AddIntKey("testInt", t.testInt) 153 enc.AddIntKey("testInt64", int(t.testInt64)) 154 enc.AddIntKey("testInt32", int(t.testInt32)) 155 enc.AddIntKey("testInt16", int(t.testInt16)) 156 enc.AddIntKey("testInt8", int(t.testInt8)) 157 enc.AddIntKey("testUint64", int(t.testUint64)) 158 enc.AddIntKey("testUint32", int(t.testUint32)) 159 enc.AddIntKey("testUint16", int(t.testUint16)) 160 enc.AddIntKey("testUint8", int(t.testUint8)) 161 enc.AddFloatKey("testFloat64", t.testFloat64) 162 enc.AddFloat32Key("testFloat32", t.testFloat32) 163 enc.AddBoolKey("testBool", t.testBool) 164 enc.AddInterfaceKey("testInterface", t.testInterface) 165 } 166 167 func (t *testObject0Keys) UnmarshalJSONObject(dec *Decoder, k string) error { 168 switch k { 169 case "testStr": 170 return dec.AddString(&t.testStr) 171 case "testInt": 172 return dec.AddInt(&t.testInt) 173 case "testInt64": 174 return dec.AddInt64(&t.testInt64) 175 case "testInt32": 176 return dec.AddInt32(&t.testInt32) 177 case "testInt16": 178 return dec.AddInt16(&t.testInt16) 179 case "testInt8": 180 return dec.AddInt8(&t.testInt8) 181 case "testUint64": 182 return dec.AddUint64(&t.testUint64) 183 case "testUint32": 184 return dec.AddUint32(&t.testUint32) 185 case "testUint16": 186 return dec.AddUint16(&t.testUint16) 187 case "testUint8": 188 return dec.AddUint8(&t.testUint8) 189 case "testFloat64": 190 return dec.AddFloat(&t.testFloat64) 191 case "testFloat32": 192 return dec.AddFloat32(&t.testFloat32) 193 case "testBool": 194 return dec.AddBool(&t.testBool) 195 case "testInterface": 196 return dec.AddInterface(&t.testInterface) 197 } 198 return nil 199 } 200 201 func (t *testObject0Keys) NKeys() int { 202 return 0 203 } 204 205 type testObjectComplex struct { 206 testSubObject *testObject 207 testSubSliceInts *testSliceInts 208 testStr string 209 testSubObject2 *testObjectComplex 210 } 211 212 func (t *testObjectComplex) IsNil() bool { 213 return t == nil 214 } 215 216 func (t *testObjectComplex) MarshalJSONObject(enc *Encoder) { 217 enc.AddObjectKey("testSubObject", t.testSubObject) 218 enc.AddStringKey("testStr", t.testStr) 219 enc.AddObjectKey("testStr", t.testSubObject2) 220 } 221 222 func (t *testObjectComplex) UnmarshalJSONObject(dec *Decoder, k string) error { 223 switch k { 224 case "testSubObject": 225 return dec.AddObject(t.testSubObject) 226 case "testSubSliceInts": 227 return dec.AddArray(t.testSubSliceInts) 228 case "testStr": 229 return dec.AddString(&t.testStr) 230 case "testSubObject2": 231 return dec.AddObject(t.testSubObject2) 232 } 233 return nil 234 } 235 236 func (t *testObjectComplex) NKeys() int { 237 return 4 238 } 239 240 // make sure it implements interfaces 241 var _ MarshalerJSONObject = &testObjectComplex{} 242 var _ UnmarshalerJSONObject = &testObjectComplex{} 243 244 type TestObj struct { 245 test int 246 test2 int 247 test3 string 248 test4 string 249 test5 float64 250 testArr testSliceObjects 251 testSubObj *TestSubObj 252 testSubObj2 *TestSubObj 253 } 254 255 type TestSubObj struct { 256 test3 int 257 test4 int 258 test5 string 259 testSubSubObj *TestSubObj 260 testSubSubObj2 *TestSubObj 261 } 262 263 func (t *TestSubObj) UnmarshalJSONObject(dec *Decoder, key string) error { 264 switch key { 265 case "test": 266 return dec.AddInt(&t.test3) 267 case "test2": 268 return dec.AddInt(&t.test4) 269 case "test3": 270 return dec.AddString(&t.test5) 271 case "testSubSubObj": 272 t.testSubSubObj = &TestSubObj{} 273 return dec.AddObject(t.testSubSubObj) 274 case "testSubSubObj2": 275 t.testSubSubObj2 = &TestSubObj{} 276 return dec.AddObject(t.testSubSubObj2) 277 } 278 return nil 279 } 280 281 func (t *TestSubObj) NKeys() int { 282 return 0 283 } 284 285 func (t *TestObj) UnmarshalJSONObject(dec *Decoder, key string) error { 286 switch key { 287 case "test": 288 return dec.AddInt(&t.test) 289 case "test2": 290 return dec.AddInt(&t.test2) 291 case "test3": 292 return dec.AddString(&t.test3) 293 case "test4": 294 return dec.AddString(&t.test4) 295 case "test5": 296 return dec.AddFloat(&t.test5) 297 case "testSubObj": 298 t.testSubObj = &TestSubObj{} 299 return dec.AddObject(t.testSubObj) 300 case "testSubObj2": 301 t.testSubObj2 = &TestSubObj{} 302 return dec.AddObject(t.testSubObj2) 303 case "testArr": 304 return dec.AddArray(&t.testArr) 305 } 306 return nil 307 } 308 309 func (t *TestObj) NKeys() int { 310 return 8 311 }