public static List < TableFieldSchema > getSchemaFromString ( String fields ) { logger . atFine ( ) . log ( "getSchemaFromString('%s')" , fields ) ; // Parse the output schema for Json from fields. JsonParser jsonParser = new JsonParser ( ) ; JsonArray json = jsonParser . parse ( fields ) . getAsJsonArray ( ) ; List < TableFieldSchema > fieldsList = new ArrayList <> ( ) ; // For each item in the list of fields. for ( JsonElement jsonElement : json ) { checkArgument ( jsonElement . isJsonObject ( ) , "Expected JsonObject for element, got '%s'." , jsonElement ) ; JsonObject jsonObject = jsonElement . getAsJsonObject ( ) ; // Set the name and type. checkArgument ( jsonObject . get ( "name" ) != null , "Expected non-null entry for key 'name' in JsonObject '%s'" , jsonObject ) ; checkArgument ( jsonObject . get ( "type" ) != null , "Expected non-null entry for key 'type' in JsonObject '%s'" , jsonObject ) ; TableFieldSchema fieldDef = new TableFieldSchema ( ) ; fieldDef . setName ( jsonObject . get ( "name" ) . getAsString ( ) ) ; fieldDef . setType ( jsonObject . get ( "type" ) . getAsString ( ) ) ; // If mode is not null, set mode. if ( jsonObject . get ( "mode" ) != null ) { fieldDef . setMode ( jsonObject . get ( "mode" ) . getAsString ( ) ) ; } // If the type is RECORD set the fields. if ( jsonObject . get ( "type" ) . getAsString ( ) . equals ( "RECORD" ) ) { checkArgument ( jsonObject . get ( "fields" ) != null , "Expected non-null entry for key 'fields' in JsonObject of type RECORD: '%s'" , jsonObject ) ; fieldDef . setFields ( getSchemaFromString ( jsonObject . get ( "fields" ) . toString ( ) ) ) ; } fieldsList . add ( fieldDef ) ; } // Return list of TableFieldSchema. return fieldsList ; }
Parses the given JSON string and returns the extracted schema .