use of org.osgi.util.converter.ConversionException in project felix by apache.
the class YamlDeserializingImpl method from.
@Override
public T from(InputStream in, Charset charset) {
try {
byte[] bytes = Util.readStream(in);
String s = new String(bytes, charset);
return from(s);
} catch (IOException e) {
throw new ConversionException("Error reading inputstream", e);
}
}
use of org.osgi.util.converter.ConversionException in project felix by apache.
the class SchemaBasedConverter method convertToDTO.
@SuppressWarnings({ "rawtypes", "unchecked" })
private <U extends DTO> U convertToDTO(Class<U> targetCls, Map<?, ?> m, Schema schema, String contextPath) {
try {
U dto;
try {
dto = targetCls.newInstance();
} catch (Throwable t) {
throw new ConversionException("Cannot create instance of DTO " + targetCls + ". Bad constructor?", t);
}
for (Map.Entry entry : m.entrySet()) {
try {
Field f = targetCls.getField(entry.getKey().toString());
Object val = entry.getValue();
if (val == null)
continue;
String path = contextPath + f.getName();
Node node = schema.nodeAtPath(path);
Object obj;
if (node.typeReference().isPresent()) {
TypeReference<?> tr = Util.typeReferenceOf(node.typeReference().get());
if (node.isCollection())
if (!Collection.class.isAssignableFrom(val.getClass()))
// TODO: PANIC! Something is wrong... what should we do??
obj = null;
else
obj = convertToCollection((Class) Util.rawClassOf(tr), (Class) node.collectionType(), (Collection) val, schema, path);
else
obj = convertToDTO((Class<? extends DTO>) rawClassOf(tr), (Map<?, ?>) val, schema, path + "/");
} else {
if (node.isCollection()) {
Collection c = instantiateCollection(node.collectionType());
Type type = node.type();
for (Object o : (Collection) val) {
if (o == null)
c.add(null);
else if (asDTO(rawClassOf(type)))
c.add(convertToDTO((Class) Util.rawClassOf(type), (Map) o, schema, path + "/"));
else
c.add(converter.convert(o).to(type));
}
obj = c;
} else {
Class<?> rawClass = rawClassOf(node.type());
if (asDTO(rawClass))
obj = convertToDTO((Class<? extends DTO>) rawClass, (Map<?, ?>) val, schema, path + "/");
else
obj = converter.convert(val).to(node.type());
}
}
f.set(dto, obj);
} catch (NoSuchFieldException e) {
}
}
return dto;
} catch (Exception e) {
throw new ConversionException("Cannot create DTO " + targetCls, e);
}
}
use of org.osgi.util.converter.ConversionException in project felix by apache.
the class SchemaBasedConverter method convertToCollection.
@SuppressWarnings({ "rawtypes", "unchecked" })
private <U, V extends Collection<U>> V convertToCollection(Class<U> targetCls, Class<V> collectionClass, Collection sourceCollection, Schema schema, String path) {
try {
V targetCollection = instantiateCollection(collectionClass);
sourceCollection.stream().map(obj -> convertCollectionItemToObject(obj, targetCls, schema, path)).forEach(u -> targetCollection.add((U) u));
return targetCollection;
} catch (Exception e) {
throw new ConversionException("Cannot create DTO " + targetCls, e);
}
}
use of org.osgi.util.converter.ConversionException in project felix by apache.
the class JsonDeserializingImpl method from.
@Override
public T from(InputStream in, Charset charset) {
try {
byte[] bytes = Util.readStream(in);
String s = new String(bytes, charset);
return from(s);
} catch (IOException e) {
throw new ConversionException("Error reading inputstream", e);
}
}
Aggregations