public void validate(JSONValue aValue) throws ValidationException { // First we check if we have an array. if(!aValue.isObject()) fail("The value is not a JSONObject.", aValue); } }
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 Object toJava(JSONValue aValue, Class aRawClass, Type[] aTypes) throws MapperException if (!aValue.isObject()) throw new MapperException("MapMapper cannot map: " + aValue.getClass().getName()); if (!Map.class.isAssignableFrom(aRawClass)) throw new MapperException("MapMapper cannot map: " + aValue.getClass().getName());
public Object toJava(JSONValue aValue, Class aRequestedClass) throws MapperException if(!aValue.isObject()) throw new MapperException("ObjectMapperDirect cannot map: " + aValue.getClass().getName()); JSONObject aObject = (JSONObject) aValue;
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.isObject()) throw new MapperException("ObjectMapper cannot map: " + aValue.getClass().getName()); JSONObject aObject = (JSONObject) aValue;
public static Validator buildValidator(JSONValue aVal, HashMap<String, Validator> aRuleset) throws ValidationException if(! aVal.isObject())
public void validate(JSONValue aValue) throws ValidationException { if(!aValue.isObject()) fail("The value is not a JSONObject.", aValue); JSONObject lObj = (JSONObject) aValue; if(!lObj.containsKey(key)) fail("The object does not contain the key: \"" + key + "\".", lObj); JSONValue lVal = lObj.get(key); for (Case aCase : rules) { if(aCase.isApplicable(lVal)) { aCase.getValidator().validate(lObj); return; } } fail("No applicable rule found for key: \"" + key + "\", value: " + lVal.toString(), aValue); } }
public void validate(JSONValue aValue) throws ValidationException { // Only for objects. if(!aValue.isObject()) fail("The value is not a JSONObject.", aValue); JSONObject lObj = (JSONObject) aValue; // First we check if required keys are there. for(PropRule aRequired : required) { if(!lObj.containsKey(aRequired.getKey())) fail("The object lacks a required key: \"" + aRequired.getKey() + "\".", aValue); } // Now we iterate over all keys in the object and lookup the spec. for(String lKey : lObj.getValue().keySet()) { if(!all.containsKey(lKey)) fail("The object contains an unspecified key: \"" + lKey + "\".", aValue); PropRule lRule = all.get(lKey); try { lRule.getRule().validate(lObj.get(lKey)); } catch(ValidationException e) { fail("The object property: \"" + lKey + "\" has invalid content. Internal message: " + e.getMessage(), aValue); } } } }
public Switch(String aName, JSONObject aRule, HashMap<String,Validator> aRuleset) throws ValidationException { super(aName, aRule); ValidatorUtil.requiresAttribute(aRule, ValidatorUtil.PARAM_CASE, JSONArray.class); List<JSONValue> lCases = ((JSONArray) aRule.get(ValidatorUtil.PARAM_CASE)).getValue(); for (JSONValue lCase : lCases) { if(!lCase.isObject()) fail("A case in a swicht should be an object type.", lCase); JSONObject lObjCase = (JSONObject) lCase; ValidatorUtil.requiresAttribute(lObjCase, ValidatorUtil.PARAM_RULE, JSONObject.class); JSONObject lRule = (JSONObject) lObjCase.get(ValidatorUtil.PARAM_RULE); Validator lValidator = ValidatorUtil.buildValidator(lRule, aRuleset); ValidatorUtil.requiresAttribute(lObjCase, ValidatorUtil.PARAM_VALUES, JSONArray.class); JSONArray lVals = (JSONArray) lObjCase.get(ValidatorUtil.PARAM_VALUES); Case lNewCase = new Case(lValidator, lVals.getValue()); rules.add(lNewCase); } ValidatorUtil.requiresAttribute(aRule, ValidatorUtil.PARAM_KEY, JSONString.class); key = ((JSONString) aRule.get(ValidatorUtil.PARAM_KEY)).getValue(); }
for (JSONValue lPair : lPairs) if(!lPair.isObject())