1package mxj 2 3import ( 4 "fmt" 5 "testing" 6) 7 8func TestLNHeader(t *testing.T) { 9 fmt.Println("\n---------------- leafnode_test.go ...") 10} 11 12func TestLeafNodes(t *testing.T) { 13 json1 := []byte(`{ 14 "friends": [ 15 { 16 "skills": [ 17 44, 12 18 ] 19 } 20 ] 21 }`) 22 23 json2 := []byte(`{ 24 "friends": 25 { 26 "skills": [ 27 44, 12 28 ] 29 } 30 31 }`) 32 33 m, _ := NewMapJson(json1) 34 ln := m.LeafNodes() 35 fmt.Println("\njson1-LeafNodes:") 36 for _, v := range ln { 37 fmt.Printf("%#v\n", v) 38 } 39 p := m.LeafPaths() 40 fmt.Println("\njson1-LeafPaths:") 41 for _, v := range p { 42 fmt.Printf("%#v\n", v) 43 } 44 45 m, _ = NewMapJson(json2) 46 ln = m.LeafNodes() 47 fmt.Println("\njson2-LeafNodes:") 48 for _, v := range ln { 49 fmt.Printf("%#v\n", v) 50 } 51 v := m.LeafValues() 52 fmt.Println("\njson1-LeafValues:") 53 for _, v := range v { 54 fmt.Printf("%#v\n", v) 55 } 56 57 json3 := []byte(`{ "a":"list", "of":["data", "of", 3, "types", true]}`) 58 m, _ = NewMapJson(json3) 59 ln = m.LeafNodes() 60 fmt.Println("\njson3-LeafNodes:") 61 for _, v := range ln { 62 fmt.Printf("%#v\n", v) 63 } 64 v = m.LeafValues() 65 fmt.Println("\njson3-LeafValues:") 66 for _, v := range v { 67 fmt.Printf("%#v\n", v) 68 } 69 p = m.LeafPaths() 70 fmt.Println("\njson3-LeafPaths:") 71 for _, v := range p { 72 fmt.Printf("%#v\n", v) 73 } 74 75 xmldata2 := []byte(` 76 <doc> 77 <item num="2" color="blue">Item 2 is blue</item> 78 <item num="3" color="green"> 79 <arm side="left" length="3.5"/> 80 <arm side="right" length="3.6"/> 81 </item> 82 </doc>`) 83 m, err := NewMapXml(xmldata2) 84 if err != nil { 85 t.Fatal(err.Error()) 86 } 87 fmt.Println("\nxml2data2-LeafValues:") 88 ln = m.LeafNodes() 89 for _, v := range ln { 90 fmt.Printf("%#v\n", v) 91 } 92 fmt.Println("\nxml2data2-LeafValues(NoAttributes):") 93 ln = m.LeafNodes(NoAttributes) 94 for _, v := range ln { 95 fmt.Printf("%#v\n", v) 96 } 97 98 // no-hyphen 99 PrependAttrWithHyphen(false) 100 m, err = NewMapXml(xmldata2) 101 if err != nil { 102 t.Fatal(err.Error()) 103 } 104 fmt.Println("\nno-hyphen-xml2data2-LeafValues:") 105 ln = m.LeafNodes() 106 for _, v := range ln { 107 fmt.Printf("%#v\n", v) 108 } 109 fmt.Println("\nno-hyphen-xml2data2-LeafValues(NoAttributes):") 110 ln = m.LeafNodes(NoAttributes) 111 for _, v := range ln { 112 fmt.Printf("%#v\n", v) 113 } 114 115 // restore default 116 PrependAttrWithHyphen(true) 117} 118 119func TestLeafDotNotation(t *testing.T) { 120 xmldata2 := []byte(` 121 <doc> 122 <item num="2" color="blue">Item 2 is blue</item> 123 <item num="3" color="green"> 124 <arm side="left" length="3.5"/> 125 <arm side="right" length="3.6"/> 126 </item> 127 </doc>`) 128 m, err := NewMapXml(xmldata2) 129 if err != nil { 130 t.Fatal(err.Error()) 131 } 132 fmt.Println("\nDotNotation-LeafValues:") 133 LeafUseDotNotation() 134 defer LeafUseDotNotation() 135 ln := m.LeafNodes() 136 for _, v := range ln { 137 fmt.Printf("%#v\n", v) 138 } 139 140} 141