mirror of
https://github.com/stleary/JSON-java.git
synced 2025-08-03 03:15:32 -04:00
Added documentation
This commit is contained in:
parent
61c1a882d6
commit
0a8091c954
@ -1,5 +1,66 @@
|
||||
package org.json;
|
||||
/*
|
||||
Copyright (c) 2002 JSON.org
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
The Software shall be used for Good, not Evil.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Type conversion configuration interface to be used with xsi:type attributes.
|
||||
* <pre>
|
||||
* <h1>XML Sample</h1>
|
||||
* {@code
|
||||
* <root>
|
||||
* <asString xsi:type="string">12345</asString>
|
||||
* <asInt xsi:type="integer">54321</asInt>
|
||||
* </root>
|
||||
* }
|
||||
* <h1>JSON Output</h1>
|
||||
* {@code
|
||||
* {
|
||||
* "root" : {
|
||||
* "asString" : "12345",
|
||||
* "asInt": 54321
|
||||
* }
|
||||
* }
|
||||
* }
|
||||
*
|
||||
* <h1>Usage</h1>
|
||||
* {@code
|
||||
* Map<String, XMLXsiTypeConverter<?>> xsiTypeMap = new HashMap<String, XMLXsiTypeConverter<?>>();
|
||||
* xsiTypeMap.put("string", new XMLXsiTypeConverter<String>() {
|
||||
* @Override public String convert(final String value) {
|
||||
* return value;
|
||||
* }
|
||||
* });
|
||||
* xsiTypeMap.put("integer", new XMLXsiTypeConverter<Integer>() {
|
||||
* @Override public Integer convert(final String value) {
|
||||
* return Integer.valueOf(value);
|
||||
* }
|
||||
* });
|
||||
* }
|
||||
* </pre>
|
||||
* @author kumar529
|
||||
* @param <T>
|
||||
*/
|
||||
public interface XMLXsiTypeConverter<T> {
|
||||
T convert(String value);
|
||||
}
|
||||
|
@ -1013,4 +1013,26 @@ public class XMLTest {
|
||||
Util.compareActualVsExpectedJsonObjects(actualJson,expectedJson);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToJsonWithXSITypeWhenTypeConversionEnabled() {
|
||||
String originalXml = "<root><asString xsi:type=\"string\">12345</asString><asInt "
|
||||
+ "xsi:type=\"integer\">54321</asInt></root>";
|
||||
String expectedJsonString = "{\"root\":{\"asString\":\"12345\",\"asInt\":54321}}";
|
||||
JSONObject expectedJson = new JSONObject(expectedJsonString);
|
||||
Map<String, XMLXsiTypeConverter<?>> xsiTypeMap = new HashMap<String, XMLXsiTypeConverter<?>>();
|
||||
xsiTypeMap.put("string", new XMLXsiTypeConverter<String>() {
|
||||
@Override public String convert(final String value) {
|
||||
return value;
|
||||
}
|
||||
});
|
||||
xsiTypeMap.put("integer", new XMLXsiTypeConverter<Integer>() {
|
||||
@Override public Integer convert(final String value) {
|
||||
return Integer.valueOf(value);
|
||||
}
|
||||
});
|
||||
JSONObject actualJson = XML.toJSONObject(originalXml, new XMLParserConfiguration(false,
|
||||
"content", false, xsiTypeMap));
|
||||
Util.compareActualVsExpectedJsonObjects(actualJson,expectedJson);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user