Update Bugsnag/DSL-JSON

pull/215/head
M66B 8 months ago
parent 3989504073
commit 46ff530412

@ -39,7 +39,7 @@ public abstract class BinaryConverter {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static ArrayList<byte[]> deserializeCollection(final JsonReader reader) throws IOException { public static ArrayList<byte[]> deserializeCollection(final JsonReader reader) throws IOException {
return reader.deserializeCollection(Base64Reader); return reader.deserializeCollectionCustom(Base64Reader);
} }
public static void deserializeCollection(final JsonReader reader, final Collection<byte[]> res) throws IOException { public static void deserializeCollection(final JsonReader reader, final Collection<byte[]> res) throws IOException {
@ -48,7 +48,7 @@ public abstract class BinaryConverter {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static ArrayList<byte[]> deserializeNullableCollection(final JsonReader reader) throws IOException { public static ArrayList<byte[]> deserializeNullableCollection(final JsonReader reader) throws IOException {
return reader.deserializeNullableCollection(Base64Reader); return reader.deserializeNullableCollectionCustom(Base64Reader);
} }
public static void deserializeNullableCollection(final JsonReader reader, final Collection<byte[]> res) throws IOException { public static void deserializeNullableCollection(final JsonReader reader, final Collection<byte[]> res) throws IOException {

@ -110,7 +110,7 @@ public abstract class BoolConverter {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static ArrayList<Boolean> deserializeCollection(final JsonReader reader) throws IOException { public static ArrayList<Boolean> deserializeCollection(final JsonReader reader) throws IOException {
return reader.deserializeCollection(READER); return reader.deserializeCollectionCustom(READER);
} }
public static void deserializeCollection(final JsonReader reader, final Collection<Boolean> res) throws IOException { public static void deserializeCollection(final JsonReader reader, final Collection<Boolean> res) throws IOException {
@ -119,7 +119,7 @@ public abstract class BoolConverter {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static ArrayList<Boolean> deserializeNullableCollection(final JsonReader reader) throws IOException { public static ArrayList<Boolean> deserializeNullableCollection(final JsonReader reader) throws IOException {
return reader.deserializeNullableCollection(READER); return reader.deserializeNullableCollectionCustom(READER);
} }
public static void deserializeNullableCollection(final JsonReader reader, final Collection<Boolean> res) throws IOException { public static void deserializeNullableCollection(final JsonReader reader, final Collection<Boolean> res) throws IOException {

@ -804,7 +804,7 @@ public class DslJson<TContext> implements UnknownSerializer, TypeLookup {
for (ClassLoader loader : loaders) { for (ClassLoader loader : loaders) {
try { try {
Class<?> external = loader.loadClass(name); Class<?> external = loader.loadClass(name);
Configuration instance = (Configuration) external.newInstance(); Configuration instance = (Configuration) external.getDeclaredConstructor().newInstance();
instance.configure(json); instance.configure(json);
} catch (NoClassDefFoundError ignore) { } catch (NoClassDefFoundError ignore) {
} catch (Exception ignore) { } catch (Exception ignore) {
@ -813,6 +813,8 @@ public class DslJson<TContext> implements UnknownSerializer, TypeLookup {
} }
static void registerJavaSpecifics(final DslJson json) { static void registerJavaSpecifics(final DslJson json) {
json.registerReader(Element.class, XmlConverter.Reader);
json.registerWriter(Element.class, XmlConverter.Writer);
} }
private final Map<Type, Object> defaults = new ConcurrentHashMap<Type, Object>(); private final Map<Type, Object> defaults = new ConcurrentHashMap<Type, Object>();
@ -1653,7 +1655,7 @@ public class DslJson<TContext> implements UnknownSerializer, TypeLookup {
} }
final JsonReader.ReadObject<?> contentReader = tryFindReader(content); final JsonReader.ReadObject<?> contentReader = tryFindReader(content);
if (contentReader != null) { if (contentReader != null) {
final ArrayList<?> result = json.deserializeNullableCollection(contentReader); final ArrayList<?> result = json.deserializeNullableCollectionCustom(contentReader);
if (container.isArray()) { if (container.isArray()) {
return returnAsArray(content, result); return returnAsArray(content, result);
} }
@ -1673,7 +1675,7 @@ public class DslJson<TContext> implements UnknownSerializer, TypeLookup {
} }
final JsonReader.ReadObject<?> contentReader = tryFindReader(content); final JsonReader.ReadObject<?> contentReader = tryFindReader(content);
if (contentReader != null) { if (contentReader != null) {
final ArrayList<?> result = json.deserializeNullableCollection(contentReader); final ArrayList<?> result = json.deserializeNullableCollectionCustom(contentReader);
return returnAsArray(content, result); return returnAsArray(content, result);
} }
} }
@ -1768,7 +1770,7 @@ public class DslJson<TContext> implements UnknownSerializer, TypeLookup {
} }
final JsonReader.ReadObject<?> simpleReader = tryFindReader(manifest); final JsonReader.ReadObject<?> simpleReader = tryFindReader(manifest);
if (simpleReader != null) { if (simpleReader != null) {
return json.deserializeNullableCollection(simpleReader); return json.deserializeNullableCollectionCustom(simpleReader);
} }
if (fallback != null) { if (fallback != null) {
final Object array = Array.newInstance(manifest, 0); final Object array = Array.newInstance(manifest, 0);
@ -1883,7 +1885,7 @@ public class DslJson<TContext> implements UnknownSerializer, TypeLookup {
} }
final JsonReader.ReadObject simpleReader = tryFindReader(manifest); final JsonReader.ReadObject simpleReader = tryFindReader(manifest);
if (simpleReader != null) { if (simpleReader != null) {
return json.deserializeNullableCollection(simpleReader); return json.deserializeNullableCollectionCustom(simpleReader);
} }
if (fallback != null) { if (fallback != null) {
final Object array = Array.newInstance(manifest, 0); final Object array = Array.newInstance(manifest, 0);
@ -2009,7 +2011,7 @@ public class DslJson<TContext> implements UnknownSerializer, TypeLookup {
} }
final JsonReader.ReadObject<?> simpleElementReader = tryFindReader(elementManifest); final JsonReader.ReadObject<?> simpleElementReader = tryFindReader(elementManifest);
if (simpleElementReader != null) { if (simpleElementReader != null) {
List<?> list = json.deserializeNullableCollection(simpleElementReader); List<?> list = json.deserializeNullableCollectionCustom(simpleElementReader);
return (TResult) convertResultToArray(elementManifest, list); return (TResult) convertResultToArray(elementManifest, list);
} }
} }
@ -2267,7 +2269,7 @@ public class DslJson<TContext> implements UnknownSerializer, TypeLookup {
} }
final JsonReader.ReadObject<?> simpleReader = tryFindReader(manifest); final JsonReader.ReadObject<?> simpleReader = tryFindReader(manifest);
if (simpleReader != null) { if (simpleReader != null) {
return json.iterateOver(simpleReader); return json.iterateOverCustom(simpleReader);
} }
if (fallback != null) { if (fallback != null) {
final Object array = Array.newInstance(manifest, 0); final Object array = Array.newInstance(manifest, 0);

@ -1,5 +1,6 @@
package com.bugsnag.android.repackaged.dslplatform.json; package com.bugsnag.android.repackaged.dslplatform.json;
import java.lang.reflect.InvocationTargetException;
import java.util.*; import java.util.*;
class ExternalConverterAnalyzer { class ExternalConverterAnalyzer {
@ -19,14 +20,16 @@ class ExternalConverterAnalyzer {
try { try {
Class<?> converterClass = cl.loadClass(ccn); Class<?> converterClass = cl.loadClass(ccn);
if (!Configuration.class.isAssignableFrom(converterClass)) continue; if (!Configuration.class.isAssignableFrom(converterClass)) continue;
Configuration converter = (Configuration) converterClass.newInstance(); Configuration converter = (Configuration) converterClass.getDeclaredConstructor().newInstance();
converter.configure(dslJson); converter.configure(dslJson);
return true; return true;
} catch (ClassNotFoundException ignored) { } catch (ClassNotFoundException ignored) {
} catch (IllegalAccessException ignored) { } catch (IllegalAccessException ignored) {
} catch (InstantiationException ignored) { } catch (InstantiationException ignored) {
} } catch (InvocationTargetException e) {
} } catch (NoSuchMethodException e) {
}
}
} }
return false; return false;
} }

@ -1594,12 +1594,13 @@ public final class JsonReader<TContext> {
return res.toArray(emptyArray); return res.toArray(emptyArray);
} }
public final <T, S extends T> ArrayList<T> deserializeCollection(final ReadObject<S> readObject) throws IOException { public final <T, S extends T> ArrayList<T> deserializeCollectionCustom(final ReadObject<S> readObject) throws IOException {
final ArrayList<T> res = new ArrayList<T>(4); final ArrayList<T> res = new ArrayList<T>(4);
deserializeCollection(readObject, res); deserializeCollection(readObject, res);
return res; return res;
} }
@SuppressWarnings("overloads")
public final <T, S extends T> void deserializeCollection(final ReadObject<S> readObject, final Collection<T> res) throws IOException { public final <T, S extends T> void deserializeCollection(final ReadObject<S> readObject, final Collection<T> res) throws IOException {
res.add(readObject.read(this)); res.add(readObject.read(this));
while (getNextToken() == ',') { while (getNextToken() == ',') {
@ -1609,12 +1610,13 @@ public final class JsonReader<TContext> {
checkArrayEnd(); checkArrayEnd();
} }
public final <T, S extends T> ArrayList<T> deserializeNullableCollection(final ReadObject<S> readObject) throws IOException { public final <T, S extends T> ArrayList<T> deserializeNullableCollectionCustom(final ReadObject<S> readObject) throws IOException {
final ArrayList<T> res = new ArrayList<T>(4); final ArrayList<T> res = new ArrayList<T>(4);
deserializeNullableCollection(readObject, res); deserializeNullableCollection(readObject, res);
return res; return res;
} }
@SuppressWarnings("overloads")
public final <T, S extends T> void deserializeNullableCollection(final ReadObject<S> readObject, final Collection<T> res) throws IOException { public final <T, S extends T> void deserializeNullableCollection(final ReadObject<S> readObject, final Collection<T> res) throws IOException {
if (wasNull()) { if (wasNull()) {
res.add(null); res.add(null);
@ -1638,6 +1640,7 @@ public final class JsonReader<TContext> {
return res; return res;
} }
@SuppressWarnings("overloads")
public final <T extends JsonObject> void deserializeCollection(final ReadJsonObject<T> readObject, final Collection<T> res) throws IOException { public final <T extends JsonObject> void deserializeCollection(final ReadJsonObject<T> readObject, final Collection<T> res) throws IOException {
if (last == '{') { if (last == '{') {
getNextToken(); getNextToken();
@ -1658,6 +1661,7 @@ public final class JsonReader<TContext> {
return res; return res;
} }
@SuppressWarnings("overloads")
public final <T extends JsonObject> void deserializeNullableCollection(final ReadJsonObject<T> readObject, final Collection<T> res) throws IOException { public final <T extends JsonObject> void deserializeNullableCollection(final ReadJsonObject<T> readObject, final Collection<T> res) throws IOException {
if (last == '{') { if (last == '{') {
getNextToken(); getNextToken();
@ -1676,7 +1680,7 @@ public final class JsonReader<TContext> {
checkArrayEnd(); checkArrayEnd();
} }
public final <T> Iterator<T> iterateOver(final JsonReader.ReadObject<T> reader) { public final <T> Iterator<T> iterateOverCustom(final JsonReader.ReadObject<T> reader) {
return new WithReader<T>(reader, this); return new WithReader<T>(reader, this);
} }

@ -70,7 +70,7 @@ public abstract class MapConverter {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static ArrayList<Map<String, String>> deserializeCollection(final JsonReader reader) throws IOException { public static ArrayList<Map<String, String>> deserializeCollection(final JsonReader reader) throws IOException {
return reader.deserializeCollection(TypedMapReader); return reader.deserializeCollectionCustom(TypedMapReader);
} }
public static void deserializeCollection(final JsonReader reader, final Collection<Map<String, String>> res) throws IOException { public static void deserializeCollection(final JsonReader reader, final Collection<Map<String, String>> res) throws IOException {
@ -79,7 +79,7 @@ public abstract class MapConverter {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static ArrayList<Map<String, String>> deserializeNullableCollection(final JsonReader reader) throws IOException { public static ArrayList<Map<String, String>> deserializeNullableCollection(final JsonReader reader) throws IOException {
return reader.deserializeNullableCollection(TypedMapReader); return reader.deserializeNullableCollectionCustom(TypedMapReader);
} }
public static void deserializeNullableCollection(final JsonReader reader, final Collection<Map<String, String>> res) throws IOException { public static void deserializeNullableCollection(final JsonReader reader, final Collection<Map<String, String>> res) throws IOException {

@ -56,7 +56,7 @@ public abstract class NetConverter {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static ArrayList<URI> deserializeUriCollection(final JsonReader reader) throws IOException { public static ArrayList<URI> deserializeUriCollection(final JsonReader reader) throws IOException {
return reader.deserializeCollection(UriReader); return reader.deserializeCollectionCustom(UriReader);
} }
public static void deserializeUriCollection(final JsonReader reader, final Collection<URI> res) throws IOException { public static void deserializeUriCollection(final JsonReader reader, final Collection<URI> res) throws IOException {
@ -65,7 +65,7 @@ public abstract class NetConverter {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static ArrayList<URI> deserializeUriNullableCollection(final JsonReader reader) throws IOException { public static ArrayList<URI> deserializeUriNullableCollection(final JsonReader reader) throws IOException {
return reader.deserializeNullableCollection(UriReader); return reader.deserializeNullableCollectionCustom(UriReader);
} }
public static void deserializeUriNullableCollection(final JsonReader reader, final Collection<URI> res) throws IOException { public static void deserializeUriNullableCollection(final JsonReader reader, final Collection<URI> res) throws IOException {
@ -92,7 +92,7 @@ public abstract class NetConverter {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static ArrayList<InetAddress> deserializeIpCollection(final JsonReader reader) throws IOException { public static ArrayList<InetAddress> deserializeIpCollection(final JsonReader reader) throws IOException {
return reader.deserializeCollection(AddressReader); return reader.deserializeCollectionCustom(AddressReader);
} }
public static void deserializeIpCollection(final JsonReader reader, final Collection<InetAddress> res) throws IOException { public static void deserializeIpCollection(final JsonReader reader, final Collection<InetAddress> res) throws IOException {
@ -101,7 +101,7 @@ public abstract class NetConverter {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static ArrayList<InetAddress> deserializeIpNullableCollection(final JsonReader reader) throws IOException { public static ArrayList<InetAddress> deserializeIpNullableCollection(final JsonReader reader) throws IOException {
return reader.deserializeNullableCollection(AddressReader); return reader.deserializeNullableCollectionCustom(AddressReader);
} }
public static void deserializeIpNullableCollection(final JsonReader reader, final Collection<InetAddress> res) throws IOException { public static void deserializeIpNullableCollection(final JsonReader reader, final Collection<InetAddress> res) throws IOException {

@ -570,7 +570,7 @@ public abstract class NumberConverter {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static ArrayList<Double> deserializeDoubleCollection(final JsonReader reader) throws IOException { public static ArrayList<Double> deserializeDoubleCollection(final JsonReader reader) throws IOException {
return reader.deserializeCollection(DOUBLE_READER); return reader.deserializeCollectionCustom(DOUBLE_READER);
} }
public static void deserializeDoubleCollection(final JsonReader reader, final Collection<Double> res) throws IOException { public static void deserializeDoubleCollection(final JsonReader reader, final Collection<Double> res) throws IOException {
@ -579,7 +579,7 @@ public abstract class NumberConverter {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static ArrayList<Double> deserializeDoubleNullableCollection(final JsonReader reader) throws IOException { public static ArrayList<Double> deserializeDoubleNullableCollection(final JsonReader reader) throws IOException {
return reader.deserializeNullableCollection(DOUBLE_READER); return reader.deserializeNullableCollectionCustom(DOUBLE_READER);
} }
public static void deserializeDoubleNullableCollection(final JsonReader reader, final Collection<Double> res) throws IOException { public static void deserializeDoubleNullableCollection(final JsonReader reader, final Collection<Double> res) throws IOException {
@ -772,7 +772,7 @@ public abstract class NumberConverter {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static ArrayList<Float> deserializeFloatCollection(final JsonReader reader) throws IOException { public static ArrayList<Float> deserializeFloatCollection(final JsonReader reader) throws IOException {
return reader.deserializeCollection(FLOAT_READER); return reader.deserializeCollectionCustom(FLOAT_READER);
} }
public static void deserializeFloatCollection(final JsonReader reader, Collection<Float> res) throws IOException { public static void deserializeFloatCollection(final JsonReader reader, Collection<Float> res) throws IOException {
@ -781,7 +781,7 @@ public abstract class NumberConverter {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static ArrayList<Float> deserializeFloatNullableCollection(final JsonReader reader) throws IOException { public static ArrayList<Float> deserializeFloatNullableCollection(final JsonReader reader) throws IOException {
return reader.deserializeNullableCollection(FLOAT_READER); return reader.deserializeNullableCollectionCustom(FLOAT_READER);
} }
public static void deserializeFloatNullableCollection(final JsonReader reader, final Collection<Float> res) throws IOException { public static void deserializeFloatNullableCollection(final JsonReader reader, final Collection<Float> res) throws IOException {
@ -981,7 +981,7 @@ public abstract class NumberConverter {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static ArrayList<Integer> deserializeIntCollection(final JsonReader reader) throws IOException { public static ArrayList<Integer> deserializeIntCollection(final JsonReader reader) throws IOException {
return reader.deserializeCollection(INT_READER); return reader.deserializeCollectionCustom(INT_READER);
} }
public static int[] deserializeIntArray(final JsonReader reader) throws IOException { public static int[] deserializeIntArray(final JsonReader reader) throws IOException {
@ -1080,7 +1080,7 @@ public abstract class NumberConverter {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static ArrayList<Short> deserializeShortNullableCollection(final JsonReader reader) throws IOException { public static ArrayList<Short> deserializeShortNullableCollection(final JsonReader reader) throws IOException {
return reader.deserializeNullableCollection(SHORT_READER); return reader.deserializeNullableCollectionCustom(SHORT_READER);
} }
public static void deserializeShortNullableCollection(final JsonReader reader, final Collection<Short> res) throws IOException { public static void deserializeShortNullableCollection(final JsonReader reader, final Collection<Short> res) throws IOException {
@ -1093,7 +1093,7 @@ public abstract class NumberConverter {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static ArrayList<Integer> deserializeIntNullableCollection(final JsonReader reader) throws IOException { public static ArrayList<Integer> deserializeIntNullableCollection(final JsonReader reader) throws IOException {
return reader.deserializeNullableCollection(INT_READER); return reader.deserializeNullableCollectionCustom(INT_READER);
} }
public static void deserializeIntNullableCollection(final JsonReader reader, final Collection<Integer> res) throws IOException { public static void deserializeIntNullableCollection(final JsonReader reader, final Collection<Integer> res) throws IOException {
@ -1317,7 +1317,7 @@ public abstract class NumberConverter {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static ArrayList<Long> deserializeLongCollection(final JsonReader reader) throws IOException { public static ArrayList<Long> deserializeLongCollection(final JsonReader reader) throws IOException {
return reader.deserializeCollection(LONG_READER); return reader.deserializeCollectionCustom(LONG_READER);
} }
public static void deserializeLongCollection(final JsonReader reader, final Collection<Long> res) throws IOException { public static void deserializeLongCollection(final JsonReader reader, final Collection<Long> res) throws IOException {
@ -1326,7 +1326,7 @@ public abstract class NumberConverter {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static ArrayList<Long> deserializeLongNullableCollection(final JsonReader reader) throws IOException { public static ArrayList<Long> deserializeLongNullableCollection(final JsonReader reader) throws IOException {
return reader.deserializeNullableCollection(LONG_READER); return reader.deserializeNullableCollectionCustom(LONG_READER);
} }
public static void deserializeLongNullableCollection(final JsonReader reader, final Collection<Long> res) throws IOException { public static void deserializeLongNullableCollection(final JsonReader reader, final Collection<Long> res) throws IOException {
@ -1682,7 +1682,7 @@ public abstract class NumberConverter {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static ArrayList<BigDecimal> deserializeDecimalCollection(final JsonReader reader) throws IOException { public static ArrayList<BigDecimal> deserializeDecimalCollection(final JsonReader reader) throws IOException {
return reader.deserializeCollection(DecimalReader); return reader.deserializeCollectionCustom(DecimalReader);
} }
public static void deserializeDecimalCollection(final JsonReader reader, final Collection<BigDecimal> res) throws IOException { public static void deserializeDecimalCollection(final JsonReader reader, final Collection<BigDecimal> res) throws IOException {
@ -1691,7 +1691,7 @@ public abstract class NumberConverter {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static ArrayList<BigDecimal> deserializeDecimalNullableCollection(final JsonReader reader) throws IOException { public static ArrayList<BigDecimal> deserializeDecimalNullableCollection(final JsonReader reader) throws IOException {
return reader.deserializeNullableCollection(DecimalReader); return reader.deserializeNullableCollectionCustom(DecimalReader);
} }
public static void deserializeDecimalNullableCollection(final JsonReader reader, final Collection<BigDecimal> res) throws IOException { public static void deserializeDecimalNullableCollection(final JsonReader reader, final Collection<BigDecimal> res) throws IOException {

@ -117,7 +117,7 @@ public abstract class ObjectConverter {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static ArrayList<Map<String, Object>> deserializeMapCollection(final JsonReader reader) throws IOException { public static ArrayList<Map<String, Object>> deserializeMapCollection(final JsonReader reader) throws IOException {
return reader.deserializeCollection(TypedMapReader); return reader.deserializeCollectionCustom(TypedMapReader);
} }
public static void deserializeMapCollection(final JsonReader reader, final Collection<Map<String, Object>> res) throws IOException { public static void deserializeMapCollection(final JsonReader reader, final Collection<Map<String, Object>> res) throws IOException {
@ -126,7 +126,7 @@ public abstract class ObjectConverter {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static ArrayList<Map<String, Object>> deserializeNullableMapCollection(final JsonReader reader) throws IOException { public static ArrayList<Map<String, Object>> deserializeNullableMapCollection(final JsonReader reader) throws IOException {
return reader.deserializeNullableCollection(TypedMapReader); return reader.deserializeNullableCollectionCustom(TypedMapReader);
} }
public static void deserializeNullableMapCollection(final JsonReader reader, final Collection<Map<String, Object>> res) throws IOException { public static void deserializeNullableMapCollection(final JsonReader reader, final Collection<Map<String, Object>> res) throws IOException {

@ -89,7 +89,7 @@ public abstract class StringConverter {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static ArrayList<String> deserializeCollection(final JsonReader reader) throws IOException { public static ArrayList<String> deserializeCollection(final JsonReader reader) throws IOException {
return reader.deserializeCollection(READER); return reader.deserializeCollectionCustom(READER);
} }
public static void deserializeCollection(final JsonReader reader, final Collection<String> res) throws IOException { public static void deserializeCollection(final JsonReader reader, final Collection<String> res) throws IOException {
@ -98,7 +98,7 @@ public abstract class StringConverter {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static ArrayList<String> deserializeNullableCollection(final JsonReader reader) throws IOException { public static ArrayList<String> deserializeNullableCollection(final JsonReader reader) throws IOException {
return reader.deserializeNullableCollection(READER); return reader.deserializeNullableCollectionCustom(READER);
} }
public static void deserializeNullableCollection(final JsonReader reader, final Collection<String> res) throws IOException { public static void deserializeNullableCollection(final JsonReader reader, final Collection<String> res) throws IOException {

@ -180,7 +180,7 @@ public abstract class UUIDConverter {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static ArrayList<UUID> deserializeCollection(final JsonReader reader) throws IOException { public static ArrayList<UUID> deserializeCollection(final JsonReader reader) throws IOException {
return reader.deserializeCollection(READER); return reader.deserializeCollectionCustom(READER);
} }
public static void deserializeCollection(final JsonReader reader, final Collection<UUID> res) throws IOException { public static void deserializeCollection(final JsonReader reader, final Collection<UUID> res) throws IOException {
@ -189,7 +189,7 @@ public abstract class UUIDConverter {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static ArrayList<UUID> deserializeNullableCollection(final JsonReader reader) throws IOException { public static ArrayList<UUID> deserializeNullableCollection(final JsonReader reader) throws IOException {
return reader.deserializeNullableCollection(READER); return reader.deserializeNullableCollectionCustom(READER);
} }
public static void deserializeNullableCollection(final JsonReader reader, final Collection<UUID> res) throws IOException { public static void deserializeNullableCollection(final JsonReader reader, final Collection<UUID> res) throws IOException {

@ -0,0 +1,217 @@
package com.bugsnag.android.repackaged.dslplatform.json;
import androidx.annotation.Nullable;
import org.w3c.dom.*;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSOutput;
import org.w3c.dom.ls.LSSerializer;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.*;
@SuppressWarnings({"rawtypes", "unchecked"}) // suppress pre-existing warnings
public abstract class XmlConverter {
static final JsonReader.ReadObject<Element> Reader = new JsonReader.ReadObject<Element>() {
@Nullable
@Override
public Element read(JsonReader reader) throws IOException {
return reader.wasNull() ? null : deserialize(reader);
}
};
static final JsonWriter.WriteObject<Element> Writer = new JsonWriter.WriteObject<Element>() {
@Override
public void write(JsonWriter writer, @Nullable Element value) {
serializeNullable(value, writer);
}
};
private static final DocumentBuilder documentBuilder;
static {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
try {
documentBuilder = dbFactory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
throw new RuntimeException(e);
}
}
public static void serializeNullable(@Nullable final Element value, final JsonWriter sw) {
if (value == null)
sw.writeNull();
else
serialize(value, sw);
}
public static void serialize(final Element value, final JsonWriter sw) {
Document document = value.getOwnerDocument();
DOMImplementationLS domImplLS = (DOMImplementationLS) document.getImplementation();
LSSerializer serializer = domImplLS.createLSSerializer();
LSOutput lsOutput = domImplLS.createLSOutput();
lsOutput.setEncoding("UTF-8");
StringWriter writer = new StringWriter();
lsOutput.setCharacterStream(writer);
serializer.write(document, lsOutput);
StringConverter.serialize(writer.toString(), sw);
}
public static Element deserialize(final JsonReader reader) throws IOException {
if (reader.last() == '"') {
try {
InputSource source = new InputSource(new StringReader(reader.readString()));
return documentBuilder.parse(source).getDocumentElement();
} catch (SAXException ex) {
throw reader.newParseErrorAt("Invalid XML value", 0, ex);
}
} else {
final Map<String, Object> map = ObjectConverter.deserializeMap(reader);
return mapToXml(map);
}
}
public static Element mapToXml(final Map<String, Object> map) throws IOException {
final Set<String> xmlRootElementNames = map.keySet();
if (xmlRootElementNames.size() > 1) {
throw ParsingException.create("Invalid XML. Expecting root element", true);
}
final String rootName = xmlRootElementNames.iterator().next();
final Document document = createDocument();
final Element rootElement = document.createElement(rootName);
document.appendChild(rootElement);
buildXmlFromHashMap(document, rootElement, map.get(rootName));
return rootElement;
}
private static synchronized Document createDocument() {
try {
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
final DocumentBuilder builder = factory.newDocumentBuilder();
return builder.newDocument();
} catch (ParserConfigurationException e) {
throw new ConfigurationException(e);
}
}
private static final String TEXT_NODE_TAG = "#text";
private static final String COMMENT_NODE_TAG = "#comment";
private static final String CDATA_NODE_TAG = "#cdata-section";
@SuppressWarnings("unchecked")
private static void buildXmlFromHashMap(
final Document doc,
final Element subtreeRootElement,
@Nullable final Object elementContent) {
if (elementContent instanceof HashMap) {
final HashMap<String, Object> elementContentMap = (HashMap<String, Object>) elementContent;
for (final Map.Entry<String, Object> childEntry : elementContentMap.entrySet()) {
final String key = childEntry.getKey();
if (key.startsWith("@")) {
subtreeRootElement.setAttribute(key.substring(1), childEntry.getValue().toString());
} else if (key.startsWith("#")) {
if (key.equals(TEXT_NODE_TAG)) {
if (childEntry.getValue() instanceof List) {
buildTextNodeList(doc, subtreeRootElement, (List<String>) childEntry.getValue());
} else {
final Node textNode = doc.createTextNode(childEntry.getValue().toString());
subtreeRootElement.appendChild(textNode);
}
} else if (key.equals(CDATA_NODE_TAG)) {
if (childEntry.getValue() instanceof List) {
buildCDataList(doc, subtreeRootElement, (List<String>) childEntry.getValue());
} else {
final Node cDataNode = doc.createCDATASection(childEntry.getValue().toString());
subtreeRootElement.appendChild(cDataNode);
}
} else if (key.equals(COMMENT_NODE_TAG)) {
if (childEntry.getValue() instanceof List) {
buildCommentList(doc, subtreeRootElement, (List<String>) childEntry.getValue());
} else {
final Node commentNode = doc.createComment(childEntry.getValue().toString());
subtreeRootElement.appendChild(commentNode);
}
} //else if (key.equals(WHITESPACE_NODE_TAG)
// || key.equals(SIGNIFICANT_WHITESPACE_NODE_TAG)) {
// Ignore
//} else {
/*
* All other nodes whose name starts with a '#' are invalid XML
* nodes, and thus ignored:
*/
//}
} else {
final Element newElement = doc.createElement(key);
subtreeRootElement.appendChild(newElement);
buildXmlFromHashMap(doc, newElement, childEntry.getValue());
}
}
} else if (elementContent instanceof List) {
buildXmlFromJsonArray(doc, subtreeRootElement, (List<Object>) elementContent);
} else {
if (elementContent != null) {
subtreeRootElement.setTextContent(elementContent.toString());
}
}
}
private static void buildTextNodeList(final Document doc, final Node subtreeRoot, final List<String> nodeValues) {
final StringBuilder sb = new StringBuilder();
for (final String nodeValue : nodeValues) {
sb.append(nodeValue);
}
subtreeRoot.appendChild(doc.createTextNode(sb.toString()));
}
private static void buildCDataList(final Document doc, final Node subtreeRoot, final List<String> nodeValues) {
for (final String nodeValue : nodeValues) {
subtreeRoot.appendChild(doc.createCDATASection(nodeValue));
}
}
private static void buildCommentList(final Document doc, final Node subtreeRoot, final List<String> nodeValues) {
for (final String nodeValue : nodeValues) {
subtreeRoot.appendChild(doc.createComment(nodeValue));
}
}
private static void buildXmlFromJsonArray(
final Document doc,
final Node listHeadNode,
final List<Object> elementContentList) {
final Node subtreeRootNode = listHeadNode.getParentNode();
/* The head node (already exists) */
buildXmlFromHashMap(doc, (Element) listHeadNode, elementContentList.get(0));
/* The rest of the list */
for (final Object elementContent : elementContentList.subList(1, elementContentList.size())) {
final Element newElement = doc.createElement(listHeadNode.getNodeName());
subtreeRootNode.appendChild(newElement);
buildXmlFromHashMap(doc, newElement, elementContent);
}
}
@SuppressWarnings("unchecked")
public static ArrayList<Element> deserializeCollection(final JsonReader reader) throws IOException {
return reader.deserializeCollectionCustom(Reader);
}
public static void deserializeCollection(final JsonReader reader, final Collection<Element> res) throws IOException {
reader.deserializeCollection(Reader, res);
}
@SuppressWarnings("unchecked")
public static ArrayList<Element> deserializeNullableCollection(final JsonReader reader) throws IOException {
return reader.deserializeNullableCollectionCustom(Reader);
}
public static void deserializeNullableCollection(final JsonReader reader, final Collection<Element> res) throws IOException {
reader.deserializeNullableCollection(Reader, res);
}
}
Loading…
Cancel
Save