List of usage examples for com.google.gson.reflect TypeToken TypeToken
@SuppressWarnings("unchecked") protected TypeToken()
From source file:ch.ethz.inf.vs.hypermedia.corehal.block.CoREHalResourceFuture.java
License:Open Source License
public Map<String, Object> dataToObjectMap(JsonElement data) { Type type = new TypeToken<Map<String, Object>>() { }.getType();/* ww w .j a v a2 s . c o m*/ return ResourceFuture.gson.fromJson(data, type); }
From source file:ch.icclab.cyclops.consume.data.BillProcess.java
License:Open Source License
/** * Parse JSON into list of Bill objects//w w w . j a va2 s . c om * @param json text as array * @return list or null */ private List<Bill> parseWhatCouldBeJSONList(String json) { try { Type billType = new TypeToken<List<Bill>>() { }.getType(); return getNewGson().fromJson(json, billType); } catch (Exception e) { return null; } }
From source file:ch.icclab.cyclops.consume.data.CDRProcess.java
License:Open Source License
/** * Parse JSON into list of CDR objects//from w w w. j av a 2 s.c o m * @param json text as array * @return list or null */ private List<CDR> parseWhatCouldBeJSONList(String json) { try { Type CDRType = new TypeToken<List<CDR>>() { }.getType(); return getNewGson().fromJson(json, CDRType); } catch (Exception e) { return null; } }
From source file:ch.icclab.cyclops.consume.data.UsageProcess.java
License:Open Source License
/** * Parse JSON into list of Usage objects * @param json text as array/*www .j a v a2 s .co m*/ * @return list or null */ private List<Usage> parseWhatCouldBeJSONList(String json) { try { Type usageType = new TypeToken<List<Usage>>() { }.getType(); return getNewGson().fromJson(json, usageType); } catch (Exception e) { return null; } }
From source file:ch.njol.skript.bukkitutil.BukkitUnsafe.java
License:Open Source License
private static boolean loadMaterialMap(String name) throws IOException { try (InputStream is = Skript.getInstance().getResource(name)) { if (is == null) { // No mappings for this Minecraft version return false; }/*w w w . j a v a 2 s. c o m*/ String data = new String(ByteStreams.toByteArray(is), StandardCharsets.UTF_8); Type type = new TypeToken<Map<String, Material>>() { }.getType(); materialMap = new Gson().fromJson(data, type); } return true; }
From source file:ch.njol.skript.bukkitutil.BukkitUnsafe.java
License:Open Source License
private static void initIdMappings() { try (InputStream is = Skript.getInstance().getResource("materials/ids.json")) { if (is == null) { throw new AssertionError("missing id mappings"); }/*from ww w. jav a 2s .c om*/ String data = new String(ByteStreams.toByteArray(is), StandardCharsets.UTF_8); Type type = new TypeToken<Map<Integer, String>>() { }.getType(); Map<Integer, String> rawMappings = new Gson().fromJson(data, type); // Process raw mappings Map<Integer, Material> parsed = new HashMap<>(rawMappings.size()); if (newMaterials) { // Legacy material conversion API for (Map.Entry<Integer, String> entry : rawMappings.entrySet()) { parsed.put(entry.getKey(), Material.matchMaterial(entry.getValue(), true)); } } else { // Just enum API for (Map.Entry<Integer, String> entry : rawMappings.entrySet()) { parsed.put(entry.getKey(), Material.valueOf(entry.getValue())); } } idMappings = parsed; } catch (IOException e) { throw new AssertionError(e); } }
From source file:ch.there.gson.GsonInvokerServiceExporter.java
License:Apache License
@Override public void handle(HttpExchange exchange) throws IOException { Gson gson = GsonFactory.getGson();//w ww .j a v a 2 s . c om try { Integer rpcVersion = Integer.valueOf(exchange.getRequestHeaders().getFirst("rpc-version")); CallerRemoteApiVersion.setVersion(rpcVersion); InputStreamReader reader = new InputStreamReader(exchange.getRequestBody()); JsonParser parser = new JsonParser(); JsonObject remote = parser.parse(reader).getAsJsonObject(); String methodName = remote.get("methodName").getAsString(); Type collectionType = new TypeToken<Collection<Class<?>>>() { }.getType(); Collection<Class<?>> parameterTypes = gson.fromJson(remote.get("parameterTypes"), collectionType); JsonArray args = remote.get("arguments").getAsJsonArray(); Class<?>[] params = parameterTypes.toArray(new Class[parameterTypes.size()]); Object[] arguments = new Object[params.length]; for (int i = 0; i < params.length; i++) { Class<?> clazz = params[i]; Object argument = gson.fromJson(args.get(i), clazz); arguments[i] = argument; } RemoteInvocation remoteInvocation = new RemoteInvocation(methodName, params, arguments); RemoteInvocationResult result = invokeAndCreateResult(remoteInvocation, getProxy()); writeRemoteInvocationResult(exchange, result); exchange.close(); } catch (Throwable e) { e.printStackTrace(); } }
From source file:ch.uzh.supersede.feedbacklibrary.FeedbackActivity.java
License:Apache License
public void sendButtonClicked(View view) { if (baseURL != null && language != null) { // The mechanism models are updated with the view values for (MechanismView mechanismView : allMechanismViews) { mechanismView.updateModel(); }//w w w .j a v a 2s . c o m final ArrayList<String> messages = new ArrayList<>(); if (validateInput(allMechanisms, messages)) { if (fbAPI == null) { Retrofit rtf = new Retrofit.Builder().baseUrl(baseURL) .addConverterFactory(GsonConverterFactory.create()).build(); fbAPI = rtf.create(feedbackAPI.class); } Call<ResponseBody> checkUpAndRunning = fbAPI.pingRepository(); if (checkUpAndRunning != null) { checkUpAndRunning.enqueue(new Callback<ResponseBody>() { @Override public void onFailure(Call<ResponseBody> call, Throwable t) { Log.e(TAG, "Failed to ping the server. onFailure method called", t); DialogUtils .showInformationDialog(FeedbackActivity.this, new String[] { getResources() .getString(R.string.supersede_feedbacklibrary_error_text) }, true); } @Override public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { if (response.code() == 200) { Feedback feedback = new Feedback(allMechanisms); feedback.setTitle(getResources().getString( R.string.supersede_feedbacklibrary_feedback_title_text, System.currentTimeMillis())); feedback.setApplicationId(orchestratorConfiguration.getId()); feedback.setConfigurationId(activeConfiguration.getId()); feedback.setLanguage(language); feedback.setUserIdentification(Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID)); // The JSON string of the feedback GsonBuilder builder = new GsonBuilder(); builder.excludeFieldsWithoutExposeAnnotation(); builder.serializeNulls(); Gson gson = builder.create(); Type feedbackType = new TypeToken<Feedback>() { }.getType(); String feedbackJsonString = gson.toJson(feedback, feedbackType); RequestBody feedbackJSONPart = RequestBody .create(MediaType.parse("multipart/form-data"), feedbackJsonString); Map<String, RequestBody> files = new HashMap<>(); // Audio multipart List<AudioFeedback> audioFeedbackList = feedback.getAudioFeedbacks(); if (audioFeedbackList != null) { for (int pos = 0; pos < audioFeedbackList.size(); ++pos) { RequestBody requestBody = createRequestBody( new File(audioFeedbackList.get(pos).getAudioPath())); String fileName = audioFeedbackList.get(pos).getFileName(); String key = String.format("%1$s\"; filename=\"%2$s", audioFeedbackList.get(pos).getPartString() + String.valueOf(pos), fileName); files.put(key, requestBody); } } // Screenshots multipart List<ScreenshotFeedback> screenshotFeedbackList = feedback.getScreenshotFeedbacks(); if (screenshotFeedbackList != null) { for (int pos = 0; pos < screenshotFeedbackList.size(); ++pos) { RequestBody requestBody = createRequestBody( new File(screenshotFeedbackList.get(pos).getImagePath())); String fileName = screenshotFeedbackList.get(pos).getFileName(); String key = String.format("%1$s\"; filename=\"%2$s", screenshotFeedbackList.get(pos).getPartString() + String.valueOf(pos), fileName); files.put(key, requestBody); } } // Send the feedback Call<JsonObject> result = fbAPI.createFeedbackVariant(language, feedback.getApplicationId(), feedbackJSONPart, files); if (result != null) { result.enqueue(new Callback<JsonObject>() { @Override public void onFailure(Call<JsonObject> call, Throwable t) { Log.e(TAG, "Failed to send the feedback. onFailure method called", t); DialogUtils.showInformationDialog(FeedbackActivity.this, new String[] { getResources().getString( R.string.supersede_feedbacklibrary_error_text) }, true); } @Override public void onResponse(Call<JsonObject> call, Response<JsonObject> response) { if (response.code() == 201) { Log.i(TAG, "Feedback successfully sent"); Toast toast = Toast.makeText(getApplicationContext(), getResources().getString( R.string.supersede_feedbacklibrary_success_text), Toast.LENGTH_SHORT); toast.show(); } else { Log.e(TAG, "Failed to send the feedback. Response code == " + response.code()); DialogUtils.showInformationDialog(FeedbackActivity.this, new String[] { getResources().getString( R.string.supersede_feedbacklibrary_error_text) }, true); } } }); } else { Log.e(TAG, "Failed to send the feebdkack. Call<JsonObject> result is null"); DialogUtils.showInformationDialog(FeedbackActivity.this, new String[] { getResources() .getString(R.string.supersede_feedbacklibrary_error_text) }, true); } } else { Log.e(TAG, "The server is not up and running. Response code == " + response.code()); DialogUtils .showInformationDialog(FeedbackActivity.this, new String[] { getResources() .getString(R.string.supersede_feedbacklibrary_error_text) }, true); } } }); } else { Log.e(TAG, "Failed to ping the server. Call<ResponseBody> checkUpAndRunning result is null"); DialogUtils.showInformationDialog(FeedbackActivity.this, new String[] { getResources().getString(R.string.supersede_feedbacklibrary_error_text) }, true); } } else { Log.v(TAG, "Validation of the mechanism failed"); DialogUtils.showInformationDialog(this, messages.toArray(new String[messages.size()]), false); } } else { if (baseURL == null) { Log.e(TAG, "Failed to send the feedback. baseURL is null"); } else { Log.e(TAG, "Failed to send the feedback. language is null"); } DialogUtils.showInformationDialog(this, new String[] { getResources().getString(R.string.supersede_feedbacklibrary_error_text) }, true); } }
From source file:client.ClientEnd.java
public void transfRoomsbkd(JSONObject jObject) { String message = jObject.getString("RoomsBookedToday"); Type type = new TypeToken<ArrayList<RoomsBookedView>>() { }.getType();/* ww w. ja v a 2 s .c o m*/ ArrayList<RoomsBookedView> bookingsRecieved = gson.fromJson(message, type); AdminController.setRoomsBooked(bookingsRecieved); //LogginController.setRoomsBooked(bookingsRecieved); //LogginController.switchView(); }
From source file:client.ClientEnd.java
public void transfUsrsRoomsBooked(JSONObject usersRooms) { String message = usersRooms.getString("YourRoomsBookedToday"); Type type = new TypeToken<ArrayList<UsersBookedRooms>>() { }.getType();//from ww w. ja v a 2s . c o m ArrayList<UsersBookedRooms> allBookedRooms = gson.fromJson(message, type); for (UsersBookedRooms room : allBookedRooms) { System.out.println(room); //ClientController.bookings.getItems().add(room); } UserController.setUsersBookedRooms(allBookedRooms); //LogginController.usersBookedRooms = allBookedRooms; //LogginController.switchView(); }