List < HostToken > parseTokenListFromJson ( String json ) { List < HostToken > hostTokens = new ArrayList < HostToken > ( ) ; JSONParser parser = new JSONParser ( ) ; try { JSONArray arr = ( JSONArray ) parser . parse ( json ) ; Iterator < ? > iter = arr . iterator ( ) ; while ( iter . hasNext ( ) ) { Object item = iter . next ( ) ; if ( ! ( item instanceof JSONObject ) ) { continue ; } JSONObject jItem = ( JSONObject ) item ; Long token = Long . parseLong ( ( String ) jItem . get ( "token" ) ) ; String hostname = ( String ) jItem . get ( "hostname" ) ; String ipAddress = ( String ) jItem . get ( "ip" ) ; String zone = ( String ) jItem . get ( "zone" ) ; String datacenter = ( String ) jItem . get ( "dc" ) ; String portStr = ( String ) jItem . get ( "port" ) ; String securePortStr = ( String ) jItem . get ( "secure_port" ) ; String hashtag = ( String ) jItem . get ( "hashtag" ) ; int port = Host . DEFAULT_PORT ; if ( portStr != null ) { port = Integer . valueOf ( portStr ) ; } int securePort = port ; if ( securePortStr != null ) { securePort = Integer . valueOf ( securePortStr ) ; } Host host = new Host ( hostname , ipAddress , port , securePort , zone , datacenter , Status . Up , hashtag ) ; if ( isLocalDatacenterHost ( host ) ) { HostToken hostToken = new HostToken ( token , host ) ; hostTokens . add ( hostToken ) ; } } } catch ( ParseException e ) { Logger . error ( "Failed to parse json response: " + json , e ) ; throw new RuntimeException ( e ) ; } return hostTokens ; }
package - private for Test