@Test(expected = JSONException.class) public void testString() throws JSONException { String jsonString = "test"; OpenAPIParser.parse(jsonString); }
public static Swagger parse(String jsonString) throws JSONException { JSONObject jsonObject = new JSONObject(jsonString); Swagger swagger = new Swagger(); for (Iterator<String> it = jsonObject.keys(); it.hasNext();) { String key = it.next(); Object object = jsonObject.get(key); if (object != null) { if ("swagger".equalsIgnoreCase(key)) { swagger.setSwagger((String) object); } else if ("basePath".equalsIgnoreCase(key)) { swagger.setBasePath((String) object); } else if ("host".equalsIgnoreCase(key)) { swagger.setHost((String) object); } else if ("info".equalsIgnoreCase(key)) { swagger.setInfo(parseInfo((JSONObject) object)); } else if ("schemes".equalsIgnoreCase(key)) { swagger.setSchemes(parseStringArray((JSONArray) object)); } else if ("consumes".equalsIgnoreCase(key)) { swagger.setConsumes(parseStringArray((JSONArray) object)); } else if ("produces".equalsIgnoreCase(key)) { swagger.setProduces(parseStringArray((JSONArray) object)); } else if ("paths".equalsIgnoreCase(key)) { swagger.setPaths(parsePaths((JSONObject) object)); } else if ("definitions".equalsIgnoreCase(key)) { swagger.setDefinitions(parseDefinitions((JSONObject) object)); } else if ("parameters".equalsIgnoreCase(key)) { swagger.setParameters(parseParameters((JSONObject) object)); } else if ("responses".equalsIgnoreCase(key)) { swagger.setResponses(parseResponsesWithRoot((JSONObject) object)); } else if ("securityDefinitions".equalsIgnoreCase(key)) { swagger.setSecurityDefinitions(parseSecurityDefinitions((JSONObject) object)); } else if ("security".equalsIgnoreCase(key)) { swagger.setSecurityRequirement(parseSecurity((JSONObject) object)); } else if ("tags".equalsIgnoreCase(key)) { swagger.setTags(parseTags((JSONArray) object)); } else if ("externalDocs".equalsIgnoreCase(key)) { swagger.setExternalDocs(parseExternalDocs((JSONObject) object)); } else if (key.startsWith("x-")) { swagger.addVendorExtension(key, parseVendorExtension(object)); } } } if (swagger.getSwagger() == null) { throw new IllegalArgumentException("swagger does not exist."); } if (!swagger.getSwagger().equalsIgnoreCase("2.0")) { throw new IllegalArgumentException("swagger is invalid."); } if (swagger.getInfo() == null) { throw new IllegalArgumentException("info does not exist."); } if (swagger.getPaths() == null) { throw new IllegalArgumentException("paths does not exist."); } return swagger; }
OpenAPIParser { public static Swagger parse(String jsonString) throws JSONException { JSONObject jsonObject = new JSONObject(jsonString); Swagger swagger = new Swagger(); for (Iterator<String> it = jsonObject.keys(); it.hasNext();) { String key = it.next(); Object object = jsonObject.get(key); if (object != null) { if ("swagger".equalsIgnoreCase(key)) { swagger.setSwagger((String) object); } else if ("basePath".equalsIgnoreCase(key)) { swagger.setBasePath((String) object); } else if ("host".equalsIgnoreCase(key)) { swagger.setHost((String) object); } else if ("info".equalsIgnoreCase(key)) { swagger.setInfo(parseInfo((JSONObject) object)); } else if ("schemes".equalsIgnoreCase(key)) { swagger.setSchemes(parseStringArray((JSONArray) object)); } else if ("consumes".equalsIgnoreCase(key)) { swagger.setConsumes(parseStringArray((JSONArray) object)); } else if ("produces".equalsIgnoreCase(key)) { swagger.setProduces(parseStringArray((JSONArray) object)); } else if ("paths".equalsIgnoreCase(key)) { swagger.setPaths(parsePaths((JSONObject) object)); } else if ("definitions".equalsIgnoreCase(key)) { swagger.setDefinitions(parseDefinitions((JSONObject) object)); } else if ("parameters".equalsIgnoreCase(key)) { swagger.setParameters(parseParameters((JSONObject) object)); } else if ("responses".equalsIgnoreCase(key)) { swagger.setResponses(parseResponsesWithRoot((JSONObject) object)); } else if ("securityDefinitions".equalsIgnoreCase(key)) { swagger.setSecurityDefinitions(parseSecurityDefinitions((JSONObject) object)); } else if ("security".equalsIgnoreCase(key)) { swagger.setSecurityRequirement(parseSecurity((JSONObject) object)); } else if ("tags".equalsIgnoreCase(key)) { swagger.setTags(parseTags((JSONArray) object)); } else if ("externalDocs".equalsIgnoreCase(key)) { swagger.setExternalDocs(parseExternalDocs((JSONObject) object)); } else if (key.startsWith("x-")) { swagger.addVendorExtension(key, parseVendorExtension(object)); } } } if (swagger.getSwagger() == null) { throw new IllegalArgumentException("swagger does not exist."); } if (!swagger.getSwagger().equalsIgnoreCase("2.0")) { throw new IllegalArgumentException("swagger is invalid."); } if (swagger.getInfo() == null) { throw new IllegalArgumentException("info does not exist."); } if (swagger.getPaths() == null) { throw new IllegalArgumentException("paths does not exist."); } return swagger; } }
OpenAPIParser { public static Swagger parse(String jsonString) throws JSONException { JSONObject jsonObject = new JSONObject(jsonString); Swagger swagger = new Swagger(); for (Iterator<String> it = jsonObject.keys(); it.hasNext();) { String key = it.next(); Object object = jsonObject.get(key); if (object != null) { if ("swagger".equalsIgnoreCase(key)) { swagger.setSwagger((String) object); } else if ("basePath".equalsIgnoreCase(key)) { swagger.setBasePath((String) object); } else if ("host".equalsIgnoreCase(key)) { swagger.setHost((String) object); } else if ("info".equalsIgnoreCase(key)) { swagger.setInfo(parseInfo((JSONObject) object)); } else if ("schemes".equalsIgnoreCase(key)) { swagger.setSchemes(parseStringArray((JSONArray) object)); } else if ("consumes".equalsIgnoreCase(key)) { swagger.setConsumes(parseStringArray((JSONArray) object)); } else if ("produces".equalsIgnoreCase(key)) { swagger.setProduces(parseStringArray((JSONArray) object)); } else if ("paths".equalsIgnoreCase(key)) { swagger.setPaths(parsePaths((JSONObject) object)); } else if ("definitions".equalsIgnoreCase(key)) { swagger.setDefinitions(parseDefinitions((JSONObject) object)); } else if ("parameters".equalsIgnoreCase(key)) { swagger.setParameters(parseParameters((JSONObject) object)); } else if ("responses".equalsIgnoreCase(key)) { swagger.setResponses(parseResponsesWithRoot((JSONObject) object)); } else if ("securityDefinitions".equalsIgnoreCase(key)) { swagger.setSecurityDefinitions(parseSecurityDefinitions((JSONObject) object)); } else if ("security".equalsIgnoreCase(key)) { swagger.setSecurityRequirement(parseSecurity((JSONObject) object)); } else if ("tags".equalsIgnoreCase(key)) { swagger.setTags(parseTags((JSONArray) object)); } else if ("externalDocs".equalsIgnoreCase(key)) { swagger.setExternalDocs(parseExternalDocs((JSONObject) object)); } else if (key.startsWith("x-")) { swagger.addVendorExtension(key, parseVendorExtension(object)); } } } if (swagger.getSwagger() == null) { throw new IllegalArgumentException("swagger does not exist."); } if (!swagger.getSwagger().equalsIgnoreCase("2.0")) { throw new IllegalArgumentException("swagger is invalid."); } if (swagger.getInfo() == null) { throw new IllegalArgumentException("info does not exist."); } if (swagger.getPaths() == null) { throw new IllegalArgumentException("paths does not exist."); } return swagger; } private  OpenAPIParser(); }
OpenAPIParser { public static Swagger parse(String jsonString) throws JSONException { JSONObject jsonObject = new JSONObject(jsonString); Swagger swagger = new Swagger(); for (Iterator<String> it = jsonObject.keys(); it.hasNext();) { String key = it.next(); Object object = jsonObject.get(key); if (object != null) { if ("swagger".equalsIgnoreCase(key)) { swagger.setSwagger((String) object); } else if ("basePath".equalsIgnoreCase(key)) { swagger.setBasePath((String) object); } else if ("host".equalsIgnoreCase(key)) { swagger.setHost((String) object); } else if ("info".equalsIgnoreCase(key)) { swagger.setInfo(parseInfo((JSONObject) object)); } else if ("schemes".equalsIgnoreCase(key)) { swagger.setSchemes(parseStringArray((JSONArray) object)); } else if ("consumes".equalsIgnoreCase(key)) { swagger.setConsumes(parseStringArray((JSONArray) object)); } else if ("produces".equalsIgnoreCase(key)) { swagger.setProduces(parseStringArray((JSONArray) object)); } else if ("paths".equalsIgnoreCase(key)) { swagger.setPaths(parsePaths((JSONObject) object)); } else if ("definitions".equalsIgnoreCase(key)) { swagger.setDefinitions(parseDefinitions((JSONObject) object)); } else if ("parameters".equalsIgnoreCase(key)) { swagger.setParameters(parseParameters((JSONObject) object)); } else if ("responses".equalsIgnoreCase(key)) { swagger.setResponses(parseResponsesWithRoot((JSONObject) object)); } else if ("securityDefinitions".equalsIgnoreCase(key)) { swagger.setSecurityDefinitions(parseSecurityDefinitions((JSONObject) object)); } else if ("security".equalsIgnoreCase(key)) { swagger.setSecurityRequirement(parseSecurity((JSONObject) object)); } else if ("tags".equalsIgnoreCase(key)) { swagger.setTags(parseTags((JSONArray) object)); } else if ("externalDocs".equalsIgnoreCase(key)) { swagger.setExternalDocs(parseExternalDocs((JSONObject) object)); } else if (key.startsWith("x-")) { swagger.addVendorExtension(key, parseVendorExtension(object)); } } } if (swagger.getSwagger() == null) { throw new IllegalArgumentException("swagger does not exist."); } if (!swagger.getSwagger().equalsIgnoreCase("2.0")) { throw new IllegalArgumentException("swagger is invalid."); } if (swagger.getInfo() == null) { throw new IllegalArgumentException("info does not exist."); } if (swagger.getPaths() == null) { throw new IllegalArgumentException("paths does not exist."); } return swagger; } private  OpenAPIParser(); static Swagger parse(String jsonString); @SuppressWarnings("unchecked") static Bundle parseJSONObject(JSONObject jsonObject); @SuppressWarnings("unchecked") static ArrayList parseJSONArray(JSONArray jsonArray); }
OpenAPIParser { public static Swagger parse(String jsonString) throws JSONException { JSONObject jsonObject = new JSONObject(jsonString); Swagger swagger = new Swagger(); for (Iterator<String> it = jsonObject.keys(); it.hasNext();) { String key = it.next(); Object object = jsonObject.get(key); if (object != null) { if ("swagger".equalsIgnoreCase(key)) { swagger.setSwagger((String) object); } else if ("basePath".equalsIgnoreCase(key)) { swagger.setBasePath((String) object); } else if ("host".equalsIgnoreCase(key)) { swagger.setHost((String) object); } else if ("info".equalsIgnoreCase(key)) { swagger.setInfo(parseInfo((JSONObject) object)); } else if ("schemes".equalsIgnoreCase(key)) { swagger.setSchemes(parseStringArray((JSONArray) object)); } else if ("consumes".equalsIgnoreCase(key)) { swagger.setConsumes(parseStringArray((JSONArray) object)); } else if ("produces".equalsIgnoreCase(key)) { swagger.setProduces(parseStringArray((JSONArray) object)); } else if ("paths".equalsIgnoreCase(key)) { swagger.setPaths(parsePaths((JSONObject) object)); } else if ("definitions".equalsIgnoreCase(key)) { swagger.setDefinitions(parseDefinitions((JSONObject) object)); } else if ("parameters".equalsIgnoreCase(key)) { swagger.setParameters(parseParameters((JSONObject) object)); } else if ("responses".equalsIgnoreCase(key)) { swagger.setResponses(parseResponsesWithRoot((JSONObject) object)); } else if ("securityDefinitions".equalsIgnoreCase(key)) { swagger.setSecurityDefinitions(parseSecurityDefinitions((JSONObject) object)); } else if ("security".equalsIgnoreCase(key)) { swagger.setSecurityRequirement(parseSecurity((JSONObject) object)); } else if ("tags".equalsIgnoreCase(key)) { swagger.setTags(parseTags((JSONArray) object)); } else if ("externalDocs".equalsIgnoreCase(key)) { swagger.setExternalDocs(parseExternalDocs((JSONObject) object)); } else if (key.startsWith("x-")) { swagger.addVendorExtension(key, parseVendorExtension(object)); } } } if (swagger.getSwagger() == null) { throw new IllegalArgumentException("swagger does not exist."); } if (!swagger.getSwagger().equalsIgnoreCase("2.0")) { throw new IllegalArgumentException("swagger is invalid."); } if (swagger.getInfo() == null) { throw new IllegalArgumentException("info does not exist."); } if (swagger.getPaths() == null) { throw new IllegalArgumentException("paths does not exist."); } return swagger; } private  OpenAPIParser(); static Swagger parse(String jsonString); @SuppressWarnings("unchecked") static Bundle parseJSONObject(JSONObject jsonObject); @SuppressWarnings("unchecked") static ArrayList parseJSONArray(JSONArray jsonArray);  }