aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/bennyscetbun/jsongo/README.md
blob: ebc81c637a575aabb1ed37a11942025726bba1f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
jsongo
======

**Jsongo is a simple library for golang to help you build Json without static struct or map[string]interface**

[json.Marshal](http://golang.org/pkg/encoding/json/#Marshal) and [json.Unmarshal](http://golang.org/pkg/encoding/json/#Unmarshal) have never been that easy

**If you had only one function to look at, look at the "[At](#at)" function**

***If you want an easy way to turn your json into a structure you should use the "[Print](#print)" function after unmarshalling json in a JSONNODE***

You can find the doc on godoc.org [![GoDoc](https://godoc.org/github.com/bennyscetbun/jsongo?status.png)](https://godoc.org/github.com/bennyscetbun/jsongo)


##JsonNode

JsonNode is the basic Structure that you must use when using jsongo. It can either be a:
- Map (jsongo.TypeMap)
- Array (jsongo.TypeArray)
- Value (jsongo.TypeValue) *Precisely a pointer store in an interface{}*
- Undefined (jsongo.TypeUndefined) *default type*

*When a JSONNode Type is set you cant change it without using Unset() first*
____
###Val
####Synopsis:
turn this JSONNode to TypeValue and set that value
```go
func (that *JSONNode) Val(val interface{}) 
```

####Examples
#####code:
```go
package main

import (
    "github.com/bennyscetbun/jsongo"
)

func main() {
    root := jsongo.JSONNode{}
	root.Val(42)
	root.DebugPrint("")
}
```
#####output:
```
42
```
#####code:
```go
package main

import (
    "github.com/bennyscetbun/jsongo"
)
type MyStruct struct {
	Member1 string
	Member2 int
}

func main() {
	root := jsongo.JSONNode{}
	root.Val(MyStruct{"The answer", 42})
	root.DebugPrint("")
}
```
#####output:
```
{
  "Member1": "The answer",
  "Member2": 42
}
```
_____
###Array
####Synopsis:
 Turn this JSONNode to a TypeArray and/or set the array size (reducing size will make you loose data)
```go
func (that *JSONNode) Array(size int) *[]JSONNode
```

####Examples
#####code:
```go
package main

import (
    "github.com/bennyscetbun/jsongo"
)

func main() {
    root := jsongo.JSONNode{}
	a := root.Array(4)
    for i := 0; i < 4; i++ {
        (*a)[i].Val(i)
    }
	root.DebugPrint("")
}
```
#####output:
```
[
  0,
  1,
  2,
  3
]
```
#####code:
```go
package main

import (
    "github.com/bennyscetbun/jsongo"
)

func main() {
    root := jsongo.JSONNode{}
    a := root.Array(4)
    for i := 0; i < 4; i++ {
        (*a)[i].Val(i)
    }
    root.Array(2) //Here we reduce the size and we loose some data
	root.DebugPrint("")
}
```
#####output:
```
[
  0,
  1
]
```
____
###Map
####Synopsis:
Turn this JSONNode to a TypeMap and/or Create a new element for key if necessary and return it
```go
func (that *JSONNode) Map(key string) *JSONNode
```

####Examples
#####code:
```go
package main

import (
    "github.com/bennyscetbun/jsongo"
)

func main() {
    root := jsongo.JSONNode{}
    root.Map("there").Val("you are")
    root.Map("here").Val("you should be")
	root.DebugPrint("")
}
```
#####output:
```
{
  "here": "you should be",
  "there": "you are"
}
```
____
###At
####Synopsis:
Helps you move through your node by building them on the fly

*val can be string or int only*

*strings are keys for TypeMap*

*ints are index in TypeArray (it will make array grow on the fly, so you should start to populate with the biggest index first)*
```go
func (that *JSONNode) At(val ...interface{}) *JSONNode
```

####Examples
#####code:
```go
package main

import (
    "github.com/bennyscetbun/jsongo"
)

func main() {
    root := jsongo.JSONNode{}
    root.At(4, "Who").Val("Let the dog out") //is equivalent to (*root.Array(5))[4].Map("Who").Val("Let the dog out")
    root.DebugPrint("")
}
```
#####output:
```
[
  null,
  null,
  null,
  null,
  {
    "Who": "Let the dog out"
  }
]
```
#####code:
```go
package main

import (
    "github.com/bennyscetbun/jsongo"
)

func main() {
    root := jsongo.JSONNode{}
    root.At(4, "Who").Val("Let the dog out")
    //to win some time you can even even save a certain JSONNode
	node := root.At(2, "What")
	node.At("Can", "You").Val("do with that?")
	node.At("Do", "You", "Think").Val("Of that")
    root.DebugPrint("")
}
```
#####output:
```
[
  null,
  null,
  {
    "What": {
      "Can": {
        "You": "do with that?"
      },
      "Do": {
        "You": {
          "Think": "Of that"
        }
      }
    }
  },
  null,
  {
    "Who": "Let the dog out"
  }
]
```
____
###Print
####Synopsis:
Helps you build your code by printing a go structure from the json you ve just unmarshaled

```go
func (that *JSONNode) Print()
```

____
###Other Function
There is plenty of other function, you should check the complete doc [![GoDoc](https://godoc.org/github.com/bennyscetbun/jsongo?status.png)](https://godoc.org/github.com/bennyscetbun/jsongo)

####A last Example for fun
#####code:
```go
package main

import (
    "github.com/bennyscetbun/jsongo"
)

func ShowOnlyValue(current *jsongo.JSONNode) {
    switch current.GetType() {
    	case jsongo.TypeValue:
			println(current.Get().(string))
		case jsongo.TypeMap:
			for _, key := range current.GetKeys() {
				ShowOnlyValue(current.At(key))
			}
		case jsongo.TypeArray:
			for _, key := range current.GetKeys() {
				ShowOnlyValue(current.At(key))
			}
	}
}

func main() {
    root := jsongo.JSONNode{}
    root.At(4, "Who").Val("Let the dog out")
	node := root.At(2, "What")
	node.At("Can", "You").Val("do with that?")
	node.At("Do", "You", "Think").Val("Of that")
	ShowOnlyValue(&root)
}
```
#####output:
```
Of that
do with that?
Let the dog out
```
_____
_____
##Json Marshal/Unmarshal

One of the main purpose of jsongo was to create Json from data without using static structure or map[string]interface.

You can use the full power of the [encoding/json](http://golang.org/pkg/encoding/json/) package with jsongo.

###Marshal
####Example
#####code:
```go
package main

import (
    "encoding/json"
	"fmt"
    "github.com/bennyscetbun/jsongo"
)

type Test struct {
	Static string `json:"static"`
	Over int `json:"over"`
}

func main() {
	root := jsongo.JSONNode{}
	root.At("A", "AA", "AAA").Val(42)

	node := root.At("A", "AB")
	node.At(1).Val("Peace")
	node.At(0).Val(Test{"struct suck when you build json", 9000})
	root.At("B").Val("Oh Yeah")

	tojson, err := json.MarshalIndent(&root, "", "  ")
	if err != nil {
		fmt.Printf("%s\n", err.Error())
		return
	}
	fmt.Printf("%s\n", tojson)
}
```
#####output:
```
{
  "A": {
    "AA": {
      "AAA": 42
    },
    "AB": [
      {
        "static": "struct suck when you build json",
        "over": 9000
      },
      "Peace"
    ]
  },
  "B": "Oh Yeah"
}
```
____
###Unmarshal
Unmarshal using JSONNode follow some simple rules:
- Any TypeUndefined JSONNode will be set to the right type, any other type wont be changed
- Array will grow if necessary
- New keys will be added to Map
- Values set to nil "*.Val(nil)*" will be turn into the type decide by Json
- It will respect any current mapping and will return errors if needed

You can set a node as "DontExpand" with the UnmarshalDontExpand function and thoose rules will apply:
- The type wont be change for any type
- Array wont grow
- New keys wont be added to Map
- Values set to nil "*.Val(nil)*" will be turn into the type decide by Json
- It will respect any current mapping and will return errors if needed

####Example of full expand
#####code:
```go
package main

import (
    "encoding/json"
    "github.com/bennyscetbun/jsongo"
    "fmt"
)

func main() {
    root := jsongo.JSONNode{}
    fromjson := `{
	  "A": {
		"AA": {
		  "AAA": 42
		},
		"AB": [
		  {
			"static": "struct suck when you build json",
			"over": 9000
		  },
		  "Peace"
		]
	  },
	  "B": "Oh Yeah"
	}`
    err := json.Unmarshal([]byte(fromjson), &root)
	if err != nil {
		fmt.Printf("%s\n", err.Error())
		return
	}
	root.DebugProspect(0, "\t")
}
```
#####output:
```
Is of Type: TypeMap
A:
        Is of Type: TypeMap
        AA:
                Is of Type: TypeMap
                AAA:
                        Is of Type: TypeValue
                        Value of type: float64
                        42
        AB:
                Is of Type: TypeArray
                [0]:
                        Is of Type: TypeMap
                        static:
                                Is of Type: TypeValue
                                Value of type: string
                                struct suck when you build json
                        over:
                                Is of Type: TypeValue
                                Value of type: float64
                                9000
                [1]:
                        Is of Type: TypeValue
                        Value of type: string
                        Peace
B:
        Is of Type: TypeValue
        Value of type: string
        Oh Yeah
```
####Example expand with mapping
#####code:
```go
package main

import (
    "encoding/json"
    "github.com/bennyscetbun/jsongo"
    "fmt"
)
type Test struct {
    Static string `json:"static"`
    Over int `json:"over"`
}

func main() {
	root := jsongo.JSONNode{}
    fromjson := `{
      "A": {
		"AA": {
		  "AAA": 42
		},
		"AB": [
		  {
			"static": "struct suck when you build json",
			"over": 9000
		  },
		  "Peace"
		]
	  },
	  "B": "Oh Yeah"
	}`
	root.At("A", "AB", 0).Val(Test{})
    err := json.Unmarshal([]byte(fromjson), &root)
	if err != nil {
		fmt.Printf("%s\n", err.Error())
		return
	}
	root.DebugProspect(0, "\t")
}
```
#####output:
```
Is of Type: TypeMap
A:
        Is of Type: TypeMap
        AB:
                Is of Type: TypeArray
                [0]:
                        Is of Type: TypeValue
                        Value of type: main.Test
                        {Static:struct suck when you build json Over:9000}
                [1]:
                        Is of Type: TypeValue
                        Value of type: string
                        Peace
        AA:
                Is of Type: TypeMap
                AAA:
                        Is of Type: TypeValue
                        Value of type: float64
                        42
B:
        Is of Type: TypeValue
        Value of type: string
        Oh Yeah
```
####Example expand with some UnmarshalDontExpand
#####code:
```go
package main

import (
    "encoding/json"
    "github.com/bennyscetbun/jsongo"
    "fmt"
)
type Test struct {
	Static string `json:"static"`
	Over int `json:"over"`
}

func main() {
    root := jsongo.JSONNode{}
    fromjson := `{
      "A": {
		"AA": {
		  "AAA": 42
		},
		"AB": [
		  {
			"static": "struct suck when you build json",
			"over": 9000
		  },
		  "Peace"
		]
	  },
	  "B": "Oh Yeah"
	}`
	root.At("A", "AB").UnmarshalDontExpand(true, false).At(0).Val(Test{})
    err := json.Unmarshal([]byte(fromjson), &root)
	if err != nil {
		fmt.Printf("%s\n", err.Error())
		return
	}
	root.DebugProspect(0, "\t")
}
```
#####output:
```
Is of Type: TypeMap
A:
        Is of Type: TypeMap
        AB:
                Is of Type: TypeArray
                [0]:
                        Is of Type: TypeValue
                        Value of type: main.Test
                        {Static:struct suck when you build json Over:9000}
        AA:
                Is of Type: TypeMap
                AAA:
                        Is of Type: TypeValue
                        Value of type: float64
                        42
B:
        Is of Type: TypeValue
        Value of type: string
        Oh Yeah
```