mirror of
https://github.com/stleary/JSON-java.git
synced 2025-08-02 11:05:28 -04:00
Added shallow copy for config map
This commit is contained in:
parent
ed9658d5cb
commit
56d4130a86
@ -24,6 +24,7 @@ SOFTWARE.
|
||||
*/
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
@ -278,7 +279,8 @@ public class XMLParserConfiguration {
|
||||
*/
|
||||
public XMLParserConfiguration withXsiTypeMap(final Map<String, XMLXsiTypeConverter<?>> xsiTypeMap) {
|
||||
XMLParserConfiguration newConfig = this.clone();
|
||||
newConfig.xsiTypeMap = Collections.unmodifiableMap(xsiTypeMap);
|
||||
Map<String, XMLXsiTypeConverter<?>> cloneXsiTypeMap = new HashMap<String, XMLXsiTypeConverter<?>>(xsiTypeMap);
|
||||
newConfig.xsiTypeMap = Collections.unmodifiableMap(cloneXsiTypeMap);
|
||||
return newConfig;
|
||||
}
|
||||
}
|
||||
|
@ -1033,4 +1033,39 @@ public class XMLTest {
|
||||
Util.compareActualVsExpectedJsonObjects(actualJson,expectedJson);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToJsonWithXSITypeWhenTypeConversionNotEnabledOnOne() {
|
||||
String originalXml = "<root><asString xsi:type=\"string\">12345</asString><asInt>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;
|
||||
}
|
||||
});
|
||||
JSONObject actualJson = XML.toJSONObject(originalXml, new XMLParserConfiguration().withXsiTypeMap(xsiTypeMap));
|
||||
Util.compareActualVsExpectedJsonObjects(actualJson,expectedJson);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testXSITypeMapNotModifiable() {
|
||||
Map<String, XMLXsiTypeConverter<?>> xsiTypeMap = new HashMap<String, XMLXsiTypeConverter<?>>();
|
||||
XMLParserConfiguration config = new XMLParserConfiguration().withXsiTypeMap(xsiTypeMap);
|
||||
xsiTypeMap.put("string", new XMLXsiTypeConverter<String>() {
|
||||
@Override public String convert(final String value) {
|
||||
return value;
|
||||
}
|
||||
});
|
||||
assertEquals("Config Conversion Map size is expected to be 0", 0, config.getXsiTypeMap().size());
|
||||
|
||||
try {
|
||||
config.getXsiTypeMap().put("boolean", new XMLXsiTypeConverter<Boolean>() {
|
||||
@Override public Boolean convert(final String value) {
|
||||
return Boolean.valueOf(value);
|
||||
}
|
||||
});
|
||||
fail("Expected to be unable to modify the config");
|
||||
} catch (Exception ignored) { }
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user