@Override protected void writeInternal(Object o, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { OutputStreamWriter writer = new OutputStreamWriter(outputMessage.getBody(), getCharset(outputMessage.getHeaders())); try { if (this.prefixJson) { writer.append("{} && "); } Type typeOfSrc = getType(); if (typeOfSrc != null) { this.gson.toJson(o, typeOfSrc, writer); } else { this.gson.toJson(o, writer); } writer.close(); } catch(JsonIOException ex) { throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex); } }
@Override protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { Reader json = new InputStreamReader(inputMessage.getBody(), getCharset(inputMessage.getHeaders())); try { Type typeOfT = getType(); if (typeOfT != null) { return this.gson.fromJson(json, typeOfT); } else { return this.gson.fromJson(json, clazz); } } catch (JsonSyntaxException ex) { throw new HttpMessageNotReadableException("Could not read JSON: " + ex.getMessage(), ex); } catch (JsonIOException ex) { throw new HttpMessageNotReadableException("Could not read JSON: " + ex.getMessage(), ex); } catch (JsonParseException ex) { throw new HttpMessageNotReadableException("Could not read JSON: " + ex.getMessage(), ex); } }