List of usage examples for com.google.gson JsonElement getAsString
public String getAsString()
From source file:com.github.mfriedenhagen.artifactorygo.DateTimeDeserializer.java
License:Apache License
@Override public DateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) { return new DateTime(json.getAsString()); }
From source file:com.github.mfriedenhagen.artifactorygo.MD5Deserializer.java
License:Apache License
@Override public MD5 deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) { return MD5.valueOf(json.getAsString()); }
From source file:com.github.mfriedenhagen.artifactorygo.Sha1Deserializer.java
License:Apache License
@Override public Sha1 deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) { return Sha1.valueOf(json.getAsString()); }
From source file:com.github.nyrkovalex.deploy.me.parsing.Parser.java
License:Open Source License
@Override public Script parse(String path, JsonElement locations) { Iterable<String> targets = locations.isJsonArray() ? FluentIterable.from(locations.getAsJsonArray()).transform(item -> item.getAsString()).toSet() : ImmutableSet.of(locations.getAsString()); return new Script(path, targets); }
From source file:com.github.sheigutn.pushbullet.gson.RuntimeTypeAdapterFactory.java
License:Apache License
public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) { if (type.getRawType() != baseType) { return null; }// ww w . ja va 2s. c om if (baseDelegateAdapter == null) { baseDelegateAdapter = gson.getDelegateAdapter(RuntimeTypeAdapterFactory.this, TypeToken.get(baseType)); } final Map<String, TypeAdapter<?>> labelToDelegate = new LinkedHashMap<String, TypeAdapter<?>>(); final Map<Class<?>, TypeAdapter<?>> subtypeToDelegate = new LinkedHashMap<Class<?>, TypeAdapter<?>>(); for (Map.Entry<String, Class<?>> entry : labelToSubtype.entrySet()) { TypeAdapter<?> delegate = gson.getDelegateAdapter(this, TypeToken.get(entry.getValue())); labelToDelegate.put(entry.getKey(), delegate); subtypeToDelegate.put(entry.getValue(), delegate); } return new TypeAdapter<R>() { @SuppressWarnings("unchecked") @Override public R read(JsonReader in) throws IOException { JsonElement jsonElement = Streams.parse(in); JsonElement labelJsonElement = jsonElement.getAsJsonObject().get(typeFieldName); //remove(typeFieldName) changed to get(typeFieldName) if (labelJsonElement == null) { return (R) baseDelegateAdapter.fromJsonTree(jsonElement); } String label = labelJsonElement.getAsString(); @SuppressWarnings("unchecked") // registration requires that subtype extends T TypeAdapter<R> delegate = (TypeAdapter<R>) labelToDelegate.get(label); if (delegate == null) { throw new JsonParseException("cannot deserialize " + baseType + " subtype named " + label + "; did you forget to register a subtype?"); } return delegate.fromJsonTree(jsonElement); } @Override public void write(JsonWriter out, R value) throws IOException { Class<?> srcType = value.getClass(); String label = subtypeToLabel.get(srcType); @SuppressWarnings("unchecked") // registration requires that subtype extends T TypeAdapter<R> delegate = (TypeAdapter<R>) subtypeToDelegate.get(srcType); if (delegate == null) { throw new JsonParseException( "cannot serialize " + srcType.getName() + "; did you forget to register a subtype?"); } JsonObject jsonObject = delegate.toJsonTree(value).getAsJsonObject(); //if (jsonObject.has(typeFieldName)) { // throw new JsonParseException("cannot serialize " + srcType.getName() // + " because it already defines a field named " + typeFieldName); <-- Commented //} JsonObject clone = new JsonObject(); clone.add(typeFieldName, new JsonPrimitive(label)); for (Map.Entry<String, JsonElement> e : jsonObject.entrySet()) { clone.add(e.getKey(), e.getValue()); } Streams.write(clone, out); } }.nullSafe(); }
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(); }/*from w w w . ja va 2 s . c om*/ if (p.isBoolean()) { return e.getAsBoolean(); } if (p.isNumber()) { return e.getAsInt(); } return p.getAsString(); }
From source file:com.github.zhizheng.json.JsonSchemaGeneratorImpl.java
License:Apache License
/** * ? JsonElement? Json Schema /*www.j a v a 2 s .co m*/ * * @param jsonElement * @param elementName * @param isFirstLevel * @param required * @return */ private JsonObject makeSchemaElement(JsonElement jsonElement, String elementName, boolean isFirstLevel, JsonArray required) { JsonObject jsonSchemaObject = new JsonObject(); // id, $schema if (isFirstLevel) { if (jsonSchemaConfig.isPrintId()) { jsonSchemaObject.addProperty(JsonSchemaKeywords.ID.toString(), jsonSchemaConfig.getId()); } jsonSchemaObject.addProperty(JsonSchemaKeywords.SCHEMA.toString(), jsonSchemaConfig.getVersion()); } else { if (jsonSchemaConfig.isPrintId()) { jsonSchemaObject.addProperty(JsonSchemaKeywords.ID.toString(), "/" + elementName); } } // title if (jsonSchemaConfig.isPrintTitle()) { jsonSchemaObject.addProperty(JsonSchemaKeywords.TITLE.toString(), elementName);// jsonSchemaConfig.getTitle() } // description if (jsonSchemaConfig.isPrintDescription()) { jsonSchemaObject.addProperty(JsonSchemaKeywords.DESCRIPTION.toString(), jsonSchemaConfig.getDescription()); } // type String jsonElementType = JsonValueTypes.getJsonValueType(jsonElement); jsonSchemaObject.addProperty(JsonSchemaKeywords.TYPE.toString(), jsonElementType); if (jsonElementType.equals(JsonValueTypes.STRING.toString())) {// string if (jsonSchemaConfig.isPrintMinLength()) { jsonSchemaObject.addProperty(JsonSchemaKeywords.MINLENGTH.toString(), jsonSchemaConfig.getMinLength()); } if (jsonSchemaConfig.isPrintMaxLength()) { jsonSchemaObject.addProperty(JsonSchemaKeywords.MAXLENGTH.toString(), jsonSchemaConfig.getMaxLength()); } if (jsonSchemaConfig.isPrintDefault()) { jsonSchemaObject.addProperty(JsonSchemaKeywords.DEFAULT.toString(), jsonSchemaConfig.isDefaultFromJson() ? jsonElement.getAsString() : jsonSchemaConfig.getDefaultString()); } } if (jsonElementType.equals(JsonValueTypes.NUMBER.toString())) {// number if (jsonSchemaConfig.isPrintMinimum()) { jsonSchemaObject.addProperty(JsonSchemaKeywords.MINIMUM.toString(), jsonSchemaConfig.getMinimum()); } if (jsonSchemaConfig.isPrintMaximum()) { jsonSchemaObject.addProperty(JsonSchemaKeywords.MAXIMUM.toString(), jsonSchemaConfig.getMaximum()); } if (jsonSchemaConfig.isPrintExclusiveMinimum()) { if (!jsonSchemaConfig.isPrintMinimum()) { jsonSchemaObject.addProperty(JsonSchemaKeywords.MINIMUM.toString(), jsonSchemaConfig.getMinimum()); } jsonSchemaObject.addProperty(JsonSchemaKeywords.EXCLUSIVEMINIMUM.toString(), jsonSchemaConfig.isExclusiveMinimum()); } if (jsonSchemaConfig.isPrintExclusiveMaximum()) { if (!jsonSchemaConfig.isPrintMaximum()) { jsonSchemaObject.addProperty(JsonSchemaKeywords.MAXIMUM.toString(), jsonSchemaConfig.getMaximum()); } jsonSchemaObject.addProperty(JsonSchemaKeywords.EXCLUSIVEMAXIMUM.toString(), jsonSchemaConfig.isExclusiveMaximum()); } if (jsonSchemaConfig.isPrintDefault()) { jsonSchemaObject.addProperty(JsonSchemaKeywords.DEFAULT.toString(), jsonSchemaConfig.isDefaultFromJson() ? jsonElement.getAsNumber() : jsonSchemaConfig.getDefaultNumber()); } } // required && V3 if (jsonSchemaConfig.isPrintRequired() && JsonSchemaVersions.V3.toString().equals(jsonSchemaConfig.getVersion())) {// V3 required boolean ??? jsonSchemaObject.addProperty(JsonSchemaKeywords.REQUIRED.toString(), jsonSchemaConfig.isRequired()); } // required && V4 if (jsonSchemaConfig.isPrintRequired() && JsonSchemaVersions.V4.toString().equals(jsonSchemaConfig.getVersion()) && (jsonElementType.equals(JsonValueTypes.STRING.toString()) || jsonElementType.equals(JsonValueTypes.NUMBER.toString()) || jsonElementType.equals(JsonValueTypes.INTEGER.toString()) || jsonElementType.equals(JsonValueTypes.BOOLEAN.toString()))) {// V4 required array ? object required.add(elementName); } // properties, items JsonArray newRequired = new JsonArray(); if (jsonElementType.equals(JsonValueTypes.OBJECT.toString()) && !jsonElement.getAsJsonObject().entrySet().isEmpty()) {// object.properties JsonObject propertiesObject = new JsonObject(); for (Map.Entry<String, JsonElement> propertyElemement : jsonElement.getAsJsonObject().entrySet()) { propertiesObject.add(propertyElemement.getKey(), makeSchemaElement(propertyElemement.getValue(), propertyElemement.getKey(), false, newRequired)); } jsonSchemaObject.add(JsonSchemaKeywords.PROPERTIES.toString(), propertiesObject); } else if (jsonElementType.equals(JsonValueTypes.ARRAY.toString()) && jsonElement.getAsJsonArray().size() > 0) {// array.items JsonArray jsonArray = jsonElement.getAsJsonArray(); jsonSchemaObject.add(JsonSchemaKeywords.ITEMS.toString(), makeSchemaElement(jsonArray.get(0), "0", false, new JsonArray())); } // required && V4 if (jsonElementType.equals(JsonValueTypes.OBJECT.toString()) && JsonSchemaVersions.V4.toString().equals(jsonSchemaConfig.getVersion())) {// object.required jsonSchemaObject.add(JsonSchemaKeywords.REQUIRED.toString(), newRequired); } // minitems , uniqueitems if (jsonElementType.equals(JsonValueTypes.ARRAY.toString())) {// array if (jsonSchemaConfig.isPrintMinItems()) {// array.minitems jsonSchemaObject.addProperty(JsonSchemaKeywords.MINITEMS.toString(), jsonSchemaConfig.getMinItems()); } if (jsonSchemaConfig.isPrintUniqueItems()) {// array.uniqueitems jsonSchemaObject.addProperty(JsonSchemaKeywords.UNIQUEITEMS.toString(), jsonSchemaConfig.isUniqueItems()); } } return jsonSchemaObject; }
From source file:com.gmx.library.GsonUtil.java
License:Apache License
@SuppressWarnings("unchecked") public static <T> T jsonToBeanDateSerializer(String jsonStr, Class<T> cl, final String pattern) { Object obj = null;//from ww w .jav a 2 s .c o m gson = new GsonBuilder().registerTypeAdapter(Date.class, new JsonDeserializer<Date>() { public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { SimpleDateFormat format = new SimpleDateFormat(pattern); String dateStr = json.getAsString(); try { return format.parse(dateStr); } catch (ParseException e) { e.printStackTrace(); } return null; } }).setDateFormat(pattern).create(); obj = gson.fromJson(jsonStr, cl); return (T) obj; }
From source file:com.goodow.realtime.server.presence.PresenceHandler.java
License:Apache License
@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { boolean isConnect; if (req.getRequestURI().endsWith(Services.PRESENCE_CONNECT)) { isConnect = true;//w w w .j a va2s .c o m } else if (req.getRequestURI().endsWith(Services.PRESENCE_DISCONNECT)) { isConnect = false; } else { throw new IllegalArgumentException( "Can't determine the type of channel presence from the path: " + req.getRequestURI()); } String sessionId = req.getParameter(Params.SESSION_ID); String json = RpcUtil.readRequestBody(req); if (json == null || json.isEmpty()) { assert !isConnect; presenceEndpoint.get().disconnect(sessionId, null); return; } JsonObject payload = new JsonParser().parse(json).getAsJsonObject(); JsonArray ids = payload.get(Params.IDS).getAsJsonArray(); List<String> docIds = new ArrayList<String>(ids.size()); int i = 0; for (JsonElement e : ids) { docIds.add(i++, e.getAsString()); } if (isConnect) { presenceEndpoint.get().connect(sessionId, docIds); } else { presenceEndpoint.get().disconnect(sessionId, docIds); } super.doPost(req, resp); }
From source file:com.google.belay.server.CapabilityAdapter.java
License:Open Source License
@Override public Capability deserialize(JsonElement elem, Type typeOfSrc, JsonDeserializationContext context) throws JsonParseException { JsonObject obj = elem.getAsJsonObject(); JsonElement capElem = obj.get("@"); if (capElem == null) { throw new JsonParseException("capability object missing '@' field"); }/*from ww w . j a v a 2s . co m*/ String capUrlStr = capElem.getAsString(); if (capUrlStr == null) { throw new JsonParseException("'@' field is not a string"); } try { URL capUrl = new URL(capUrlStr); return new Capability(capUrl); } catch (MalformedURLException e) { throw new JsonParseException("'@' field does not map to a valid url"); } }