Refine search
@Override public URI read(JsonReader in) throws IOException { if (in.peek() == JsonToken.NULL) { in.nextNull(); return null; } try { String nextString = in.nextString(); return "null".equals(nextString) ? null : new URI(nextString); } catch (URISyntaxException e) { throw new JsonIOException(e); } } @Override
@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; }
/** * Parses the specified JSON string into a parse tree * * @param json JSON text * @return a parse tree of {@link JsonElement}s corresponding to the specified JSON * @throws JsonParseException if the specified text is not valid JSON * @since 1.3 */ public JsonElement parse(Reader json) throws JsonIOException, JsonSyntaxException { try { JsonReader jsonReader = new JsonReader(json); JsonElement element = parse(jsonReader); if (!element.isJsonNull() && jsonReader.peek() != JsonToken.END_DOCUMENT) { throw new JsonSyntaxException("Did not consume the entire document."); } return element; } catch (MalformedJsonException e) { throw new JsonSyntaxException(e); } catch (IOException e) { throw new JsonIOException(e); } catch (NumberFormatException e) { throw new JsonSyntaxException(e); } }
@Override public T convert(ResponseBody value) throws IOException { JsonReader jsonReader = gson.newJsonReader(value.charStream()); try { T result = adapter.read(jsonReader); if (jsonReader.peek() != JsonToken.END_DOCUMENT) { throw new JsonIOException("JSON document was not fully consumed."); } return result; } finally { value.close(); } } }
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()); } }
public JsonParserStream parse(final JsonReader reader) { // code copied from gson's JsonParser and Stream classes final boolean lenient = reader.isLenient(); reader.setLenient(true); boolean isEmpty = true; try { reader.peek(); isEmpty = false; final CompositeNodeDataWithSchema compositeNodeDataWithSchema = new CompositeNodeDataWithSchema(parentNode); read(reader, compositeNodeDataWithSchema); compositeNodeDataWithSchema.write(writer); return this; } catch (final EOFException e) { if (isEmpty) { return this; } // The stream ended prematurely so it is likely a syntax error. throw new JsonSyntaxException(e); } catch (final MalformedJsonException | NumberFormatException e) { throw new JsonSyntaxException(e); } catch (final IOException e) { throw new JsonIOException(e); } catch (StackOverflowError | OutOfMemoryError e) { throw new JsonParseException("Failed parsing JSON source: " + reader + " to Json", e); } finally { reader.setLenient(lenient); } }
/** * Converts {@code jsonTree} to a Java object. * * @param jsonTree the Java object to convert. May be {@link JsonNull}. */ /*public*/ final T fromJsonTree(JsonElement jsonTree) { try { JsonReader jsonReader = new JsonTreeReader(jsonTree); jsonReader.setLenient(true); return read(jsonReader); } catch (IOException e) { throw new JsonIOException(e); } } }
@SuppressWarnings("rawtypes") @Override public T construct() { if (type instanceof ParameterizedType) { Type elementType = ((ParameterizedType) type).getActualTypeArguments()[0]; if (elementType instanceof Class) { return (T) EnumSet.noneOf((Class)elementType); } else { throw new JsonIOException("Invalid EnumSet type: " + type.toString()); } } else { throw new JsonIOException("Invalid EnumSet type: " + type.toString()); } } };
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()); }
/** * Parses the MCEF configuration file. * * @param f The configuration file. * @return The parsed configuration file. */ private JsonObject readConfig(File f) { try { return (new JsonParser()).parse(new FileReader(f)).getAsJsonObject(); } catch(JsonIOException e) { Log.error("IOException while reading remote config."); e.printStackTrace(); return null; } catch(FileNotFoundException e) { Log.error("Couldn't find remote config."); e.printStackTrace(); return null; } catch(Exception e) { Log.error("Syntax error in remote config."); e.printStackTrace(); return null; } }
/** * Parses the specified JSON string into a parse tree * * @param json JSON text * @return a parse tree of {@link JsonElement}s corresponding to the specified JSON * @throws JsonParseException if the specified text is not valid JSON * @since 1.3 */ public JsonElement parse(Reader json) throws JsonIOException, JsonSyntaxException { try { JsonReader jsonReader = new JsonReader(json); JsonElement element = parse(jsonReader); if (!element.isJsonNull() && jsonReader.peek() != JsonToken.END_DOCUMENT) { throw new JsonSyntaxException("Did not consume the entire document."); } return element; } catch (MalformedJsonException e) { throw new JsonSyntaxException(e); } catch (IOException e) { throw new JsonIOException(e); } catch (NumberFormatException e) { throw new JsonSyntaxException(e); } }
@Override public T convert(ResponseBody value) throws IOException { JsonReader jsonReader = gson.newJsonReader(value.charStream()); try { T result = adapter.read(jsonReader); if (jsonReader.peek() != JsonToken.END_DOCUMENT) { throw new JsonIOException("JSON document was not fully consumed."); } return result; } finally { value.close(); } } }
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()); } }
public JsonParserStream parse(final JsonReader reader) { // code copied from gson's JsonParser and Stream classes final boolean lenient = reader.isLenient(); reader.setLenient(true); boolean isEmpty = true; try { reader.peek(); isEmpty = false; final CompositeNodeDataWithSchema compositeNodeDataWithSchema = new CompositeNodeDataWithSchema(parentNode); read(reader, compositeNodeDataWithSchema); compositeNodeDataWithSchema.write(writer); return this; } catch (final EOFException e) { if (isEmpty) { return this; } // The stream ended prematurely so it is likely a syntax error. throw new JsonSyntaxException(e); } catch (final MalformedJsonException | NumberFormatException e) { throw new JsonSyntaxException(e); } catch (final IOException e) { throw new JsonIOException(e); } catch (StackOverflowError | OutOfMemoryError e) { throw new JsonParseException("Failed parsing JSON source: " + reader + " to Json", e); } finally { reader.setLenient(lenient); } }
/** {@inheritDoc} */ @Override public void makeAccessible(AccessibleObject ao) { boolean success = makeAccessibleWithUnsafe(ao); if (!success) { try { // unsafe couldn't be found, so try using accessible anyway ao.setAccessible(true); } catch (SecurityException e) { throw new JsonIOException("Gson couldn't modify fields for " + ao + "\nand sun.misc.Unsafe not found.\nEither write a custom type adapter," + " or make fields accessible, or include sun.misc.Unsafe.", e); } } }
@Override public Object decode(Response response, Type type) throws IOException { if (response.status() == 404) return Util.emptyValueOf(type); if (response.body() == null) return null; Reader reader = response.body().asReader(); try { return gson.fromJson(reader, type); } catch (JsonIOException e) { if (e.getCause() != null && e.getCause() instanceof IOException) { throw IOException.class.cast(e.getCause()); } throw e; } finally { ensureClosed(reader); } } }
/** * Main method that calls all methods above in a flow. * * @param args */ public static void main(String[] args) { try { SubscriptionSample subscriptionSample = new SubscriptionSample(); APIContext context = new APIContext(clientID, clientSecret, "sandbox"); subscriptionSample.create(context); System.out.println("create response:\n" + Plan.getLastResponse()); subscriptionSample.update(context); System.out.println("plan updated"); subscriptionSample.retrieve(context); System.out.println("retrieve response:\n" + Plan.getLastResponse()); } catch (JsonSyntaxException e) { e.printStackTrace(); } catch (JsonIOException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (PayPalRESTException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
@Override public URI read(JsonReader in) throws IOException { if (in.peek() == JsonToken.NULL) { in.nextNull(); return null; } try { String nextString = in.nextString(); return "null".equals(nextString) ? null : new URI(nextString); } catch (URISyntaxException e) { throw new JsonIOException(e); } } @Override