List of usage examples for com.google.gson JsonPrimitive JsonPrimitive
public JsonPrimitive(Character c)
From source file:bind.JsonTreeWriter.java
License:Apache License
@Override public JsonWriter value(Number value) throws IOException { if (value == null) { return nullValue(); }// www. ja v a 2 s . co m if (!isLenient()) { double d = value.doubleValue(); if (Double.isNaN(d) || Double.isInfinite(d)) { throw new IllegalArgumentException("JSON forbids NaN and infinities: " + value); } } put(new JsonPrimitive(value)); return this; }
From source file:blockplus.exports.ContextRepresentation.java
License:Open Source License
public JsonElement encodeBoard() { final JsonObject boardState = new JsonObject(); final JsonObject meta = new JsonObject(); final JsonObject data = new JsonObject(); final Board board = this.context.board(); final int rows = board.rows(); final int columns = board.columns(); meta.addProperty("rows", rows); meta.addProperty("columns", columns); final Set<Colors> colors = board.getColors(); for (final Colors color : colors) { final JsonArray jsonArray = new JsonArray(); for (final IPosition position : board.getSelves(color)) jsonArray.add(new JsonPrimitive(columns * position.row() + position.column() % rows)); // TODO extract method data.add(color.toString(), jsonArray); }//from w w w .j av a2 s . c o m boardState.add("dimension", meta); boardState.add("cells", data); return boardState; }
From source file:blockplus.exports.ContextRepresentation.java
License:Open Source License
public JsonElement encodePieces() { final JsonObject data = new JsonObject(); final Context context = this.context; for (final Entry<Colors, Side> sideEntry : context.sides()) { final Colors color = sideEntry.getKey(); final Side side = sideEntry.getValue(); int bits = 1; final Pieces remainingPieces = side.remainingPieces(); for (final Entry<Polyomino, Integer> entry : remainingPieces) { if (entry.getKey().ordinal() == 0) continue; bits = bits << 1 | entry.getValue(); }/*from www. j av a 2 s .co m*/ data.add(color.toString(), new JsonPrimitive(bits)); } return data; }
From source file:blockplus.transport.VirtualClient.java
License:Open Source License
@Override public void onMessage(final String message) { final JsonParser jsonParser = new JsonParser(); final JsonObject jsonObject = jsonParser.parse(message).getAsJsonObject(); final String type = jsonObject.get("type").getAsString(); if (type.equals("game")) { final JsonObject data = jsonObject.get("data").getAsJsonObject(); final int k = data.get("players").getAsInt(); this.color = Colors.values()[k - 1].toString(); }//ww w .jav a 2 s. c o m if (type.equals("update")) { final JsonObject data = jsonObject.get("data").getAsJsonObject(); // System.out.println(data); final String color = data.get("color").getAsString(); if (color.equals(this.color)) { if (data.get("isTerminal").getAsBoolean()) { System.out.println("Game Over"); } else { final Colors side = Colors.valueOf(this.color); final BoardEncoding boardEncoding = new BoardEncoding(); final Board board = boardEncoding.decode(data.get("board").getAsJsonObject()); // final OptionsEncoding optionsEncoding = new OptionsEncoding(); // final Options options = optionsEncoding.decode(data.get("options").getAsJsonObject()); final SidesEncoding sidesEncoding = new SidesEncoding(); final Sides sides = sidesEncoding.decode(data.get("pieces").getAsJsonObject()); final Context context = new Context(side, sides, board); final AI3 ai = new AI3(); // final IPosition position = this.testAI(side, board, options); // final Set<IPosition> positions = this.moveSupplier(options, position); System.out.println(); System.out.println(side); final Set<IPosition> positions = ai.get(context); final JsonArray jsonArray = new JsonArray(); for (final IPosition iPosition : positions) jsonArray.add(new JsonPrimitive(20 * iPosition.row() + iPosition.column() % 20)); // TODO !!! final MoveSubmit moveSubmit = new MoveSubmit(jsonArray); try { this.send(moveSubmit); } catch (final Exception e) { } } } } }
From source file:br.com.caelum.vraptor.serialization.gson.CalendarGsonConverter.java
License:Open Source License
@Override public JsonElement serialize(Calendar calendar, Type typeOfSrc, JsonSerializationContext context) { return new JsonPrimitive(DatatypeConverter.printDateTime(calendar)); }
From source file:br.com.caelum.vraptor.serialization.gson.DateGsonConverter.java
License:Open Source License
@Override public JsonElement serialize(Date date, Type typeOfSrc, JsonSerializationContext context) { String dateString = iso8601Format.format(date); return new JsonPrimitive(dateString); }
From source file:br.edu.unochapeco.unoheartserver.util.CustomGsonBuilder.java
public static Gson getInstance() { if (gson == null) { gson = new GsonBuilder().registerTypeAdapter(LocalDate.class, (JsonSerializer<LocalDate>) new JsonSerializer<LocalDate>() { @Override//from ww w .j a va 2 s. c o m public JsonElement serialize(LocalDate t, java.lang.reflect.Type type, JsonSerializationContext jsc) { if (t != null) { DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); return new JsonPrimitive(t.format(dateTimeFormatter)); } else { return null; } } }).registerTypeAdapter(LocalDate.class, (JsonDeserializer<LocalDate>) new JsonDeserializer<LocalDate>() { @Override public LocalDate deserialize(JsonElement je, java.lang.reflect.Type type, JsonDeserializationContext jdc) { if (je.getAsJsonPrimitive().getAsString() != null) { return LocalDate.parse( je.getAsJsonPrimitive().getAsString().substring(0, 10).trim(), DateTimeFormatter.ofPattern("yyyy-MM-dd")); } else { return null; } } }) .registerTypeAdapter(LocalDateTime.class, (JsonSerializer<LocalDateTime>) new JsonSerializer<LocalDateTime>() { @Override public JsonElement serialize(LocalDateTime t, java.lang.reflect.Type type, JsonSerializationContext jsc) { if (t != null) { DateTimeFormatter dateTimeFormatter = DateTimeFormatter .ofPattern("yyyy-MM-dd'T'HH:mm:ss"); return new JsonPrimitive(t.format(dateTimeFormatter)); } else { return null; } } }) .registerTypeAdapter(LocalDateTime.class, (JsonDeserializer<LocalDateTime>) (JsonElement je, java.lang.reflect.Type type, JsonDeserializationContext jdc) -> { if (je.getAsJsonPrimitive().getAsString() != null) { return LocalDateTime.parse(je.getAsJsonPrimitive().getAsString().trim(), DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss")); } else { return null; } }).create(); } return gson; }
From source file:br.ufmg.hc.telessaude.webservices.mobile.utils.GsonUtils.java
public static Gson getInstanceWithStringDateAdapter() { GsonBuilder builder = new GsonBuilder(); final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); // final SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy HH:mm:ss a"); builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() { @Override/* w ww . j a va 2s . c om*/ public Date deserialize(JsonElement je, Type type, JsonDeserializationContext jdc) throws JsonParseException { String data_str = ""; Date data = null; try { data_str = je.getAsJsonPrimitive().getAsString(); if (data_str.replaceAll("[\\-\\d]", "").isEmpty()) { data = new Date(Long.parseLong(data_str)); } else { if (data_str.length() == 12) { data = new SimpleDateFormat("MMM dd, yyyy").parse(data_str); } else if (data_str.length() == 11) { data = new SimpleDateFormat("MMM d, yyyy").parse(data_str); } else if (data_str.length() == 10) { data = new SimpleDateFormat("dd/MM/yyyy").parse(data_str); } else { data = sdf.parse(data_str); } } } catch (ParseException ex) { Logger.getLogger(GsonUtils.class.getName()).log(Level.SEVERE, null, ex); } return data; } }); builder.registerTypeAdapter(Date.class, new JsonSerializer<Date>() { @Override public JsonElement serialize(Date t, Type type, JsonSerializationContext jsc) { if (t == null) { return new JsonPrimitive((String) null); } return new JsonPrimitive(sdf.format(t)); } }); builder.registerTypeAdapter(java.sql.Date.class, new JsonSerializer<java.sql.Date>() { @Override public JsonElement serialize(java.sql.Date t, Type type, JsonSerializationContext jsc) { if (t == null) { return new JsonPrimitive((String) null); } return new JsonPrimitive(sdf.format(t)); } }); return builder.create(); }
From source file:br.ufsc.inf.ine5646.rest.CalculadoraServlet.java
public String handleRequest(String urlParams) { String[] params = urlParams.split("/"); JsonArray ja = new JsonArray(); try {/* w w w.j a v a 2s. com*/ if (params.length != 3) { ja.add(new JsonPrimitive("erro")); ja.add(new JsonPrimitive("nmero de parmetros incorreto! Exemplo: soma/5/8")); } else if (!params[0].equals("soma") && !params[0].equals("subt") && !params[0].equals("mult") && !params[0].equals("divi")) { ja.add(new JsonPrimitive("erro")); ja.add(new JsonPrimitive("Operao deve ser uma destas: soma,subt,mult,divi")); } else if (params[0].equals("divi") && Double.parseDouble(params[2]) == 0) { ja.add(new JsonPrimitive("erro")); ja.add(new JsonPrimitive("diviso por zero")); } else { double num1 = Double.parseDouble(params[1]); double num2 = Double.parseDouble(params[2]); ja.add(new JsonPrimitive("ok")); switch (params[0]) { case "soma": ja.add(new JsonPrimitive(num1 + num2)); break; case "subt": ja.add(new JsonPrimitive(num1 - num2)); break; case "mult": ja.add(new JsonPrimitive(num1 * num2)); break; case "divi": ja.add(new JsonPrimitive(num1 / num2)); break; default: break; } } } catch (NumberFormatException ex) { ja.add(new JsonPrimitive("erro")); ja.add(new JsonPrimitive(params[1] + " e/ou " + params[2] + " no nmero")); } return ja.toString(); }
From source file:buri.ddmsence.util.Util.java
License:Open Source License
/** * Converts a list of items into a JSON Array. * //from www .j av a 2 s .c om * @param values the values * @return a JSON array, with the values in the same order */ public static JsonArray getJSONArray(List<?> values) { JsonArray array = new JsonArray(); for (Iterator iterator = values.iterator(); iterator.hasNext();) { Object value = (Object) iterator.next(); if (value instanceof Double) { array.add(new JsonPrimitive((Double) value)); } else if (value instanceof String) { array.add(new JsonPrimitive((String) value)); } else if (value instanceof AbstractBaseComponent) { array.add(((AbstractBaseComponent) value).getJSONObject()); } else { throw new IllegalArgumentException("Unexpected class for JSON property: " + value); } } return (array); }