4 **Jsongo is a simple library for golang to help you build Json without static struct or map[string]interface**
6 [json.Marshal](http://golang.org/pkg/encoding/json/#Marshal) and [json.Unmarshal](http://golang.org/pkg/encoding/json/#Unmarshal) have never been that easy
8 **If you had only one function to look at, look at the "[At](#at)" function**
10 ***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***
12 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)
17 JsonNode is the basic Structure that you must use when using jsongo. It can either be a:
18 - Map (jsongo.TypeMap)
19 - Array (jsongo.TypeArray)
20 - Value (jsongo.TypeValue) *Precisely a pointer store in an interface{}*
21 - Undefined (jsongo.TypeUndefined) *default type*
23 *When a JSONNode Type is set you cant change it without using Unset() first*
27 turn this JSONNode to TypeValue and set that value
29 func (that *JSONNode) Val(val interface{})
38 "github.com/bennyscetbun/jsongo"
42 root := jsongo.JSONNode{}
56 "github.com/bennyscetbun/jsongo"
58 type MyStruct struct {
64 root := jsongo.JSONNode{}
65 root.Val(MyStruct{"The answer", 42})
72 "Member1": "The answer",
79 Turn this JSONNode to a TypeArray and/or set the array size (reducing size will make you loose data)
81 func (that *JSONNode) Array(size int) *[]JSONNode
90 "github.com/bennyscetbun/jsongo"
94 root := jsongo.JSONNode{}
96 for i := 0; i < 4; i++ {
116 "github.com/bennyscetbun/jsongo"
120 root := jsongo.JSONNode{}
122 for i := 0; i < 4; i++ {
125 root.Array(2) //Here we reduce the size and we loose some data
139 Turn this JSONNode to a TypeMap and/or Create a new element for key if necessary and return it
141 func (that *JSONNode) Map(key string) *JSONNode
150 "github.com/bennyscetbun/jsongo"
154 root := jsongo.JSONNode{}
155 root.Map("there").Val("you are")
156 root.Map("here").Val("you should be")
163 "here": "you should be",
170 Helps you move through your node by building them on the fly
172 *val can be string or int only*
174 *strings are keys for TypeMap*
176 *ints are index in TypeArray (it will make array grow on the fly, so you should start to populate with the biggest index first)*
178 func (that *JSONNode) At(val ...interface{}) *JSONNode
187 "github.com/bennyscetbun/jsongo"
191 root := jsongo.JSONNode{}
192 root.At(4, "Who").Val("Let the dog out") //is equivalent to (*root.Array(5))[4].Map("Who").Val("Let the dog out")
204 "Who": "Let the dog out"
213 "github.com/bennyscetbun/jsongo"
217 root := jsongo.JSONNode{}
218 root.At(4, "Who").Val("Let the dog out")
219 //to win some time you can even even save a certain JSONNode
220 node := root.At(2, "What")
221 node.At("Can", "You").Val("do with that?")
222 node.At("Do", "You", "Think").Val("Of that")
234 "You": "do with that?"
245 "Who": "Let the dog out"
252 Helps you build your code by printing a go structure from the json you ve just unmarshaled
255 func (that *JSONNode) Print()
260 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)
262 ####A last Example for fun
268 "github.com/bennyscetbun/jsongo"
271 func ShowOnlyValue(current *jsongo.JSONNode) {
272 switch current.GetType() {
273 case jsongo.TypeValue:
274 println(current.Get().(string))
276 for _, key := range current.GetKeys() {
277 ShowOnlyValue(current.At(key))
279 case jsongo.TypeArray:
280 for _, key := range current.GetKeys() {
281 ShowOnlyValue(current.At(key))
287 root := jsongo.JSONNode{}
288 root.At(4, "Who").Val("Let the dog out")
289 node := root.At(2, "What")
290 node.At("Can", "You").Val("do with that?")
291 node.At("Do", "You", "Think").Val("Of that")
303 ##Json Marshal/Unmarshal
305 One of the main purpose of jsongo was to create Json from data without using static structure or map[string]interface.
307 You can use the full power of the [encoding/json](http://golang.org/pkg/encoding/json/) package with jsongo.
318 "github.com/bennyscetbun/jsongo"
322 Static string `json:"static"`
323 Over int `json:"over"`
327 root := jsongo.JSONNode{}
328 root.At("A", "AA", "AAA").Val(42)
330 node := root.At("A", "AB")
331 node.At(1).Val("Peace")
332 node.At(0).Val(Test{"struct suck when you build json", 9000})
333 root.At("B").Val("Oh Yeah")
335 tojson, err := json.MarshalIndent(&root, "", " ")
337 fmt.Printf("%s\n", err.Error())
340 fmt.Printf("%s\n", tojson)
352 "static": "struct suck when you build json",
363 Unmarshal using JSONNode follow some simple rules:
364 - Any TypeUndefined JSONNode will be set to the right type, any other type wont be changed
365 - Array will grow if necessary
366 - New keys will be added to Map
367 - Values set to nil "*.Val(nil)*" will be turn into the type decide by Json
368 - It will respect any current mapping and will return errors if needed
370 You can set a node as "DontExpand" with the UnmarshalDontExpand function and thoose rules will apply:
371 - The type wont be change for any type
373 - New keys wont be added to Map
374 - Values set to nil "*.Val(nil)*" will be turn into the type decide by Json
375 - It will respect any current mapping and will return errors if needed
377 ####Example of full expand
384 "github.com/bennyscetbun/jsongo"
389 root := jsongo.JSONNode{}
397 "static": "struct suck when you build json",
405 err := json.Unmarshal([]byte(fromjson), &root)
407 fmt.Printf("%s\n", err.Error())
410 root.DebugProspect(0, "\t")
421 Is of Type: TypeValue
422 Value of type: float64
425 Is of Type: TypeArray
429 Is of Type: TypeValue
430 Value of type: string
431 struct suck when you build json
433 Is of Type: TypeValue
434 Value of type: float64
437 Is of Type: TypeValue
438 Value of type: string
441 Is of Type: TypeValue
442 Value of type: string
445 ####Example expand with mapping
452 "github.com/bennyscetbun/jsongo"
456 Static string `json:"static"`
457 Over int `json:"over"`
461 root := jsongo.JSONNode{}
469 "static": "struct suck when you build json",
477 root.At("A", "AB", 0).Val(Test{})
478 err := json.Unmarshal([]byte(fromjson), &root)
480 fmt.Printf("%s\n", err.Error())
483 root.DebugProspect(0, "\t")
492 Is of Type: TypeArray
494 Is of Type: TypeValue
495 Value of type: main.Test
496 {Static:struct suck when you build json Over:9000}
498 Is of Type: TypeValue
499 Value of type: string
504 Is of Type: TypeValue
505 Value of type: float64
508 Is of Type: TypeValue
509 Value of type: string
512 ####Example expand with some UnmarshalDontExpand
519 "github.com/bennyscetbun/jsongo"
523 Static string `json:"static"`
524 Over int `json:"over"`
528 root := jsongo.JSONNode{}
536 "static": "struct suck when you build json",
544 root.At("A", "AB").UnmarshalDontExpand(true, false).At(0).Val(Test{})
545 err := json.Unmarshal([]byte(fromjson), &root)
547 fmt.Printf("%s\n", err.Error())
550 root.DebugProspect(0, "\t")
559 Is of Type: TypeArray
561 Is of Type: TypeValue
562 Value of type: main.Test
563 {Static:struct suck when you build json Over:9000}
567 Is of Type: TypeValue
568 Value of type: float64
571 Is of Type: TypeValue
572 Value of type: string