Skip to content

Commit 0a8091c

Browse files
author
Rahul Kumar
committed
Added documentation
1 parent 61c1a88 commit 0a8091c

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,66 @@
11
package org.json;
2+
/*
3+
Copyright (c) 2002 JSON.org
24
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
The Software shall be used for Good, not Evil.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
SOFTWARE.
24+
*/
25+
26+
/**
27+
* Type conversion configuration interface to be used with xsi:type attributes.
28+
* <pre>
29+
* <h1>XML Sample</h1>
30+
* {@code
31+
* <root>
32+
* <asString xsi:type="string">12345</asString>
33+
* <asInt xsi:type="integer">54321</asInt>
34+
* </root>
35+
* }
36+
* <h1>JSON Output</h1>
37+
* {@code
38+
* {
39+
* "root" : {
40+
* "asString" : "12345",
41+
* "asInt": 54321
42+
* }
43+
* }
44+
* }
45+
*
46+
* <h1>Usage</h1>
47+
* {@code
48+
* Map<String, XMLXsiTypeConverter<?>> xsiTypeMap = new HashMap<String, XMLXsiTypeConverter<?>>();
49+
* xsiTypeMap.put("string", new XMLXsiTypeConverter<String>() {
50+
* @Override public String convert(final String value) {
51+
* return value;
52+
* }
53+
* });
54+
* xsiTypeMap.put("integer", new XMLXsiTypeConverter<Integer>() {
55+
* @Override public Integer convert(final String value) {
56+
* return Integer.valueOf(value);
57+
* }
58+
* });
59+
* }
60+
* </pre>
61+
* @author kumar529
62+
* @param <T>
63+
*/
364
public interface XMLXsiTypeConverter<T> {
465
T convert(String value);
566
}

src/test/java/org/json/junit/XMLTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,4 +1013,26 @@ public void testToJsonWithTypeWhenTypeConversionEnabled() {
10131013
Util.compareActualVsExpectedJsonObjects(actualJson,expectedJson);
10141014
}
10151015

1016+
@Test
1017+
public void testToJsonWithXSITypeWhenTypeConversionEnabled() {
1018+
String originalXml = "<root><asString xsi:type=\"string\">12345</asString><asInt "
1019+
+ "xsi:type=\"integer\">54321</asInt></root>";
1020+
String expectedJsonString = "{\"root\":{\"asString\":\"12345\",\"asInt\":54321}}";
1021+
JSONObject expectedJson = new JSONObject(expectedJsonString);
1022+
Map<String, XMLXsiTypeConverter<?>> xsiTypeMap = new HashMap<String, XMLXsiTypeConverter<?>>();
1023+
xsiTypeMap.put("string", new XMLXsiTypeConverter<String>() {
1024+
@Override public String convert(final String value) {
1025+
return value;
1026+
}
1027+
});
1028+
xsiTypeMap.put("integer", new XMLXsiTypeConverter<Integer>() {
1029+
@Override public Integer convert(final String value) {
1030+
return Integer.valueOf(value);
1031+
}
1032+
});
1033+
JSONObject actualJson = XML.toJSONObject(originalXml, new XMLParserConfiguration(false,
1034+
"content", false, xsiTypeMap));
1035+
Util.compareActualVsExpectedJsonObjects(actualJson,expectedJson);
1036+
}
1037+
10161038
}

0 commit comments

Comments
 (0)