List of usage examples for com.google.gson JsonElement getAsJsonPrimitive
public JsonPrimitive getAsJsonPrimitive()
From source file:com.github.maoo.indexer.client.WebScriptsAlfrescoClient.java
License:Apache License
private String getUsername(JsonObject userObject) { if (!userObject.has(USERNAME)) { throw new AlfrescoParseException("Json response is missing username."); }//from w ww. j a v a 2 s . c o m JsonElement usernameElement = userObject.get(USERNAME); if (!usernameElement.isJsonPrimitive() || !usernameElement.getAsJsonPrimitive().isString()) { throw new AlfrescoParseException("Username must be a string. It was: " + usernameElement.toString()); } return usernameElement.getAsString(); }
From source file:com.github.maoo.indexer.client.WebScriptsAlfrescoClient.java
License:Apache License
private List<String> getAuthorities(JsonObject userObject) { List<String> authorities = new ArrayList<String>(); if (!userObject.has(AUTHORITIES)) { throw new AlfrescoParseException("Json response is authorities."); }/*from w ww . j a v a 2 s. c o m*/ JsonElement authoritiesElement = userObject.get(AUTHORITIES); if (!authoritiesElement.isJsonArray()) { throw new AlfrescoParseException( "Authorities must be a json array. It was: " + authoritiesElement.toString()); } JsonArray authoritiesArray = authoritiesElement.getAsJsonArray(); for (JsonElement authorityElement : authoritiesArray) { if (!authorityElement.isJsonPrimitive()) { throw new AlfrescoParseException( "Authority entry must be a string. It was: " + authoritiesElement.toString()); } JsonPrimitive authorityPrimitive = authorityElement.getAsJsonPrimitive(); if (!authorityPrimitive.isString()) { throw new AlfrescoParseException( "Authority entry must be a string. It was: " + authoritiesElement.toString()); } authorities.add(authorityPrimitive.getAsString()); } return authorities; }
From source file:com.github.strawberry.util.Json.java
License:Open Source License
public static Object parsePrimitive(JsonElement e) { JsonPrimitive p = e.getAsJsonPrimitive(); if (p.isString()) { return e.getAsString(); }// w w w . j a va 2 s . co m if (p.isBoolean()) { return e.getAsBoolean(); } if (p.isNumber()) { return e.getAsInt(); } return p.getAsString(); }
From source file:com.github.tddts.jet.config.gson.LocalDateTimeJsonDeserializer.java
License:Apache License
@Override public LocalDateTime deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { Instant timeInstant = Instant.parse(jsonElement.getAsJsonPrimitive().getAsString()); // Convert Zulu time to system time zone return ZonedDateTime.ofInstant(timeInstant, ZoneId.systemDefault()).toLocalDateTime(); }
From source file:com.github.zhizheng.json.JsonValueTypes.java
License:Apache License
/** * Json /* w w w . j ava 2 s. c o m*/ * * @param jsonElement * @return */ public static String getJsonValueType(JsonElement jsonElement) { if (jsonElement.isJsonObject()) { return OBJECT.toString(); } if (jsonElement.isJsonArray()) { return ARRAY.toString(); } if (jsonElement.isJsonPrimitive()) { JsonPrimitive asJsonPrimitive = jsonElement.getAsJsonPrimitive(); if (asJsonPrimitive.isBoolean()) { return BOOLEAN.toString(); } if (asJsonPrimitive.isNumber()) { return NUMBER.toString(); } return STRING.toString(); } return NULL.toString(); }
From source file:com.google.gerrit.httpd.TokenVerifiedRestApiServlet.java
License:Apache License
private static ParsedBody parseJson(HttpServletRequest req, HttpServletResponse res) throws IOException { try {//from w w w . java 2 s . c o m JsonElement element = new JsonParser().parse(req.getReader()); if (!element.isJsonObject()) { sendError(res, SC_BAD_REQUEST, "Expected JSON object in request body"); return null; } ParsedBody body = new ParsedBody(); body.req = req; body.json = (JsonObject) element; JsonElement authKey = body.json.remove(AUTHKEY_NAME); if (authKey != null && authKey.isJsonPrimitive() && authKey.getAsJsonPrimitive().isString()) { body._authkey = authKey.getAsString(); } return body; } catch (JsonParseException e) { sendError(res, SC_BAD_REQUEST, "Invalid JSON object in request body"); return null; } }
From source file:com.google.gerrit.server.events.EventDeserializer.java
License:Apache License
@Override public Event deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (!json.isJsonObject()) { throw new JsonParseException("Not an object"); }/* ww w. jav a 2 s .com*/ JsonElement typeJson = json.getAsJsonObject().get("type"); if (typeJson == null || !typeJson.isJsonPrimitive() || !typeJson.getAsJsonPrimitive().isString()) { throw new JsonParseException("Type is not a string: " + typeJson); } String type = typeJson.getAsJsonPrimitive().getAsString(); Class<?> cls = EventTypes.getClass(type); if (cls == null) { throw new JsonParseException("Unknown event type: " + type); } return context.deserialize(json, cls); }
From source file:com.google.gwtjsonrpc.server.CallDeserializer.java
License:Apache License
private static boolean isString(final JsonElement e) { return e != null && e.isJsonPrimitive() && e.getAsJsonPrimitive().isString(); }
From source file:com.google.iosched.model.DataModelHelper.java
License:Open Source License
public static JsonPrimitive getMapValue(JsonElement map, String key, Converter converter, String defaultValueStr) { JsonPrimitive defaultValue = null;/* w w w . j a v a 2 s . c o m*/ if (defaultValueStr != null) { defaultValue = new JsonPrimitive(defaultValueStr); if (converter != null) defaultValue = converter.convert(defaultValue); } if (map == null || !map.isJsonArray()) { return defaultValue; } for (JsonElement el : map.getAsJsonArray()) { if (!el.isJsonObject()) { continue; } JsonObject obj = el.getAsJsonObject(); if (!obj.has("name") || !obj.has("value")) { continue; } if (key.equals(obj.getAsJsonPrimitive("name").getAsString())) { JsonElement value = obj.get("value"); if (!value.isJsonPrimitive()) { throw new ConverterException(value, converter, "Expected a JsonPrimitive"); } if (converter != null) value = converter.convert(value); return value.getAsJsonPrimitive(); } } return defaultValue; }
From source file:com.haulmont.restapi.service.QueriesControllerManager.java
License:Apache License
protected Object toObject(Class clazz, String value) throws ParseException { if (clazz.isArray()) { Class componentType = clazz.getComponentType(); JsonParser jsonParser = new JsonParser(); JsonArray jsonArray = jsonParser.parse(value).getAsJsonArray(); List result = new ArrayList(); for (JsonElement jsonElement : jsonArray) { String stringValue = (jsonElement.isJsonPrimitive() && jsonElement.getAsJsonPrimitive().isString()) ? jsonElement.getAsJsonPrimitive().getAsString() : jsonElement.toString(); Object arrayElementValue = toObject(componentType, stringValue); result.add(arrayElementValue); }//from w ww . j a v a2 s . c o m return result; } if (String.class == clazz) return value; if (Integer.class == clazz || Integer.TYPE == clazz || Byte.class == clazz || Byte.TYPE == clazz || Short.class == clazz || Short.TYPE == clazz) return Datatypes.getNN(Integer.class).parse(value); if (Date.class == clazz) { try { return Datatypes.getNN(Date.class).parse(value); } catch (ParseException e) { try { return Datatypes.getNN(java.sql.Date.class).parse(value); } catch (ParseException e1) { return Datatypes.getNN(Time.class).parse(value); } } } if (BigDecimal.class == clazz) return Datatypes.getNN(BigDecimal.class).parse(value); if (Boolean.class == clazz || Boolean.TYPE == clazz) return Datatypes.getNN(Boolean.class).parse(value); if (Long.class == clazz || Long.TYPE == clazz) return Datatypes.getNN(Long.class).parse(value); if (Double.class == clazz || Double.TYPE == clazz || Float.class == clazz || Float.TYPE == clazz) return Datatypes.getNN(Double.class).parse(value); if (UUID.class == clazz) return UUID.fromString(value); throw new IllegalArgumentException("Parameters of type " + clazz.getName() + " are not supported"); }