List of usage examples for com.google.gson Gson toJson
@SuppressWarnings("unchecked") public void toJson(Object src, Type typeOfSrc, JsonWriter writer) throws JsonIOException
From source file:ContattiSQL.java
protected void doGetSingle(String contattoId, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Connection con = getConnection(); Statement stmt = null;/*from w w w .j a v a 2 s . c o m*/ ResultSet rs = null; try { stmt = con.createStatement(); } catch (SQLException ex) { Logger.getLogger(ContattiSQL.class.getName()).log(Level.SEVERE, null, ex); } try { rs = stmt.executeQuery("SELECT ContattoId, Cognome, Nome, EMail FROM dbo.Contatti" + " Where ContattoId = " + contattoId); rs.next(); ContattoViewModel viewModel = new ContattoViewModel(); viewModel.cognome = rs.getString(2); viewModel.nome = rs.getString(3); viewModel.email = rs.getString(4); JsonWriter writer = new JsonWriter(response.getWriter()); Gson gson = new Gson(); gson.toJson(viewModel, ContattoViewModel.class, writer); } catch (SQLException ex) { Logger.getLogger(ContattiSQL.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:ContattiSQL.java
/** * Handles the HTTP <code>POST</code> method. * * @param request servlet request//from w w w . j av a 2 s.co m * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { BufferedReader reader = request.getReader(); Gson gson = new Gson(); ContattoViewModel contatto = (ContattoViewModel) gson.fromJson(reader, ContattoViewModel.class); try { try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance(); } catch (ClassNotFoundException ex) { Logger.getLogger(ContattiSQL.class.getName()).log(Level.SEVERE, null, ex); } catch (InstantiationException ex) { Logger.getLogger(ContattiSQL.class.getName()).log(Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { Logger.getLogger(ContattiSQL.class.getName()).log(Level.SEVERE, null, ex); } Connection con = DriverManager.getConnection( "jdbc:sqlserver://127.0.0.1;instanceName=SQLSERVER2012;databaseName=ITS2014;user=its2014;password=its2014;"); Statement stmt = con.createStatement(); String sql = "insert into dbo.Contatti (Cognome, Nome, EMail) " + "VALUES(" + "'" + contatto.cognome + "'" + ", '" + contatto.nome + "'" + ", '" + contatto.email + "'" + ")"; stmt.execute(sql); JsonWriter writer = new JsonWriter(response.getWriter()); gson.toJson(Risposta.OK, Risposta.class, writer); con.close(); } catch (SQLException ex) { Logger.getLogger(ContattiSQL.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:BundleTypeAdapterFactory.java
License:Apache License
@SuppressWarnings("unchecked") @Override/* w w w . j av a 2s . c o m*/ public <T> TypeAdapter<T> create(final Gson gson, TypeToken<T> type) { if (!Bundle.class.isAssignableFrom(type.getRawType())) { return null; } return (TypeAdapter<T>) new TypeAdapter<Bundle>() { @Override public void write(JsonWriter out, Bundle bundle) throws IOException { if (bundle == null) { out.nullValue(); return; } out.beginObject(); for (String key : bundle.keySet()) { out.name(key); Object value = bundle.get(key); if (value == null) { out.nullValue(); } else { gson.toJson(value, value.getClass(), out); } } out.endObject(); } @Override public Bundle read(JsonReader in) throws IOException { switch (in.peek()) { case NULL: in.nextNull(); return null; case BEGIN_OBJECT: return toBundle(readObject(in)); default: throw new IOException("expecting object: " + in.getPath()); } } private Bundle toBundle(List<Pair<String, Object>> values) throws IOException { Bundle bundle = new Bundle(); for (Pair<String, Object> entry : values) { String key = entry.first; Object value = entry.second; if (value instanceof String) { bundle.putString(key, (String) value); } else if (value instanceof Integer) { bundle.putInt(key, ((Integer) value).intValue()); } else if (value instanceof Long) { bundle.putLong(key, ((Long) value).longValue()); } else if (value instanceof Double) { bundle.putDouble(key, ((Double) value).doubleValue()); } else if (value instanceof Parcelable) { bundle.putParcelable(key, (Parcelable) value); } else if (value instanceof List) { List<Pair<String, Object>> objectValues = (List<Pair<String, Object>>) value; Bundle subBundle = toBundle(objectValues); bundle.putParcelable(key, subBundle); } else { throw new IOException("Unparcelable key, value: " + key + ", " + value); } } return bundle; } private List<Pair<String, Object>> readObject(JsonReader in) throws IOException { List<Pair<String, Object>> object = new ArrayList<Pair<String, Object>>(); in.beginObject(); while (in.peek() != JsonToken.END_OBJECT) { switch (in.peek()) { case NAME: String name = in.nextName(); Object value = readValue(in); object.add(new Pair<String, Object>(name, value)); break; case END_OBJECT: break; default: throw new IOException("expecting object: " + in.getPath()); } } in.endObject(); return object; } private Object readValue(JsonReader in) throws IOException { switch (in.peek()) { case BEGIN_ARRAY: return readArray(in); case BEGIN_OBJECT: return readObject(in); case BOOLEAN: return in.nextBoolean(); case NULL: in.nextNull(); return null; case NUMBER: return readNumber(in); case STRING: return in.nextString(); default: throw new IOException("expecting value: " + in.getPath()); } } private Object readNumber(JsonReader in) throws IOException { double doubleValue = in.nextDouble(); if (doubleValue - Math.ceil(doubleValue) == 0) { long longValue = (long) doubleValue; if (longValue >= Integer.MIN_VALUE && longValue <= Integer.MAX_VALUE) { return (int) longValue; } return longValue; } return doubleValue; } @SuppressWarnings("rawtypes") private List readArray(JsonReader in) throws IOException { List list = new ArrayList(); in.beginArray(); while (in.peek() != JsonToken.END_ARRAY) { Object element = readValue(in); list.add(element); } in.endArray(); return list; } }; }
From source file:ScoresServlet.java
/** * Handles the HTTP <code>POST</code> method. * * @param request servlet request//from w w w .ja v a 2s . co m * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //processRequest(request, response); BufferedReader reader = request.getReader(); Gson gson = new Gson(); ScoreViewModel contatto = (ScoreViewModel) gson.fromJson(reader, ScoreViewModel.class); try { try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance(); } catch (ClassNotFoundException ex) { Logger.getLogger(ScoresServlet.class.getName()).log(Level.SEVERE, null, ex); } catch (InstantiationException ex) { Logger.getLogger(ScoresServlet.class.getName()).log(Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { Logger.getLogger(ScoresServlet.class.getName()).log(Level.SEVERE, null, ex); } Connection con = DriverManager.getConnection( "jdbc:sqlserver://127.0.0.1;instanceName=SQLSERVER2012;databaseName=FlappyBird;user=flappybird;password=flappybird;"); Statement stmt = con.createStatement(); String sql = "insert into dbo.Scores (Username, Score) " + "VALUES(" + "'" + contatto.username + "'" + ", '" + contatto.score + "'" + ")"; stmt.execute(sql); JsonWriter writer = new JsonWriter(response.getWriter()); gson.toJson(Risposta.OK, Risposta.class, writer); con.close(); } catch (SQLException ex) { Logger.getLogger(ScoresServlet.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:ca.ualberta.cs.asn1.CounterSaveModel.java
License:Apache License
public void saveData(List<CounterModel> list) { try {// w ww . j a v a2 s. c o m FileOutputStream fos = fileContext.openFileOutput(FILE_NAME, Context.MODE_PRIVATE); OutputStreamWriter osw = new OutputStreamWriter(fos); Gson gson = new Gson(); Type fooType = new TypeToken<ArrayList<CounterModel>>() { }.getType(); gson.toJson(list, fooType, osw); osw.close(); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }