public static Table extractTableFromJson ( final JsonObject modelJson , final String tablePath ) { Objects . requireNonNull ( modelJson ) ; JsonElement potentialTableJson = findInJson ( modelJson , tablePath ) ; if ( potentialTableJson . isJsonNull ( ) ) { System . out . println ( String . format ( "Failed to extract element '%s' MojoModel dump." , tablePath ) ) ; return null ; } final JsonObject tableJson = potentialTableJson . getAsJsonObject ( ) ; final int rowCount = tableJson . get ( "rowcount" ) . getAsInt ( ) ; final String [ ] columnHeaders ; final Table . ColumnType [ ] columnTypes ; final Object [ ] [ ] data ; // Extract column attributes final JsonArray columns = findInJson ( tableJson , "columns" ) . getAsJsonArray ( ) ; final int columnCount = columns . size ( ) ; columnHeaders = new String [ columnCount ] ; columnTypes = new Table . ColumnType [ columnCount ] ; for ( int i = 0 ; i < columnCount ; i ++ ) { final JsonObject column = columns . get ( i ) . getAsJsonObject ( ) ; columnHeaders [ i ] = column . get ( "description" ) . getAsString ( ) ; columnTypes [ i ] = Table . ColumnType . extractType ( column . get ( "type" ) . getAsString ( ) ) ; } // Extract data JsonArray dataColumns = findInJson ( tableJson , "data" ) . getAsJsonArray ( ) ; data = new Object [ columnCount ] [ rowCount ] ; for ( int i = 0 ; i < columnCount ; i ++ ) { JsonArray column = dataColumns . get ( i ) . getAsJsonArray ( ) ; for ( int j = 0 ; j < rowCount ; j ++ ) { final JsonPrimitive primitiveValue = column . get ( j ) . getAsJsonPrimitive ( ) ; switch ( columnTypes [ i ] ) { case LONG : data [ i ] [ j ] = primitiveValue . getAsLong ( ) ; break ; case DOUBLE : data [ i ] [ j ] = primitiveValue . getAsDouble ( ) ; break ; case STRING : data [ i ] [ j ] = primitiveValue . getAsString ( ) ; break ; } } } return new Table ( tableJson . get ( "name" ) . getAsString ( ) , tableJson . get ( "description" ) . getAsString ( ) , new String [ rowCount ] , columnHeaders , columnTypes , "" , data ) ; }
Extracts a Table from H2O s model serialized into JSON .