Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
JsonConverter |
|
| 1.0;1 |
1 | // Copyright 2008-2014 severally by the contributors | |
2 | // | |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 | // you may not use this file except in compliance with the License. | |
5 | // You may obtain a copy of the License at | |
6 | // | |
7 | // http://www.apache.org/licenses/LICENSE-2.0 | |
8 | // | |
9 | // Unless required by applicable law or agreed to in writing, software | |
10 | // distributed under the License is distributed on an "AS IS" BASIS, | |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 | // See the License for the specific language governing permissions and | |
13 | // limitations under the License. | |
14 | ||
15 | package net.sf.practicalxml.converter; | |
16 | ||
17 | import org.w3c.dom.Document; | |
18 | import org.w3c.dom.Element; | |
19 | ||
20 | import net.sf.practicalxml.converter.json.Json2XmlConverter; | |
21 | import net.sf.practicalxml.converter.json.Json2XmlOptions; | |
22 | import net.sf.practicalxml.converter.json.Xml2JsonConverter; | |
23 | import net.sf.practicalxml.converter.json.Xml2JsonOptions; | |
24 | ||
25 | ||
26 | /** | |
27 | * Converts between XML DOMs and JSON (Javascript Object Notation) strings. | |
28 | * See the {@link net.sf.practicalxml.converter.json package docs} for | |
29 | * details. | |
30 | * <p> | |
31 | * This class provides static facade methods for | |
32 | * {@link net.sf.practicalxml.converter.json.Json2XmlConverter} and | |
33 | * {@link net.sf.practicalxml.converter.json.Xml2JsonConverter}. If static | |
34 | * methods and throwaway objects offend you then use those classes directly. | |
35 | * | |
36 | * @since 1.1 | |
37 | */ | |
38 | 0 | public class JsonConverter |
39 | { | |
40 | /** | |
41 | * Creates a new DOM document from the passed JSON string, in which all | |
42 | * elements are members of the specified namespace and will inherit the | |
43 | * root's prefix (if any). | |
44 | * | |
45 | * @param json The source object. | |
46 | * @param nsUri The namespace of the root element. This will be | |
47 | * inherited by all child elements. | |
48 | * @param rootName The qualified name given to the root element of the | |
49 | * generated document. If a qualified name, all child | |
50 | * elements will inherit its prefix. | |
51 | * @param options Conversion options. | |
52 | */ | |
53 | public static Document convertToXml( | |
54 | String json, String nsUri, String rootName, Json2XmlOptions... options) | |
55 | { | |
56 | 0 | return new Json2XmlConverter(json, options).convert().getOwnerDocument(); |
57 | } | |
58 | ||
59 | ||
60 | /** | |
61 | * Creates a new DOM document from the passed bean, without namespace. | |
62 | * | |
63 | * @param json The source object. | |
64 | * @param rootName The name given to the root element of the produced | |
65 | * document. | |
66 | * @param options Conversion options. | |
67 | */ | |
68 | public static Document convertToXml( | |
69 | String json, String rootName, Json2XmlOptions... options) | |
70 | { | |
71 | 6 | return new Json2XmlConverter(json, options).convert().getOwnerDocument(); |
72 | } | |
73 | ||
74 | ||
75 | /** | |
76 | * Converts the passed JSON string to XML, appended as children of the | |
77 | * passed element. Descendents will inherit the parent's namespace and | |
78 | * prefix (if any). | |
79 | * | |
80 | * @param json The source object. | |
81 | * @param parent The parent element | |
82 | * @param options Conversion options. | |
83 | */ | |
84 | public static void convertToXml( | |
85 | String json, Element parent, Json2XmlOptions... options) | |
86 | { | |
87 | 1 | new Json2XmlConverter(json, options).convert(parent); |
88 | 1 | } |
89 | ||
90 | ||
91 | /** | |
92 | * Creates a new JSON string from the root of the passed <code>Document | |
93 | * </code>. | |
94 | * | |
95 | * @param dom The source document. | |
96 | * @param options Conversion options. | |
97 | */ | |
98 | public static String convertToJson(Document dom, Xml2JsonOptions... options) | |
99 | { | |
100 | 0 | return convertToJson(dom.getDocumentElement(), options); |
101 | } | |
102 | ||
103 | ||
104 | /** | |
105 | * Creates a new JSON string from the the passed <code>Element</code>. | |
106 | * This is useful when a DOM contains a tree of objects and you just | |
107 | * want to convert one of them. | |
108 | * | |
109 | * @param root The source element -- this may or may not be the | |
110 | * root element of its document. | |
111 | * @param options Conversion options. | |
112 | */ | |
113 | public static String convertToJson(Element root, Xml2JsonOptions... options) | |
114 | { | |
115 | 6 | return convertToJson(root, new StringBuilder(256), options).toString(); |
116 | } | |
117 | ||
118 | ||
119 | /** | |
120 | * Creates a new JSON string from the the passed <code>Element</code>, and | |
121 | * appends that string to the passed buffer (the buffer is actually passed | |
122 | * into the JSON construction code). | |
123 | * | |
124 | * @param root The source element -- this may or may not be the | |
125 | * root element of its document. | |
126 | * @param buf A buffer to which the JSON is appended. | |
127 | * @param options Conversion options. | |
128 | * | |
129 | * @return The buffer, as a convenience for chained calls. | |
130 | */ | |
131 | public static StringBuilder convertToJson( | |
132 | Element root, StringBuilder buf, Xml2JsonOptions... options) | |
133 | { | |
134 | 6 | return new Xml2JsonConverter(options).convert(root,buf); |
135 | } | |
136 | } |