forked from clbanning/mxj
-
Notifications
You must be signed in to change notification settings - Fork 1
/
j2x.go
144 lines (126 loc) · 3.74 KB
/
j2x.go
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
// Copyright 2012-2014 Charles Banning. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file
// j2x.go - wrappers for end-to-end transformatioin of JSON to XML
// For (mostly) backwards compatibility with j2x package.
package mxj
// Wrappers for end-to-end JSON to XML transformation.
/*
import "io"
// FromJson() --> ToXml().
func JsonToXml(jsonVal []byte) ([]byte, error) {
m, merr := NewMapJson(jsonVal)
if merr != nil {
return nil, merr
}
return m.Xml()
}
// FromJson() --> ToXmlWriter().
func JsonToXmlWriter(jsonVal []byte, xmlWriter io.Writer) (*[]byte, error) {
m, merr := NewMapJson(jsonVal)
if merr != nil {
return nil, merr
}
return m.XmlWriter(xmlWriter)
}
// FromJsonReader() --> ToXml().
func JsonReaderToXml(jsonReader io.Reader) ([]byte, *[]byte, error) {
m, raw, merr := NewMapJsonReader(jsonReader)
if merr != nil {
return nil, raw, merr
}
x, xerr := m.Xml()
return x, raw, xerr
}
// FromJsonReader() --> ToXmlWriter(). Handy for transforming bulk message sets.
func JsonReaderToXmlWriter(jsonReader io.Reader, xmlWriter io.Writer) (*[]byte, *[]byte, error) {
m, jraw, merr := NewMapJsonReader(jsonReader)
if merr != nil {
return jraw, nil, merr
}
xraw, xerr := m.XmlWriter(xmlWriter)
return jraw, xraw, xerr
}
*/
// JSON wrappers for Map methods implementing key path and value functions.
/*
// Wrap PathsForKey for JSON.
func JsonPathsForKey(jsonVal []byte, key string) ([]string, error) {
m, merr := NewMapJson(jsonVal)
if merr != nil {
return nil, merr
}
paths := m.PathsForKey(key)
return paths, nil
}
// Wrap PathForKeyShortest for JSON.
func JsonPathForKeyShortest(jsonVal []byte, key string) (string, error) {
m, merr := NewMapJson(jsonVal)
if merr != nil {
return "", merr
}
path := m.PathForKeyShortest(key)
return path, nil
}
// Wrap ValuesForKey for JSON.
func JsonValuesForKey(jsonVal []byte, key string, subkeys ...string) ([]interface{}, error) {
m, merr := NewMapJson(jsonVal)
if merr != nil {
return nil, merr
}
return m.ValuesForKey(key, subkeys...)
}
// Wrap ValuesForKeyPath for JSON.
func JsonValuesForKeyPath(jsonVal []byte, path string, subkeys ...string) ([]interface{}, error) {
m, merr := NewMapJson(jsonVal)
if merr != nil {
return nil, merr
}
return m.ValuesForPath(path, subkeys...)
}
// Wrap UpdateValuesForPath for JSON
// 'jsonVal' is XML value
// 'newKeyValue' is the value to replace an existing value at the end of 'path'
// 'path' is the dot-notation path with the key whose value is to be replaced at the end
// (can include wildcard character, '*')
// 'subkeys' are key:value pairs of key:values that must match for the key
func JsonUpdateValsForPath(jsonVal []byte, newKeyValue interface{}, path string, subkeys ...string) ([]byte, error) {
m, merr := NewMapJson(jsonVal)
if merr != nil {
return 0, err
}
n, err := m.UpdateValuesForPath(newJsonValue, path, subkeys...)
if err != nil {
return nil, err
}
return m.Json()
}
// Wrap NewMap for JSON and return as JSON
// 'jsonVal' is an JSON value
// 'keypairs' are "oldKey:newKey" values that conform to 'keypairs' in (Map)NewMap.
func JsonNewJson(jsonVal []byte, keypairs ...string) ([]byte, error) (
m, merr := NewMapJson(jsonVal)
if merr != nil {
return nil, merr
}
n, nerr := m.NewMap(keypairs...)
if nerr != nil {
return nil, nerr
}
return n.Json()
}
// Wrap NewMap for JSON and return as XML
// 'jsonVal' is an JSON value
// 'keypairs' are "oldKey:newKey" values that conform to 'keypairs' in (Map)NewMap.
func JsonNewXml(jsonVal []byte, keypairs ...string) ([]byte, error) (
m, merr := NewMapJson(jsonVal)
if merr != nil {
return nil, merr
}
n, nerr := m.NewMap(tagpairs...)
if nerr != nil {
return nil, nerr
}
return n.Xml()
}
*/