@Override
 public JSONEntity read(Class<? extends JSONEntity> clazz, HttpInputMessage inputMessage)
     throws IOException, HttpMessageNotReadableException {
   // First read the string
   String jsonString =
       JSONEntityHttpMessageConverter.readToString(
           inputMessage.getBody(), inputMessage.getHeaders().getContentType().getCharSet());
   try {
     return EntityFactory.createEntityFromJSONString(jsonString, clazz);
   } catch (JSONObjectAdapterException e) {
     // Try to convert entity type to a concrete type and try again. See PLFM-2079.
     try {
       JSONObject jsonObject = new JSONObject(jsonString);
       if (jsonObject.has(ENTITY_TYPE)) {
         // get the entity type so we can replace it with concrete type
         String type = jsonObject.getString(ENTITY_TYPE);
         jsonObject.remove(ENTITY_TYPE);
         jsonObject.put(CONCRETE_TYPE, type);
         jsonString = jsonObject.toString();
         // try again
         return EntityFactory.createEntityFromJSONString(jsonString, clazz);
       } else {
         // Something else went wrong
         throw new HttpMessageNotReadableException(e.getMessage(), e);
       }
     } catch (JSONException e1) {
       throw new HttpMessageNotReadableException(e1.getMessage(), e);
     } catch (JSONObjectAdapterException e2) {
       throw new HttpMessageNotReadableException(e2.getMessage(), e);
     }
   }
 }
 @Override
 public void write(JSONEntity entity, MediaType contentType, HttpOutputMessage outputMessage)
     throws IOException, HttpMessageNotWritableException {
   // First write the entity to a JSON string
   try {
     HttpHeaders headers = outputMessage.getHeaders();
     if (headers.getContentType() == null) {
       if (contentType == null
           || contentType.isWildcardType()
           || contentType.isWildcardSubtype()) {
         contentType = MediaType.APPLICATION_JSON;
       }
       if (contentType != null) {
         headers.setContentType(contentType);
       }
     }
     String jsonString = EntityFactory.createJSONStringForEntity(entity);
     long length =
         JSONEntityHttpMessageConverter.writeToStream(
             jsonString, outputMessage.getBody(), contentType.getCharSet());
     if (headers.getContentLength() == -1) {
       headers.setContentLength(length);
     }
   } catch (JSONObjectAdapterException e) {
     throw new HttpMessageNotWritableException(e.getMessage());
   }
 }