List of usage examples for com.google.gson JsonElement getAsJsonPrimitive
public JsonPrimitive getAsJsonPrimitive()
From source file:abtlibrary.utils.as24ApiClient.JSON.java
License:Apache License
/** * Deserialize/*from w w w . ja va 2 s. co m*/ * * @param json Json element * @param date Type * @param typeOfSrc Type * @param context Json Serialization Context * @return Date * @throw JsonParseException if fail to parse */ @Override public Date deserialize(JsonElement json, Type date, JsonDeserializationContext context) throws JsonParseException { String str = json.getAsJsonPrimitive().getAsString(); try { return apiClient.parseDateOrDatetime(str); } catch (RuntimeException e) { throw new JsonParseException(e); } }
From source file:android.zetterstrom.com.forecast.ForecastClient.java
License:Apache License
private static Gson createGson() { final long MILLIS = 1000; GsonBuilder builder = new GsonBuilder(); builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() { public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { return new Date(json.getAsJsonPrimitive().getAsLong() * MILLIS); }//from w ww . j av a2s . c o m }); return builder.create(); }
From source file:angularBeans.remote.InvocationHandler.java
License:LGPL
private void update(Object o, JsonObject params) { if (params != null) { // boolean firstIn = false; for (Map.Entry<String, JsonElement> entry : params.entrySet()) { JsonElement value = entry.getValue(); String name = entry.getKey(); if ((name.equals("sessionUID")) || (name.equals("args"))) { continue; }/*ww w. j a va 2 s . co m*/ if ((value.isJsonObject()) && (!value.isJsonNull())) { String getName; try { getName = CommonUtils.obtainGetter(o.getClass().getDeclaredField(name)); Method getter = o.getClass().getMethod(getName); Object subObj = getter.invoke(o); // logger.log(Level.INFO, "#entring sub object "+name); update(subObj, value.getAsJsonObject()); } catch (NoSuchFieldException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException e) { e.printStackTrace(); } } // ------------------------------------ if (value.isJsonArray()) { try { String getter = CommonUtils.obtainGetter(o.getClass().getDeclaredField(name)); Method get = o.getClass().getDeclaredMethod(getter); Type type = get.getGenericReturnType(); ParameterizedType pt = (ParameterizedType) type; Type actType = pt.getActualTypeArguments()[0]; String className = actType.toString(); className = className.substring(className.indexOf("class") + 6); Class clazz = Class.forName(className); JsonArray array = value.getAsJsonArray(); Collection collection = (Collection) get.invoke(o); Object elem; for (JsonElement element : array) { if (element.isJsonPrimitive()) { JsonPrimitive primitive = element.getAsJsonPrimitive(); elem = element; if (primitive.isBoolean()) elem = primitive.getAsBoolean(); if (primitive.isString()) { elem = primitive.getAsString(); } if (primitive.isNumber()) elem = primitive.isNumber(); } else { elem = util.deserialise(clazz, element); } try { if (collection instanceof List) { if (collection.contains(elem)) collection.remove(elem); } collection.add(elem); } catch (UnsupportedOperationException e) { Logger.getLogger("AngularBeans").log(java.util.logging.Level.WARNING, "trying to modify an immutable collection : " + name); } } } catch (Exception e) { e.printStackTrace(); } } // ------------------------------------------ if (value.isJsonPrimitive() && (!name.equals("setSessionUID"))) { try { if (!CommonUtils.hasSetter(o.getClass(), name)) { continue; } name = "set" + name.substring(0, 1).toUpperCase() + name.substring(1); Class type = null; for (Method set : o.getClass().getDeclaredMethods()) { if (CommonUtils.isSetter(set)) { if (set.getName().equals(name)) { Class<?>[] pType = set.getParameterTypes(); type = pType[0]; break; } } } if (type.equals(LobWrapper.class)) continue; Object param = null; if ((params.entrySet().size() >= 1) && (type != null)) { param = CommonUtils.convertFromString(value.getAsString(), type); } o.getClass().getMethod(name, type).invoke(o, param); } catch (Exception e) { e.printStackTrace(); } } } } }
From source file:angularBeans.util.AngularBeansUtils.java
License:LGPL
public Object convertEvent(NGEvent event) throws ClassNotFoundException { JsonElement element = CommonUtils.parse(event.getData()); JsonElement data;//from w w w .ja v a 2s .c o m Class javaClass; try { data = element.getAsJsonObject(); javaClass = Class.forName(event.getDataClass()); } catch (Exception e) { data = element.getAsJsonPrimitive(); if (event.getDataClass() == null) { event.setDataClass("String"); } javaClass = Class.forName("java.lang." + event.getDataClass()); } Object o; if (javaClass.equals(String.class)) { o = data.toString().substring(1, data.toString().length() - 1); } else { o = deserialise(javaClass, data); } return o; }
From source file:at.orz.arangodb.util.JsonUtils.java
License:Apache License
public static double toDouble(JsonElement elem) { if (elem != null && !elem.isJsonNull()) { JsonPrimitive primitive = elem.getAsJsonPrimitive(); if (primitive.isNumber()) { return primitive.getAsDouble(); } else if (primitive.isString()) { if ("INF".equals(primitive.getAsString())) { return Double.POSITIVE_INFINITY; } else if ("NaN".equals(primitive.getAsString())) { return Double.NaN; }/*from w ww.j a va 2 s. c o m*/ } } return Double.NaN; }
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 w w w . jav a 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//from www.ja v a 2 s . co m 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:ca.cmput301w14t09.elasticSearch.JsonBitmapConverter.java
License:GNU General Public License
/** * deserialize turns a Json primitive into a bitmap *///from w w w .j a v a 2s . c o m @Override public Bitmap deserialize(JsonElement src, Type typeOfSrc, JsonDeserializationContext context) throws JsonParseException { String base64Encoded = src.getAsJsonPrimitive().getAsString(); byte[] data = Base64.decode(base64Encoded, Base64.NO_WRAP); return BitmapFactory.decodeByteArray(data, 0, data.length); }
From source file:ca.cmput301w14t09.FileManaging.SerializableBitmap.java
License:GNU General Public License
/** * deserialize turns a Json primitive into a bitmap * @param src// www.ja v a 2 s.c o m * @param typeOfSrc * @param context * @return */ @Override public Bitmap deserialize(JsonElement src, Type typeOfSrc, JsonDeserializationContext context) throws JsonParseException { String base64Encoded = src.getAsJsonPrimitive().getAsString(); byte[] data = Base64.decode(base64Encoded, Base64.NO_WRAP); return BitmapFactory.decodeByteArray(data, 0, data.length); }
From source file:ca.cs.ualberta.localpost.controller.BitmapJsonConverter.java
License:Open Source License
@Override public Bitmap deserialize(JsonElement src, Type typeOfSrc, JsonDeserializationContext context) throws JsonParseException { String base64Encoded = src.getAsJsonPrimitive().getAsString(); byte[] data = Base64.decode(base64Encoded, Base64.NO_WRAP); return BitmapFactory.decodeByteArray(data, 0, data.length); }