if (jsonString == null) {
throw new IllegalArgumentException("JSON string cannot be null");
}
Policy policy = new Policy();
List<Statement> statements = new LinkedList<Statement>();
try {
JSONObject jPolicy = new JSONObject(jsonString);
if (Arrays.asList(JSONObject.getNames(jPolicy)).contains(JsonDocumentFields.POLICY_ID)) {
policy.setId(jPolicy.getString(JsonDocumentFields.POLICY_ID));
}
JSONArray jStatements = jPolicy.getJSONArray(JsonDocumentFields.STATEMENT);
for (int index = 0 ; index < jStatements.length() ; index++) {
Statement statement = convertStatement(jStatements.getJSONObject(index));
if (statement != null) {
statements.add(statement);
}
}
} catch (Exception e) {
String message = "Unable to generate policy object fron JSON string " + e.getMessage();
throw new IllegalArgumentException(message, e);
}
policy.setStatements(statements);
return policy;
}