/** * Search a node by path and return the value of the node. * * @param parent * @param path see getByPath * @param def * @return the value */ @SuppressWarnings("unchecked") public static <T> T getValue(JsonNode parent, String path, T def ) { Object out = getValue(parent, path); if (out == null) return def; return (T)MCast.toType(out, def.getClass(), def); }
@SuppressWarnings("unchecked") public static void appendToMap(Map<?, ?> p, String para, char keySeparator, char typeSeparator) { if (para == null) return; int pos = para.indexOf(keySeparator); if (pos < 0) return; String k = para.substring(0, pos); String v = para.substring(pos+1); String t = "text"; if (typeSeparator != 0) { pos = k.indexOf(typeSeparator); if (pos > 0) { t = k.substring(pos+1); k = k.substring(0, pos); } } Object obj = MCast.toType(v,t, null); if (obj != null) ((Map<Object,Object>)p).put(k, obj); }
private Object toObject(Object value, String type, ClassLoader cl) throws ClassNotFoundException, IOException { if (type != null && type.equals(NULL)) { return null; } if (value == null) return MCast.getDefaultPrimitive(type); if (type != null && type.equals(SERIALISED)) { return MCast.unserializeFromString(String.valueOf(value), cl); } Class<?> t = cl.loadClass(type); return MCast.toType(value, t, null); }
@SuppressWarnings("unchecked") @Override public void set(Object pojo, T value) throws IOException { if (!writable) throw new IOException("field is read only: " + name); try { pojo = PojoParser.checkParent(parent,pojo); if (!field.isAccessible()) field.setAccessible(true); value = (T) MCast.toType(value, getType(), null); if (getType().isPrimitive() && value == null) { // that's not possible value = (T)MCast.getDefaultPrimitive(getType()); } field.set(pojo, value); } catch (Exception e) { throw new IOException(field.getName(),e); } }
@SuppressWarnings("unchecked") @Override public void set(Object pojo, T value) throws IOException { pojo = PojoParser.checkParent(parent,pojo); if (setter == null) throw new IOException("Method is read only: " + getter.getName()); // if (!getType().isInstance(pojo)) // throw new IOException("Object is not instance of " + getType().getCanonicalName()); try { value = (T) MCast.toType(value, getType(), null); if (getType().isPrimitive() && value == null) { // that's not possible value = (T)MCast.getDefaultPrimitive(getType()); } setter.invoke(pojo, value); } catch (Exception e) { throw new IOException("Error set: " + name + "(" + getType() + ")=" + value, e); } }
return toType(value, t, def);