@Override public void fromJson(String jsonStr) { System.out.println("FROM JSON:" + jsonStr); JsonFactory factory = new JreJsonFactory(); try { JsonObject json = factory.parse(jsonStr); backgroundColor = json.getString("bgColor"); foregroundColor = json.getString("fgColor"); borderStyle = BorderStyle.getFromStyle(json.getString("borderStyle")); borderWidth = (int) json.getNumber("borderWidth"); borderColor = json.getString("borderColor"); borderRounded = (int) json.getNumber("borderRounded"); int[] margins = parseIntJSONArr(json.getArray("margins")); setMargin(margins[0], margins[1], margins[2], margins[3]); int[] paddings = parseIntJSONArr(json.getArray("paddings")); setPadding(paddings[0], paddings[1], paddings[2], paddings[3]); fontFamily = json.getString("fontFamily"); fontSize = json.getString("fontSize"); fontBolded = json.getBoolean("fontBolded"); fontItalic = json.getBoolean("fontItalic"); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
@AssistedInject public JsonRpcRequest( @Assisted("message") String message, JsonFactory jsonFactory, JsonRpcFactory jsonRpcFactory) { checkNotNull(message, "Message must not be null"); checkArgument(!message.isEmpty(), "Message must not be empty"); this.jsonFactory = jsonFactory; JsonObject jsonObject = jsonFactory.parse(message); if (jsonObject.hasKey("id")) { JsonValue jsonValue = jsonObject.get("id"); if (jsonValue.getType().equals(JsonType.STRING)) { id = jsonValue.asString(); } else { id = Double.toString(jsonValue.asNumber()); } } else { id = null; } method = jsonObject.getString("method"); if (jsonObject.hasKey("params")) { params = jsonRpcFactory.createParams(jsonObject.get("params").toJson()); } else { params = null; } }
@AssistedInject public JsonRpcResult( @Assisted("result") Object result, DtoFactory dtoFactory, JsonFactory jsonFactory) { this.jsonFactory = jsonFactory; this.dtoFactory = dtoFactory; if (result instanceof String) { this.result = jsonFactory.create((String) result); } else if (result instanceof Double) { this.result = jsonFactory.create((Double) result); } else if (result instanceof Boolean) { this.result = jsonFactory.create((Boolean) result); } else if (result == null) { this.result = jsonFactory.createObject(); } else { this.result = jsonFactory.parse(result.toString()); } }
@AssistedInject public JsonRpcResult( @Assisted("message") String message, JsonFactory jsonFactory, DtoFactory dtoFactory) { checkNotNull(message, "Message must not be null"); checkArgument(!message.isEmpty(), "Message must not be empty"); this.jsonFactory = jsonFactory; this.dtoFactory = dtoFactory; JsonValue jsonValue = jsonFactory.parse(message); if (jsonValue.getType().equals(JsonType.ARRAY)) { JsonArray jsonArray = jsonFactory.parse(message); this.resultList = new ArrayList<>(jsonArray.length()); for (int i = 0; i < jsonArray.length(); i++) { this.resultList.add(i, jsonArray.get(i)); } } else { this.result = jsonFactory.parse(message); } }
public JsonValue toJsonValue() { if (result != null) { return result; } else { JsonArray array = jsonFactory.createArray(); for (int i = 0; i < resultList.size(); i++) { array.set(i, resultList.get(i)); } return array; } }
@AssistedInject public JsonRpcResult( @Assisted("result") List<?> result, JsonFactory jsonFactory, DtoFactory dtoFactory) { checkNotNull(result, "Result must not be null"); checkArgument(!result.isEmpty(), "Result must not be empty"); this.jsonFactory = jsonFactory; this.dtoFactory = dtoFactory; this.resultList = new ArrayList<>(result.size()); for (int i = 0; i < result.size(); i++) { Object item = result.get(i); if (item instanceof String) { resultList.add(i, jsonFactory.create((String) item)); } else if (item instanceof Double) { resultList.add(i, jsonFactory.create((Double) item)); } else if (item instanceof Boolean) { resultList.add(i, jsonFactory.create((Boolean) item)); } else { resultList.add(i, jsonFactory.parse(item.toString())); } } }
public JsonObject toJsonObject() { JsonObject request = jsonFactory.createObject(); request.put("jsonrpc", "2.0"); request.put("method", method); if (hasId()) { request.put("id", id); } if (hasParams()) { request.put("params", params.toJsonValue()); } return request; }