public JSONObject parseJSONSchema ( JSONObject schema ) throws Exception { JSONObject json = new JSONObject ( ) ; String name = "" ; JSONObject properties = schema ; // Check if key 'properties' exists if ( schema . has ( "properties" ) ) { // Obtain properties properties = schema . getJSONObject ( "properties" ) ; } // Obtain all keys and iterate through them Iterator < ? > keys = properties . keys ( ) ; while ( keys . hasNext ( ) ) { // Obtain key String key = keys . next ( ) . toString ( ) ; // Obtain value of key JSONObject element = properties . getJSONObject ( key ) ; // Check if value contain properties // If it DOESN'T CONTAIN properties if ( ! element . has ( "properties" ) ) { // Check if it has default value if ( element . has ( "default" ) ) { // Add element with the default value assigned json . put ( key , element . get ( "default" ) ) ; // If it doesn't have default value, we assign a default value depending on the type } else { switch ( element . getString ( "type" ) ) { case "string" : json . put ( key , "" ) ; break ; case "boolean" : json . put ( key , false ) ; break ; case "number" : case "integer" : json . put ( key , 0 ) ; break ; default : Assertions . fail ( "type not expected" ) ; } } // If it CONTAINS properties } else { // Recursive call, keep evaluating json json . put ( key , parseJSONSchema ( element ) ) ; } } return json ; }
Generate deployment json from schema