List of usage examples for com.google.gson.reflect TypeToken TypeToken
@SuppressWarnings("unchecked") protected TypeToken()
From source file:at.tugraz.kmi.medokyservice.fca.util.ImportExport.java
License:Open Source License
private <E extends FCAAbstract> JsonElement abstracts2JSon(Class<E> type) { BlockingDeque<E> objects = Database.getInstance().getAll(type); JsonObject block = new JsonObject(); for (E o : objects) { JsonObject jso = prepare(o);/*from w w w. j ava2s .c om*/ jso.addProperty(CID, o.getCreationId()); LinkedList<Long> loIds = new LinkedList<Long>(); LinkedList<Long> loByLearnerIds = new LinkedList<Long>(); for (LearningObject lo : o.getLearningObjects()) { loIds.add(lo.getId()); } for (LearningObject lo : o.getLearningObjectsByLearners()) { loByLearnerIds.add(lo.getId()); } jso.add(SECTION_LO, gson.toJsonTree(loIds, new TypeToken<List<Long>>() { }.getType())); jso.add(SECTION_LO_L, gson.toJsonTree(loByLearnerIds, new TypeToken<List<Long>>() { }.getType())); block.add(Long.toString(o.getId()), jso); } return block; }
From source file:at.tugraz.kmi.medokyservice.fca.util.ImportExport.java
License:Open Source License
private JsonElement metadata2JSON() { BlockingDeque<FCAItemMetadata> lobjects = Database.getInstance().getAll(FCAItemMetadata.class); JsonObject block = new JsonObject(); for (FCAItemMetadata o : lobjects) { JsonObject jso = prepare(o);//from w w w.java 2s.co m jso.addProperty(O_ID, o.getItemID()); LinkedList<Long> loIds = new LinkedList<Long>(); for (LearningObject lo : o.getLearningObjects()) { if (lo != null) loIds.add(lo.getId()); } LinkedList<Long> loByLernerIds = new LinkedList<Long>(); for (LearningObject lo : o.getLearningObjectByLearner()) { if (lo != null) loByLernerIds.add(lo.getId()); } jso.add(SECTION_LO, gson.toJsonTree(loIds, new TypeToken<List<Long>>() { }.getType())); jso.add(SECTION_LO_L, gson.toJsonTree(loByLernerIds, new TypeToken<List<Long>>() { }.getType())); block.add(Long.toString(o.getId()), jso); } return block; }
From source file:at.tugraz.kmi.medokyservice.fca.util.ImportExport.java
License:Open Source License
private JsonElement domains2JSON() { BlockingDeque<Domain> domains = Database.getInstance().getAll(Domain.class); JsonArray jsD = new JsonArray(); for (Domain d : domains) { IncidenceMatrix mat = d.getMapping(); Map<FCAObject, Set<FCAAttribute>> mapping = mat.getObjects(); JsonObject jso = prepare(d);/*from w ww. j ava2s . c om*/ JsonObject jsM = new JsonObject(); for (long mId : mat.getItemMetadata().keySet()) { jsM.addProperty(Long.toString(mId), mat.getItemMetadata().get(mId).getId()); } jso.add(SECTION_M, jsM); List<Long> ownerIDs = new LinkedList<Long>(); for (User o : d.getOwners()) { ownerIDs.add(o.getId()); } jso.add(OWNER, gson.toJsonTree(ownerIDs, new TypeToken<List<Long>>() { }.getType())); jso.addProperty(GLOBAL, d.isGlobal()); jso.addProperty(APPROVED, d.isApproved()); JsonObject jsMapping = new JsonObject(); for (FCAObject o : mapping.keySet()) { LinkedList<Long> aIds = new LinkedList<Long>(); for (FCAAttribute a : mapping.get(o)) { aIds.add(a.getId()); } jsMapping.add(Long.toString(o.getId()), gson.toJsonTree(aIds, new TypeToken<List<Long>>() { }.getType())); } jso.add(MAPPING, jsMapping); jsD.add(jso); } return jsD; }
From source file:at.tugraz.kmi.medokyservice.fca.util.ImportExport.java
License:Open Source License
private JsonElement courses2JSON() { BlockingDeque<Course> courses = Database.getInstance().getAll(Course.class); JsonArray jsC = new JsonArray(); for (Course o : courses) { JsonObject jso = prepare(o);/*ww w. j a va2 s .c om*/ Set<Domain> domains = o.getDomains(); LinkedList<Long> dIds = new LinkedList<Long>(); for (Domain d : domains) dIds.add(d.getId()); jso.add(SECTION_D, gson.toJsonTree(dIds, new TypeToken<List<Long>>() { }.getType())); jso.addProperty(E_CID, o.getExternalCourseID()); List<Long> participants = new LinkedList<Long>(); for (User u : o.getParticipants()) participants.add(u.getId()); jso.add(PARTICIPANTS, gson.toJsonTree(participants, new TypeToken<List<Long>>() { }.getType())); jsC.add(jso); } return jsC; }
From source file:au.com.breakpoint.hedron.indexer.Indexer.java
License:Apache License
private void getKeywords(final IndexInfo index) { final Type genericType = new TypeToken<TreeMap<String, String[]>>() { }.getType();//w w w .j a va 2 s. c o m final Map<String, String[]> keywordSets = JsonUtil.fromJsonFile(m_appConfig.getKeywordFilename(), genericType, null); index.setKeywordSets(keywordSets); for (final Entry<String, String[]> ks : keywordSets.entrySet()) { for (final String keyword : ks.getValue()) { final KeywordReference kr = new KeywordReference(); index.m_keywords.put(keyword, kr); } } }
From source file:augsburg.se.alltagsguide.common.AvailableLanguage.java
License:Open Source License
@NonNull public static List<AvailableLanguage> fromJson(@NonNull JsonElement elem) { List<AvailableLanguage> languages = new ArrayList<>(); try {/*from ww w. j ava2s . c o m*/ Map<String, Integer> languagesMap = new Gson().fromJson(elem, new TypeToken<Map<String, Integer>>() { }.getType()); Log.d("AvailableLanguages", "LanguagesMapSize is: " + languagesMap.size()); for (Map.Entry<String, Integer> entry : languagesMap.entrySet()) { languages.add(new AvailableLanguage(entry.getKey(), entry.getValue())); } } catch (Exception e) { //ignore Log.e("AvailableLanguage", "fromJson throw an exception"); } return languages; }
From source file:augsburg.se.alltagsguide.MainModule.java
License:Open Source License
@Provides Gson gson() {// w w w .j av a 2s .c o m return new GsonBuilder().registerTypeAdapter(new TypeToken<List<Page>>() { }.getType(), new PageSerializer()).registerTypeAdapter(new TypeToken<List<Language>>() { }.getType(), new LanguageSerializer()).registerTypeAdapter(new TypeToken<List<Location>>() { }.getType(), new LocationSerializer()).registerTypeAdapter(new TypeToken<List<EventPage>>() { }.getType(), new EventPageSerializer()).create(); }
From source file:backend.ws.AppointmentWS.java
public List<Appointment> getHPAppointments(int idHealthProfessional) { List<Appointment> aList = null; List<NameValuePair> params = new ArrayList<>(); //array com os params necessrios para registar um terapeuta params.add(new BasicNameValuePair("idHealthProfessional", String.valueOf(idHealthProfessional))); try {// w w w .j a va 2s . c o m responseWS = wrapperWS.sendRequest("Appointment", "getHPAppointments", params); //efetua o pedido ao WS String jsonResp = wrapperWS.readResponse(responseWS); //Passa a responseWS para uma string int httpResponseCod = responseWS.getStatusLine().getStatusCode(); if (httpResponseCod != 200) { Validation v = gson.fromJson(jsonResp, Validation.class); //Converso do objecto Json para o objecto Java log.error("\n\tCod: " + v.getCod() + "\tMsg: " + v.getMsg()); throw new RuntimeException("Ocorreu um erro ao aceder aos dados da consulta"); } Type type = new TypeToken<List<Appointment>>() { }.getType(); //tipo do para o qual queros retornar a responseWS Json aList = gson.fromJson(jsonResp, type); } catch (RuntimeException e) { log.error("\n\t" + e.getMessage()); throw new RuntimeException(e.getMessage()); } log.debug("\n\t Appointment data access success"); log.debug("\n\tApoints : " + aList.toString()); return aList; }
From source file:backend.ws.AppointmentWS.java
public List<Appointment> getAppointmentByIdHPDate(int id, String date) { List<Appointment> apList = null; List<NameValuePair> params = new ArrayList<>(); //array com os params necessrios para registar um terapeuta params.add(new BasicNameValuePair("idHealthProfessional", String.valueOf(id))); params.add(new BasicNameValuePair("dateAppointment", date)); try {/* w ww . j a v a 2 s . co m*/ responseWS = wrapperWS.sendRequest("Appointment", "getAppointmentByIdHPDate", params); //efetua o pedido ao WS String jsonResp = wrapperWS.readResponse(responseWS); //Passa a responseWS para uma string int httpResponseCod = responseWS.getStatusLine().getStatusCode(); if (httpResponseCod != 200) { Validation v = gson.fromJson(jsonResp, Validation.class); //Converso do objecto Json para o objecto Java log.error("\n\tCod: " + v.getCod() + "\tMsg: " + v.getMsg()); throw new RuntimeException("Ocorreu um erro ao aceder aos dados da consulta"); } Type type = new TypeToken<List<Appointment>>() { }.getType(); //tipo do para o qual queros retornar a responseWS Json apList = gson.fromJson(jsonResp, type); } catch (RuntimeException e) { log.error("\n\t" + e.getMessage()); throw new RuntimeException(e.getLocalizedMessage()); } log.debug("\n\t Appointment data access success"); log.debug("\n\tHPs : " + apList.toString()); return apList; }
From source file:backend.ws.AssignExerciseWS.java
public List<AssignExercise> getAssignExerciseByIdBlock(int idB) { List<AssignExercise> exList = null; List<NameValuePair> params = new ArrayList<>(); //array com os params necessrios para registar um terapeuta params.add(new BasicNameValuePair("idBlock", String.valueOf(idB))); try {// w w w. ja va 2 s. c o m responseWS = wrapperWS.sendRequest("AssignExercise", "getAssignExerciseByIdBlock", params); //efetua o pedido ao WS String jsonResp = wrapperWS.readResponse(responseWS); //Passa a responseWS para uma string int httpResponseCod = responseWS.getStatusLine().getStatusCode(); if (httpResponseCod != 200) { Validation v = gson.fromJson(jsonResp, Validation.class); //Converso do objecto Json para o objecto Java log.error("\n\tCod: " + v.getCod() + "\tMsg: " + v.getMsg()); throw new RuntimeException("Ocorreu um erro ao aceder aos dados dos Exerccios Atribuidos"); } Type type = new TypeToken<List<AssignExercise>>() { }.getType(); //tipo do para o qual queros retornar a responseWS Json exList = gson.fromJson(jsonResp, type); } catch (RuntimeException e) { log.error("\n\t" + e.getMessage()); throw new RuntimeException(e.getMessage()); } log.debug("\n\tAssignExercise data access success"); log.debug("\n\tPs : " + exList.toString()); return exList; }