Java Code Examples for com.google.gson.GsonBuilder#setVersion()
The following examples show how to use
com.google.gson.GsonBuilder#setVersion() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GsonHelper.java From cloudstack with Apache License 2.0 | 6 votes |
static Gson setDefaultGsonConfig(GsonBuilder builder) { builder.setVersion(1.5); InterfaceTypeAdaptor<DataStoreTO> dsAdaptor = new InterfaceTypeAdaptor<DataStoreTO>(); builder.registerTypeAdapter(DataStoreTO.class, dsAdaptor); InterfaceTypeAdaptor<DataTO> dtAdaptor = new InterfaceTypeAdaptor<DataTO>(); builder.registerTypeAdapter(DataTO.class, dtAdaptor); ArrayTypeAdaptor<Command> cmdAdaptor = new ArrayTypeAdaptor<Command>(); builder.registerTypeAdapter(Command[].class, cmdAdaptor); ArrayTypeAdaptor<Answer> ansAdaptor = new ArrayTypeAdaptor<Answer>(); builder.registerTypeAdapter(Answer[].class, ansAdaptor); builder.registerTypeAdapter(new TypeToken<List<PortConfig>>() { }.getType(), new PortConfigListTypeAdaptor()); builder.registerTypeAdapter(new TypeToken<Pair<Long, Long>>() { }.getType(), new NwGroupsCommandTypeAdaptor()); Gson gson = builder.create(); dsAdaptor.initGson(gson); dtAdaptor.initGson(gson); cmdAdaptor.initGson(gson); ansAdaptor.initGson(gson); return gson; }
Example 2
Source File: ConsoleProxyManagerImpl.java From cosmic with Apache License 2.0 | 5 votes |
public void onLoadAnswer(final ConsoleProxyLoadAnswer answer) { if (answer.getDetails() == null) { return; } ConsoleProxyStatus status = null; final GsonBuilder gb = new GsonBuilder(); gb.setVersion(1.3); final Gson gson = gb.create(); status = gson.fromJson(answer.getDetails(), ConsoleProxyStatus.class); if (status != null) { int count = 0; if (status.getConnections() != null) { count = status.getConnections().length; } byte[] details = null; if (answer.getDetails() != null) { details = answer.getDetails().getBytes(Charset.forName("US-ASCII")); } this._consoleProxyDao.update(answer.getProxyVmId(), count, DateUtil.currentGMTTime(), details); } else { logger.trace("Unable to get console proxy load info, id : " + answer.getProxyVmId()); this._consoleProxyDao.update(answer.getProxyVmId(), 0, DateUtil.currentGMTTime(), null); // TODO : something is wrong with the VM, restart it? } }
Example 3
Source File: ConsoleProxyManagerImpl.java From cosmic with Apache License 2.0 | 5 votes |
@Override public void onLoadReport(final ConsoleProxyLoadReportCommand cmd) { if (cmd.getLoadInfo() == null) { return; } ConsoleProxyStatus status = null; final GsonBuilder gb = new GsonBuilder(); gb.setVersion(1.3); final Gson gson = gb.create(); status = gson.fromJson(cmd.getLoadInfo(), ConsoleProxyStatus.class); if (status != null) { int count = 0; if (status.getConnections() != null) { count = status.getConnections().length; } byte[] details = null; if (cmd.getLoadInfo() != null) { details = cmd.getLoadInfo().getBytes(Charset.forName("US-ASCII")); } ConsoleProxyManagerImpl.this._consoleProxyDao.update(cmd.getProxyVmId(), count, DateUtil.currentGMTTime(), details); } else { logger.trace("Unable to get console proxy load info, id : " + cmd.getProxyVmId()); ConsoleProxyManagerImpl.this._consoleProxyDao.update(cmd.getProxyVmId(), 0, DateUtil.currentGMTTime(), null); } }
Example 4
Source File: GsonMessageProvider.java From development with Apache License 2.0 | 5 votes |
@Override public void writeTo(Representation rep, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException { OutputStreamWriter writer = new OutputStreamWriter(entityStream, CommonParams.CHARSET); try { GsonBuilder builder = new GsonBuilder(); builder.setDateFormat(CommonParams.FORMAT_DATE); if (rep.getVersion() != null) { builder.setVersion(rep.getVersion().intValue()); } Gson gson = builder.create(); gson.toJson(rep, genericType, writer); } catch (JsonSyntaxException e) { throw WebException.internalServerError() .message(CommonParams.ERROR_JSON_FORMAT).build(); } finally { writer.close(); } }
Example 5
Source File: JSONUtil.java From FoxBPM with Apache License 2.0 | 5 votes |
private static String toJson(Object target, Type targetType, boolean isSerializeNulls, Double version, String datePattern, boolean excludesFieldsWithoutExpose) { if (target == null) return EMPTY_JSON; GsonBuilder builder = new GsonBuilder(); if (isSerializeNulls) builder.serializeNulls(); if (version != null) builder.setVersion(version.doubleValue()); // if (StringUtil.isEmpty(datePattern)) // datePattern = DEFAULT_DATE_PATTERN; builder.setDateFormat(datePattern); if (excludesFieldsWithoutExpose) builder.excludeFieldsWithoutExposeAnnotation(); String result = null; Gson gson = builder.create(); try { if (targetType != null) { result = gson.toJson(target, targetType); } else { result = gson.toJson(target); } } catch (Exception ex) { if (target instanceof Collection || target instanceof Iterator || target instanceof Enumeration || target.getClass().isArray()) { result = EMPTY_JSON_ARRAY; } else result = EMPTY_JSON; } return result; }
Example 6
Source File: ConsoleProxyManagerImpl.java From cloudstack with Apache License 2.0 | 5 votes |
@Override public void onLoadReport(ConsoleProxyLoadReportCommand cmd) { if (cmd.getLoadInfo() == null) { return; } ConsoleProxyStatus status = null; try { GsonBuilder gb = new GsonBuilder(); gb.setVersion(1.3); Gson gson = gb.create(); status = gson.fromJson(cmd.getLoadInfo(), ConsoleProxyStatus.class); } catch (Throwable e) { s_logger.warn("Unable to parse load info from proxy, proxy vm id : " + cmd.getProxyVmId() + ", info : " + cmd.getLoadInfo()); } if (status != null) { int count = 0; if (status.getConnections() != null) { count = status.getConnections().length; } byte[] details = null; if (cmd.getLoadInfo() != null) { details = cmd.getLoadInfo().getBytes(Charset.forName("US-ASCII")); } _consoleProxyDao.update(cmd.getProxyVmId(), count, DateUtil.currentGMTTime(), details); } else { if (s_logger.isTraceEnabled()) { s_logger.trace("Unable to get console proxy load info, id : " + cmd.getProxyVmId()); } _consoleProxyDao.update(cmd.getProxyVmId(), 0, DateUtil.currentGMTTime(), null); } }
Example 7
Source File: ConsoleProxyManagerImpl.java From cloudstack with Apache License 2.0 | 5 votes |
public void onLoadAnswer(ConsoleProxyLoadAnswer answer) { if (answer.getDetails() == null) { return; } ConsoleProxyStatus status = null; try { GsonBuilder gb = new GsonBuilder(); gb.setVersion(1.3); Gson gson = gb.create(); status = gson.fromJson(answer.getDetails(), ConsoleProxyStatus.class); } catch (Throwable e) { s_logger.warn("Unable to parse load info from proxy, proxy vm id : " + answer.getProxyVmId() + ", info : " + answer.getDetails()); } if (status != null) { int count = 0; if (status.getConnections() != null) { count = status.getConnections().length; } byte[] details = null; if (answer.getDetails() != null) { details = answer.getDetails().getBytes(Charset.forName("US-ASCII")); } _consoleProxyDao.update(answer.getProxyVmId(), count, DateUtil.currentGMTTime(), details); } else { if (s_logger.isTraceEnabled()) { s_logger.trace("Unable to get console proxy load info, id : " + answer.getProxyVmId()); } _consoleProxyDao.update(answer.getProxyVmId(), 0, DateUtil.currentGMTTime(), null); // TODO : something is wrong with the VM, restart it? } }
Example 8
Source File: JSONUtil.java From fixflow with Apache License 2.0 | 5 votes |
private static String toJson(Object target, Type targetType, boolean isSerializeNulls, Double version, String datePattern, boolean excludesFieldsWithoutExpose) { if (target == null) return EMPTY_JSON; GsonBuilder builder = new GsonBuilder(); if (isSerializeNulls) builder.serializeNulls(); if (version != null) builder.setVersion(version.doubleValue()); // if (StringUtil.isEmpty(datePattern)) // datePattern = DEFAULT_DATE_PATTERN; builder.setDateFormat(datePattern); if (excludesFieldsWithoutExpose) builder.excludeFieldsWithoutExposeAnnotation(); String result = null; Gson gson = builder.create(); try { if (targetType != null) { result = gson.toJson(target, targetType); } else { result = gson.toJson(target); } } catch (Exception ex) { if (target instanceof Collection || target instanceof Iterator || target instanceof Enumeration || target.getClass().isArray()) { result = EMPTY_JSON_ARRAY; } else result = EMPTY_JSON; } return result; }
Example 9
Source File: ConsoleProxyManagerImpl.java From cosmic with Apache License 2.0 | 4 votes |
private boolean hasPreviousSession(final ConsoleProxyVO proxy, final VMInstanceVO vm) { ConsoleProxyStatus status = null; final GsonBuilder gb = new GsonBuilder(); gb.setVersion(1.3); final Gson gson = gb.create(); final byte[] details = proxy.getSessionDetails(); status = gson.fromJson(details != null ? new String(details, Charset.forName("US-ASCII")) : null, ConsoleProxyStatus.class); if (status != null && status.getConnections() != null) { final ConsoleProxyConnectionInfo[] connections = status.getConnections(); for (int i = 0; i < connections.length; i++) { long taggedVmId = 0; if (connections[i].tag != null) { try { taggedVmId = Long.parseLong(connections[i].tag); } catch (final NumberFormatException e) { logger.warn("Unable to parse console proxy connection info passed through tag: " + connections[i].tag, e); } } if (taggedVmId == vm.getId()) { return true; } } // // even if we are not in the list, it may because we haven't // received load-update yet // wait until session time // if (DateUtil.currentGMTTime().getTime() - vm.getProxyAssignTime().getTime() < this._proxySessionTimeoutValue) { return true; } return false; } else { logger.error("No proxy load info on an overloaded proxy ?"); return false; } }
Example 10
Source File: JsonMessageSerializer.java From cosmic with Apache License 2.0 | 4 votes |
public JsonMessageSerializer() { final GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.setVersion(1.5); _gson = gsonBuilder.create(); }
Example 11
Source File: JsonUtil.java From AndroidRobot with Apache License 2.0 | 4 votes |
/** * 将给定的目标对象根据指定的条件参数转换成 {@code JSON} 格式的字符串。 * <p /> * <strong>该方法转换发生错误时,不会抛出任何异常。若发生错误时,曾通对象返回 <code>"{}"</code>; * 集合或数组对象返回 <code>"[]"</code> * </strong> * * @param target 目标对象。 * @param targetType 目标对象的类型。 * @param isSerializeNulls 是否序列化 {@code null} 值字段。 * @param version 字段的版本号注解。 * @param datePattern 日期字段的格式化模式。 * @param excludesFieldsWithoutExpose 是否排除未标注 {@literal @Expose} 注解的字段。 * @return 目标对象的 {@code JSON} 格式的字符串。 */ public static String toJson(Object target, Type targetType, boolean isSerializeNulls, Double version, String datePattern, boolean excludesFieldsWithoutExpose) { if (target == null) { return EMPTY_JSON; } GsonBuilder builder = new GsonBuilder(); if (isSerializeNulls) { builder.serializeNulls(); } if (version != null) { builder.setVersion(version.doubleValue()); } if (isEmpty(datePattern)) { datePattern = DEFAULT_DATE_PATTERN; } builder.setDateFormat(datePattern); if (excludesFieldsWithoutExpose) { builder.excludeFieldsWithoutExposeAnnotation(); } String result = ""; Gson gson = builder.create(); try { if (targetType != null) { result = gson.toJson(target, targetType); } else { result = gson.toJson(target); } } catch (Exception ex) { if (target instanceof Collection || target instanceof Iterator || target instanceof Enumeration || target.getClass().isArray()) { result = EMPTY_JSON_ARRAY; } else { result = EMPTY_JSON; } } return result; }
Example 12
Source File: BasicResponse.java From p4ic4idea with Apache License 2.0 | 4 votes |
private Gson buildGson() { final GsonBuilder gson = new GsonBuilder(); gson.setVersion(version.asFloat()); return gson.create(); }
Example 13
Source File: JsonMessageSerializer.java From cloudstack with Apache License 2.0 | 4 votes |
public JsonMessageSerializer() { GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.setVersion(1.5); _gson = gsonBuilder.create(); }