public String toString() { return jsonValue.getClass().getSimpleName() + ": " + jsonValue.render(false); }
/** * Map a JSON representation to a Java object. Since no class nor type hint is passed to the * mapper, this method can only handle the most basic mappings. * @param aValue The JSON value that has to be mapped. * @return he resulting Java POJO. * @throws MapperException When the JSON text cannot be mapped to POJO. */ public static Object toJava(JSONValue aValue) throws MapperException { if(aValue.isArray()) return toJava(aValue, LinkedList.class); else if(aValue.isBoolean()) return toJava(aValue, Boolean.class); else if(aValue.isDecimal()) return toJava(aValue, BigDecimal.class); else if(aValue.isInteger()) return toJava(aValue, BigInteger.class); else if(aValue.isString()) return toJava(aValue, String.class); else return toJava(aValue, Object.class); }
if(jsonValue.isSimple()) lBuf.append(" : ").append(jsonValue.render(false, "")); lBuf.append(" :\n").append(jsonValue.render(true, lIndent2)); final JSONValue jsonValue = map.get(lKey); lBuf.append(ParserUtil.render(lKey, false, "")).append(":").append(jsonValue.render(false)); if(lKeyIter.hasNext()) lBuf.append(",");
public Object toJava(JSONValue aValue, Class aRequestedClass) throws MapperException { //lenient to the data to be converted. if(aValue.isDecimal()) return ((JSONDecimal)aValue).getValue().toString(); if(aValue.isInteger()) return ((JSONInteger)aValue).getValue().toString(); if (!aValue.isString()) throw new MapperException("StringMapper cannot map class: " + aValue.getClass().getName()); return ((JSONString) aValue).getValue(); } }
public Object toJava(JSONValue aValue, Class aRequestedClass) throws MapperException { if(aValue.isString()) { try { return Long.parseLong(((JSONString)aValue).getValue()); } catch (NumberFormatException e) { throw new MapperException("LongMapper cannot map value: " + ((JSONString)aValue).getValue()); } } else if(aValue.isInteger()) return ((JSONInteger) aValue).getValue().longValue(); else throw new MapperException("LongMapper cannot map: " + aValue.getClass().getName()); }
public void validate(JSONValue aValue) throws ValidationException { int lSize = 0; if(aValue.isArray()) lSize = ((JSONArray) aValue).getValue().size(); else if(aValue.isString()) lSize = ((JSONString) aValue).getValue().length(); else if(aValue.isObject()) lSize = ((JSONObject) aValue).getValue().size(); else fail("The value is not a JSONArray, JSONString or JSONObject.", aValue); // If there are lenght specs, we check them. if(minLength != null) { if(lSize < minLength) fail("The size (" + lSize +") is smaller then allowed (" + minLength + ").", aValue); } if(maxLength != null) { if( lSize > maxLength ) fail("The size (" + lSize +") is larger then allowed (" + maxLength + ").", aValue); } } }
public Object toJava(JSONValue aValue, Class aRequestedClass) throws MapperException { if(aValue.isString()) { return Boolean.parseBoolean(((JSONString)aValue).getValue()); } else if(aValue.isBoolean()) { return ((JSONBoolean) aValue).getValue(); } else throw new MapperException("BooleanMapper cannot map: " + aValue.getClass().getName()); }
public void validate(JSONValue aValue) throws ValidationException { Iterator<JSONValue> lIter = null; if(aValue.isArray()) lIter = ((JSONArray) aValue).getValue().iterator(); else if(aValue.isObject()) lIter = ((JSONObject) aValue).getValue().values().iterator(); else fail("The value is not a JSONComplex, it has no content.", aValue); // Finally we apply the internal rule to all elements. if(lIter != null) { while(lIter.hasNext()) { JSONValue lVal = (JSONValue) lIter.next(); rule.validate(lVal); } } } }
public Range(String aName, JSONObject aRule) throws ValidationException { super(aName, aRule); if (aRule.containsKey(ValidatorUtil.PARAM_MIN)) { JSONValue lMin = aRule.get(ValidatorUtil.PARAM_MIN); if (!lMin.isNumber()) { final String lMsg = "Minimum length should be specified using a number."; throw new ValidationException(lMsg, aRule, "WRONG TYPE"); } else minValue = cvtNumber((JSONNumber) lMin); } if (aRule.containsKey(ValidatorUtil.PARAM_MAX)) { JSONValue lMax = aRule.get(ValidatorUtil.PARAM_MAX); if (!lMax.isInteger()) { final String lMsg = "Maximum length should be specified using an integer."; throw new ValidationException(lMsg, aRule, "WRONG TYPE"); } else maxValue = cvtNumber((JSONNumber) lMax); } }
public void validate(JSONValue aValue) throws ValidationException { // First we check if we have an array. if(!aValue.isArray()) fail("The value is not a JSONArray.", aValue); } }
public void validate(JSONValue aValue) throws ValidationException { if(!aValue.isInteger()) fail("The value is not a JSONInteger.", aValue); } }
public void validate(JSONValue aValue) throws ValidationException { if(!aValue.isBoolean()) fail("The value is not a JSONBoolean", aValue); } }
public void validate(JSONValue aValue) throws ValidationException { if(!aValue.isComplex()) fail("The value is not a JSONCOmplex", aValue); } }
public void validate(JSONValue aValue) throws ValidationException { if(!aValue.isDecimal()) fail("The value is not a JSONDecimal.", aValue); } }
public Object toJava(JSONValue aValue, Class aRequestedClass) throws MapperException { if(aValue.isString()) { try { return new BigDecimal(((JSONString)aValue).getValue()); } catch (NumberFormatException e) { throw new MapperException("BigDecimalMapper cannot map value: " + ((JSONString)aValue).getValue()); } } else if(aValue.isDecimal()) return ((JSONDecimal) aValue).getValue(); else if(aValue.isInteger()) return new BigDecimal(((JSONInteger)aValue).getValue()); else throw new MapperException("BigDecimalMapper cannot map: " + aValue.getClass().getName()); }
public Object toJava(JSONValue aValue, Class aRequestedClass) throws MapperException { if(aValue.isString()) { try { return Byte.parseByte(((JSONString)aValue).getValue()); } catch (NumberFormatException e) { throw new MapperException("ByteMapper cannot map value: " + ((JSONString)aValue).getValue()); } } else if(aValue.isInteger()) return (byte) ((JSONInteger) aValue).getValue().intValue(); else throw new MapperException("ByteMapper cannot map: " + aValue.getClass().getName()); }
public Object toJava(JSONValue aValue, Class aRawClass, Type[] aTypes) throws MapperException if (!aValue.isArray()) throw new MapperException("CollectionMapper cannot map: " + aValue.getClass().getName()); if (!Collection.class.isAssignableFrom(aRawClass)) throw new MapperException("CollectionMapper cannot map: " + aValue.getClass().getName());
public Length(String aName, JSONObject aRule) throws ValidationException { super(aName, aRule); if (aRule.containsKey(ValidatorUtil.PARAM_MIN)) { JSONValue lMin = aRule.get(ValidatorUtil.PARAM_MIN); if (!lMin.isInteger()) { final String lMsg = "Minimum length should be specified using an integer."; throw new ValidationException(lMsg, aRule, "WRONG TYPE"); } else minLength = ((JSONInteger) lMin).getValue().intValue(); } if (aRule.containsKey(ValidatorUtil.PARAM_MAX)) { JSONValue lMax = aRule.get(ValidatorUtil.PARAM_MAX); if (!lMax.isInteger()) { final String lMsg = "Maximum length should be specified using an integer."; throw new ValidationException(lMsg, aRule, "WRONG TYPE"); } else maxLength = ((JSONInteger) lMax).getValue().intValue(); } }
public String getString(Object... keys) { String jsString = getValue(keys).render(false); return jsString.substring(1, jsString.length() - 1); }