@Override @Signature public Memory format(Environment env, Memory... args) { try { return StringMemory.valueOf(gson.toJson(args[0])); } catch (JsonIOException e) { env.exception(ProcessorException.class, e.getMessage()); return Memory.NULL; } }
@Override @Signature public Memory formatTo(Environment env, Memory... args) { OutputStream outputStream = Stream.getOutputStream(env, args[1]); try { OutputStreamWriter writer = new OutputStreamWriter(outputStream); gson.toJson(args[0], Memory.class, writer); try { writer.close(); } catch (IOException e) { throw new JsonIOException(e); } } catch (JsonIOException e) { env.exception(ProcessorException.class, e.getMessage()); return Memory.NULL; } finally { Stream.closeStream(env, outputStream); } return Memory.NULL; }
/** * Serializes a Metadata object to Json. This does not flush or close the writer. * * @param metadataList list of metadata to write * @param writer writer * @throws org.apache.tika.exception.TikaException if there is an IOException during writing */ public static void toJson(List<Metadata> metadataList, Writer writer) throws TikaException { try { GSON.toJson(metadataList, writer); } catch (JsonIOException e) { throw new TikaException(e.getMessage()); } }
/** * Serializes a Metadata object to Json. This does not flush or close the writer. * * @param metadata metadata to write * @param writer writer * @throws TikaException if there is an IOException during writing */ public static void toJson(Metadata metadata, Writer writer) throws TikaException { try { GSON.toJson(metadata, writer); } catch (JsonIOException e) { throw new TikaException(e.getMessage()); } }
@Override protected void writeInternal(Object o, Type type, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { Charset charset = getCharset(outputMessage.getHeaders()); try (OutputStreamWriter writer = new OutputStreamWriter(outputMessage.getBody(), charset)) { if (ignoreType) { gsonForWriter.toJson(o, writer); return; } if (type != null) { gsonForWriter.toJson(o, type, writer); return; } gsonForWriter.toJson(o, writer); } catch (JsonIOException ex) { throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex); } }
@Override public Response toResponse(JsonIOException e) { return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).type(MediaType.TEXT_PLAIN).build(); } }
/** * Serializes a Metadata object to Json. This does not flush or close the writer. * * @param metadata metadata to write * @param writer writer * @throws TikaException if there is an IOException during writing */ public static void toJson(Metadata metadata, Writer writer) throws TikaException { try { GSON.toJson(metadata, writer); } catch (JsonIOException e) { throw new TikaException(e.getMessage()); } }
/** * Serializes a Metadata object to Json. This does not flush or close the writer. * * @param metadataList list of metadata to write * @param writer writer * @throws org.apache.tika.exception.TikaException if there is an IOException during writing */ public static void toJson(List<Metadata> metadataList, Writer writer) throws TikaException { try { GSON.toJson(metadataList, writer); } catch (JsonIOException e) { throw new TikaException(e.getMessage()); } }
@Override public void writeTo(T entity, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws WebApplicationException { try { Gson gson = createGson(annotations); JsonElement jsonElement = gson.toJsonTree(entity); Writer writer = null; try { writer = new OutputStreamWriter(entityStream, Charset.forName(UTF8)); gson.toJson(jsonElement, writer); } finally { if (writer != null) { writer.flush(); } } } catch (JsonIOException e) { logger.warn("Experiencing issues on response parsing: {}", e.getMessage()); } catch (IOException e) { logger.warn("Experiencing issues on response output: {}", e.getMessage()); } }
static Config loadConfigFromFile(String str) { Gson gson = new Gson(); Config config = null; try { config = gson.fromJson(new BufferedReader(new FileReader(str)), Config.class); } catch (JsonSyntaxException e) { abort("Invalid JSON syntax in " + str + ": " + e.getMessage()); } catch (JsonIOException e) { abort("Could not read config from " + str + ": " + e.getMessage()); } catch (FileNotFoundException e) { abort("Config file " + str + " not found: " + e.getMessage()); } return config; }
void merge(Reader json, Message.Builder builder) throws IOException { try { JsonReader reader = new JsonReader(json); reader.setLenient(false); merge(jsonParser.parse(reader), builder); } catch (InvalidProtocolBufferException e) { throw e; } catch (JsonIOException e) { // Unwrap IOException. if (e.getCause() instanceof IOException) { throw (IOException) e.getCause(); } else { throw new InvalidProtocolBufferException(e.getMessage()); } } catch (Exception e) { // We convert all exceptions from JSON parsing to our own exceptions. throw new InvalidProtocolBufferException(e.getMessage()); } }
JQueryDataTableParamModel param = DataTablesParamUtility.getParam(request); if(companies.size()< param.iDisplayStart + param.iDisplayLength) { companies = companies.subList(param.iDisplayStart, companies.size()); } else { companies = companies.subList(param.iDisplayStart, param.iDisplayStart + param.iDisplayLength); } try { JsonObject jsonResponse = new JsonObject(); jsonResponse.addProperty("sEcho", sEcho); jsonResponse.addProperty("iTotalRecords", iTotalRecords); jsonResponse.addProperty("iTotalDisplayRecords", iTotalDisplayRecords); for(Company c : companies){ JsonArray row = new JsonArray(); row.add(new JsonPrimitive(c.getName())); row.add(new JsonPrimitive(c.getAddress())); row.add(new JsonPrimitive(c.getTown())); data.add(row); } jsonResponse.add("aaData", data); response.setContentType("application/Json"); response.getWriter().print(jsonResponse.toString()); } catch (JsonIOException e) { e.printStackTrace(); response.setContentType("text/html"); response.getWriter().print(e.getMessage()); }
void merge(Reader json, Message.Builder builder) throws IOException { try { JsonReader reader = new JsonReader(json); reader.setLenient(false); merge(jsonParser.parse(reader), builder); } catch (InvalidProtocolBufferException e) { throw e; } catch (JsonIOException e) { // Unwrap IOException. if (e.getCause() instanceof IOException) { throw (IOException) e.getCause(); } else { throw new InvalidProtocolBufferException(e.getMessage()); } } catch (Exception e) { // We convert all exceptions from JSON parsing to our own exceptions. throw new InvalidProtocolBufferException(e.getMessage()); } }
void merge(Reader json, Message.Builder builder) throws IOException { try { JsonReader reader = new JsonReader(json); reader.setLenient(false); merge(jsonParser.parse(reader), builder); } catch (InvalidProtocolBufferException e) { throw e; } catch (JsonIOException e) { // Unwrap IOException. if (e.getCause() instanceof IOException) { throw (IOException) e.getCause(); } else { throw new InvalidProtocolBufferException(e.getMessage()); } } catch (Exception e) { // We convert all exceptions from JSON parsing to our own exceptions. throw new InvalidProtocolBufferException(e.getMessage()); } }
void merge(Reader json, Message.Builder builder) throws IOException { try { JsonReader reader = new JsonReader(json); reader.setLenient(false); merge(jsonParser.parse(reader), builder); } catch (InvalidProtocolBufferException e) { throw e; } catch (JsonIOException e) { // Unwrap IOException. if (e.getCause() instanceof IOException) { throw (IOException) e.getCause(); } else { throw new InvalidProtocolBufferException(e.getMessage()); } } catch (Exception e) { // We convert all exceptions from JSON parsing to our own exceptions. throw new InvalidProtocolBufferException(e.getMessage()); } }
@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); } }
void merge(Reader json, Message.Builder builder) throws IOException { try { JsonReader reader = new JsonReader(json); reader.setLenient(false); merge(jsonParser.parse(reader), builder); } catch (InvalidProtocolBufferException e) { throw e; } catch (JsonIOException e) { // Unwrap IOException. if (e.getCause() instanceof IOException) { throw (IOException) e.getCause(); } else { throw new InvalidProtocolBufferException(e.getMessage()); } } catch (Exception e) { // We convert all exceptions from JSON parsing to our own exceptions. throw new InvalidProtocolBufferException(e.getMessage()); } }
void merge(Reader json, Message.Builder builder) throws IOException { try { JsonReader reader = new JsonReader(json); reader.setLenient(false); merge(jsonParser.parse(reader), builder); } catch (InvalidProtocolBufferException e) { throw e; } catch (JsonIOException e) { // Unwrap IOException. if (e.getCause() instanceof IOException) { throw (IOException) e.getCause(); } else { throw new InvalidProtocolBufferException(e.getMessage()); } } catch (Exception e) { // We convert all exceptions from JSON parsing to our own exceptions. throw new InvalidProtocolBufferException(e.getMessage()); } }
@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); } }