/** * Convert from a JSON or other string representation to real object. Used when preparing operation * argument. If the JSON structure cannot be converted, an {@link IllegalArgumentException} is thrown. * * @param type type to convert to * @param json string to deserialize * @return the deserialized object */ Object fromJson(String type, String json) { return converters.getToObjectConverter().convertFromString(type,json); }
private Object convertListToArray(Class pType, List pList) { Class valueType = pType.getComponentType(); Object ret = Array.newInstance(valueType, pList.size()); int i = 0; for (Object value : pList) { if (value == null) { if (!valueType.isPrimitive()) { Array.set(ret,i++,null); } else { throw new IllegalArgumentException("Cannot use a null value in an array of type " + valueType.getSimpleName()); } } else { if (valueType.isAssignableFrom(value.getClass())) { // Can be set directly Array.set(ret,i++,value); } else { // Try to convert from string Array.set(ret,i++,convertFromString(valueType.getCanonicalName(), value.toString())); } } } return ret; }
private Object convertToArray(String pType, String pValue) { // It's an array String t = pType.substring(1,2); Class valueType; if (t.equals("L")) { // It's an object-type String oType = pType.substring(2,pType.length()-1).replace('/','.'); valueType = ClassUtil.classForName(oType); if (valueType == null) { throw new IllegalArgumentException("No class of type " + oType + "found"); } } else { valueType = TYPE_SIGNATURE_MAP.get(t); if (valueType == null) { throw new IllegalArgumentException("Cannot convert to unknown array type " + t); } } String[] values = EscapeUtil.splitAsArray(pValue, EscapeUtil.PATH_ESCAPE, ","); Object ret = Array.newInstance(valueType,values.length); int i = 0; for (String value : values) { Array.set(ret,i++,value.equals("[null]") ? null : convertFromString(valueType.getCanonicalName(),value)); } return ret; }
/** * Prepare a value from a either a given object or its string representation. * If the value is already assignable to the given class name it is returned directly. * * @param pExpectedClassName type name of the expected type * @param pValue value to either take directly or to convert from its string representation. * @return the prepared / converted object */ public Object prepareValue(String pExpectedClassName, Object pValue) { if (pValue == null) { return null; } else { Class expectedClass = ClassUtil.classForName(pExpectedClassName); Object param = null; if (expectedClass != null) { param = prepareValue(expectedClass,pValue); } if (param == null) { // Ok, we try to convert it from a string // If expectedClass is null, it is probably a native type, so we // let happen the string conversion // later on (e.g. conversion of pArgument.toString()) which will throw // an exception at this point if conversion can not be done return convertFromString(pExpectedClassName, pValue.toString()); } return param; } }
/** * Convert from a JSON or other string representation to real object. Used when preparing operation * argument. If the JSON structure cannot be converted, an {@link IllegalArgumentException} is thrown. * * @param type type to convert to * @param json string to deserialize * @return the deserialized object */ Object fromJson(String type, String json) { return converters.getToObjectConverter().convertFromString(type,json); }
private Object convertListToArray(Class pType, List pList) { Class valueType = pType.getComponentType(); Object ret = Array.newInstance(valueType, pList.size()); int i = 0; for (Object value : pList) { if (value == null) { if (!valueType.isPrimitive()) { Array.set(ret,i++,null); } else { throw new IllegalArgumentException("Cannot use a null value in an array of type " + valueType.getSimpleName()); } } else { if (valueType.isAssignableFrom(value.getClass())) { // Can be set directly Array.set(ret,i++,value); } else { // Try to convert from string Array.set(ret,i++,convertFromString(valueType.getCanonicalName(), value.toString())); } } } return ret; }
private Object convertListToArray(Class pType, List pList) { Class valueType = pType.getComponentType(); Object ret = Array.newInstance(valueType, pList.size()); int i = 0; for (Object value : pList) { if (value == null) { if (!valueType.isPrimitive()) { Array.set(ret,i++,null); } else { throw new IllegalArgumentException("Cannot use a null value in an array of type " + valueType.getSimpleName()); } } else { if (valueType.isAssignableFrom(value.getClass())) { // Can be set directly Array.set(ret,i++,value); } else { // Try to convert from string Array.set(ret,i++,convertFromString(valueType.getCanonicalName(), value.toString())); } } } return ret; }
private Object convertToArray(String pType, String pValue) { // It's an array String t = pType.substring(1,2); Class valueType; if (t.equals("L")) { // It's an object-type String oType = pType.substring(2,pType.length()-1).replace('/','.'); valueType = ClassUtil.classForName(oType); if (valueType == null) { throw new IllegalArgumentException("No class of type " + oType + "found"); } } else { valueType = TYPE_SIGNATURE_MAP.get(t); if (valueType == null) { throw new IllegalArgumentException("Cannot convert to unknown array type " + t); } } String[] values = EscapeUtil.splitAsArray(pValue, EscapeUtil.PATH_ESCAPE, ","); Object ret = Array.newInstance(valueType,values.length); int i = 0; for (String value : values) { Array.set(ret,i++,value.equals("[null]") ? null : convertFromString(valueType.getCanonicalName(),value)); } return ret; }
private Object convertToArray(String pType, String pValue) { // It's an array String t = pType.substring(1,2); Class valueType; if (t.equals("L")) { // It's an object-type String oType = pType.substring(2,pType.length()-1).replace('/','.'); valueType = ClassUtil.classForName(oType); if (valueType == null) { throw new IllegalArgumentException("No class of type " + oType + "found"); } } else { valueType = TYPE_SIGNATURE_MAP.get(t); if (valueType == null) { throw new IllegalArgumentException("Cannot convert to unknown array type " + t); } } String[] values = EscapeUtil.splitAsArray(pValue, EscapeUtil.PATH_ESCAPE, ","); Object ret = Array.newInstance(valueType,values.length); int i = 0; for (String value : values) { Array.set(ret,i++,value.equals("[null]") ? null : convertFromString(valueType.getCanonicalName(),value)); } return ret; }
/** * Prepare a value from a either a given object or its string representation. * If the value is already assignable to the given class name it is returned directly. * * @param pExpectedClassName type name of the expected type * @param pValue value to either take directly or to convert from its string representation. * @return the prepared / converted object */ public Object prepareValue(String pExpectedClassName, Object pValue) { if (pValue == null) { return null; } else { Class expectedClass = ClassUtil.classForName(pExpectedClassName); Object param = null; if (expectedClass != null) { param = prepareValue(expectedClass,pValue); } if (param == null) { // Ok, we try to convert it from a string // If expectedClass is null, it is probably a native type, so we // let happen the string conversion // later on (e.g. conversion of pArgument.toString()) which will throw // an exception at this point if conversion can not be done return convertFromString(pExpectedClassName, pValue.toString()); } return param; } }
/** * Prepare a value from a either a given object or its string representation. * If the value is already assignable to the given class name it is returned directly. * * @param pExpectedClassName type name of the expected type * @param pValue value to either take directly or to convert from its string representation. * @return the prepared / converted object */ public Object prepareValue(String pExpectedClassName, Object pValue) { if (pValue == null) { return null; } else { Class expectedClass = ClassUtil.classForName(pExpectedClassName); Object param = null; if (expectedClass != null) { param = prepareValue(expectedClass,pValue); } if (param == null) { // Ok, we try to convert it from a string // If expectedClass is null, it is probably a native type, so we // let happen the string conversion // later on (e.g. conversion of pArgument.toString()) which will throw // an exception at this point if conversion can not be done return convertFromString(pExpectedClassName, pValue.toString()); } return param; } }