private String parseErrorObject ( String errorObject ) { if ( errorObject == null ) { return null ; } try { // Just use JsonObject parse directly instead of DataModelSerializer as we only want one attribute InputStream inputStream = new ByteArrayInputStream ( errorObject . getBytes ( Charset . forName ( "UTF-8" ) ) ) ; JsonReader jsonReader = Json . createReader ( inputStream ) ; JsonObject parsedObject = jsonReader . readObject ( ) ; jsonReader . close ( ) ; Object errorMessage = parsedObject . get ( "message" ) ; if ( errorMessage != null && errorMessage instanceof JsonString && ! ( ( JsonString ) errorMessage ) . getString ( ) . isEmpty ( ) ) { return ( ( JsonString ) errorMessage ) . getString ( ) ; } else { return errorObject ; } } catch ( JsonException e ) { return errorObject ; } }
This treats the supplied string as a JSON object and looks for the message attribute inside it . If it is not valid JSON or does not contain a message the original string is returned .