List of usage examples for com.google.gson JsonPrimitive getAsString
@Override
public String getAsString()
From source file:org.apache.sling.jms.Json.java
License:Apache License
private static <T> T toMapValue(JsonPrimitive p) { if (p.isString()) { return (T) p.getAsString(); } else if (p.isBoolean()) { return (T) ((Boolean) p.getAsBoolean()); } else if (p.isNumber()) { double d = p.getAsDouble(); if (Math.floor(d) == d) { return (T) ((Long) p.getAsLong()); }/*ww w. ja va 2 s. c om*/ return (T) ((Double) d); } else { return null; } }
From source file:org.apache.sling.query.mock.json.JsonToResource.java
License:Apache License
private static Resource parseResource(JsonPrimitive primitive, String name, Resource parent) { return new PropertyResourceMock(parent, name, primitive.getAsString()); }
From source file:org.bimserver.shared.json.JsonConverter.java
License:Open Source License
public Object fromJson(SClass definedType, SClass genericType, Object object) throws ConvertException, IOException { try {/*from www. jav a 2s .com*/ if (object instanceof JsonObject) { JsonObject jsonObject = (JsonObject) object; if (jsonObject.has("__type")) { String type = jsonObject.get("__type").getAsString(); SClass sClass = servicesMap.getType(type); if (sClass == null) { throw new ConvertException("Unknown type: " + type); } SBase newObject = sClass.newInstance(); for (SField field : newObject.getSClass().getAllFields()) { if (jsonObject.has(field.getName())) { newObject.sSet(field, fromJson(field.getType(), field.getGenericType(), jsonObject.get(field.getName()))); } } return newObject; } else { if (jsonObject.entrySet().size() != 0) { throw new ConvertException("Missing __type field in " + jsonObject.toString()); } else { return null; } } } else if (object instanceof JsonArray) { JsonArray array = (JsonArray) object; if (definedType.isList()) { List<Object> list = new ArrayList<Object>(); for (int i = 0; i < array.size(); i++) { list.add(fromJson(definedType, genericType, array.get(i))); } return list; } else if (definedType.isSet()) { Set<Object> set = new HashSet<Object>(); for (int i = 0; i < array.size(); i++) { set.add(fromJson(definedType, genericType, array.get(i))); } return set; } } else if (object instanceof JsonNull) { return null; } else if (definedType.isByteArray()) { if (object instanceof JsonPrimitive) { JsonPrimitive jsonPrimitive = (JsonPrimitive) object; return Base64.decodeBase64(jsonPrimitive.getAsString().getBytes(Charsets.UTF_8)); } } else if (definedType.isDataHandler()) { if (object instanceof JsonPrimitive) { JsonPrimitive jsonPrimitive = (JsonPrimitive) object; byte[] data = Base64.decodeBase64(jsonPrimitive.getAsString().getBytes(Charsets.UTF_8)); return new DataHandler(new ByteArrayDataSource(null, data)); } } else if (definedType.isInteger()) { if (object instanceof JsonPrimitive) { return ((JsonPrimitive) object).getAsInt(); } } else if (definedType.isLong()) { if (object instanceof JsonPrimitive) { return ((JsonPrimitive) object).getAsLong(); } } else if (definedType.isEnum()) { JsonPrimitive primitive = (JsonPrimitive) object; for (Object enumConstantObject : definedType.getInstanceClass().getEnumConstants()) { Enum<?> enumConstant = (Enum<?>) enumConstantObject; if (enumConstant.name().equals(primitive.getAsString())) { return enumConstant; } } } else if (definedType.isDate()) { if (object instanceof JsonPrimitive) { return new Date(((JsonPrimitive) object).getAsLong()); } } else if (definedType.isString()) { if (object instanceof JsonPrimitive) { return ((JsonPrimitive) object).getAsString(); } else if (object instanceof JsonNull) { return null; } } else if (definedType.isBoolean()) { if (object instanceof JsonPrimitive) { return ((JsonPrimitive) object).getAsBoolean(); } } else if (definedType.isList()) { if (genericType.isLong()) { if (object instanceof JsonPrimitive) { return ((JsonPrimitive) object).getAsLong(); } } else if (genericType.isInteger()) { if (object instanceof JsonPrimitive) { return ((JsonPrimitive) object).getAsInt(); } } else if (genericType.isString()) { if (object instanceof JsonPrimitive) { return ((JsonPrimitive) object).getAsString(); } } else if (genericType.isDouble()) { if (object instanceof JsonPrimitive) { return ((JsonPrimitive) object).getAsDouble(); } } } else if (definedType.isSet()) { if (genericType.isLong()) { if (object instanceof JsonPrimitive) { return ((JsonPrimitive) object).getAsLong(); } } else if (genericType.isInteger()) { if (object instanceof JsonPrimitive) { return ((JsonPrimitive) object).getAsInt(); } } else if (genericType.isString()) { if (object instanceof JsonPrimitive) { return ((JsonPrimitive) object).getAsString(); } } } else if (definedType.isDouble()) { if (object instanceof JsonPrimitive) { return ((JsonPrimitive) object).getAsDouble(); } } else if (definedType.isFloat()) { if (object instanceof JsonPrimitive) { return ((JsonPrimitive) object).getAsFloat(); } } else if (definedType.isVoid()) { return null; } } catch (NumberFormatException e) { throw new ConvertException(e); } throw new UnsupportedOperationException(object.toString()); }
From source file:org.broad.igv.ga4gh.Ga4ghAPIHelper.java
License:Open Source License
public static List<Ga4ghReadset> searchReadGroupsets(Ga4ghProvider provider, String datasetId, int maxResults) throws IOException { List<Ga4ghReadset> readsets = readsetCache.get(datasetId); if (readsets == null) { readsets = new ArrayList(); String genomeId = genomeIdMap.get(provider.getName() + " " + datasetId); // Hack until meta data on readsets is available // Loop through pages int maxPages = 100; JsonPrimitive pageToken = null; while (maxPages-- > 0) { String contentToPost = "{" + "\"datasetIds\": [\"" + datasetId + "\"]" + (pageToken == null ? "" : ", \"pageToken\": " + pageToken) + ", \"pageSize\":" + maxResults + "}"; String result = doPost(provider, "/readgroupsets/search", contentToPost, null, true); //"fields=readsets(id,name, fileData),nextPageToken"); if (result == null) return null; JsonParser parser = new JsonParser(); JsonObject obj = parser.parse(result).getAsJsonObject(); Iterator<JsonElement> iter = obj.getAsJsonArray("readGroupSets").iterator(); while (iter.hasNext()) { JsonElement next = iter.next(); JsonObject jobj = next.getAsJsonObject(); String id = jobj.get("id").getAsString(); String name = jobj.get("name").getAsString(); readsets.add(new Ga4ghReadset(id, name, genomeId)); }//from ww w . ja va2 s . c om if (readsets.size() >= maxResults) break; pageToken = obj.getAsJsonPrimitive("nextPageToken"); if (pageToken == null || pageToken.getAsString().equals("")) break; } Collections.sort(readsets, new Comparator<Ga4ghReadset>() { @Override public int compare(Ga4ghReadset o1, Ga4ghReadset o2) { return o1.getName().compareTo(o2.getName()); } }); readsetCache.put(datasetId, readsets); } return readsets; }
From source file:org.broad.igv.ga4gh.Ga4ghAPIHelper.java
License:Open Source License
public static List<JsonObject> searchReferences(Ga4ghProvider provider, String referenceSetId, int maxResults) throws IOException { List<JsonObject> references = referenceCache.get(referenceSetId); if (references == null) { references = new ArrayList(); // Loop through pages int maxPages = 100; JsonPrimitive pageToken = null; while (maxPages-- > 0) { String contentToPost = "{" + "\"referenceSetId\": \"" + referenceSetId + "\"" + (pageToken == null ? "" : ", \"pageToken\": " + pageToken) + ", \"pageSize\":" + maxResults + "}"; String result = doPost(provider, "/references/search", contentToPost, null, true); //"fields=readsets(id,name, fileData),nextPageToken"); if (result == null) return null; JsonParser parser = new JsonParser(); JsonObject obj = parser.parse(result).getAsJsonObject(); Iterator<JsonElement> iter = obj.getAsJsonArray("references").iterator(); while (iter.hasNext()) { JsonElement next = iter.next(); references.add(next.getAsJsonObject()); }/*from ww w . j a va 2 s. c o m*/ if (references.size() >= maxResults) break; pageToken = obj.getAsJsonPrimitive("nextPageToken"); if (pageToken == null || pageToken.getAsString().equals("")) break; } referenceCache.put(referenceSetId, references); } return references; }
From source file:org.broad.igv.ga4gh.Ga4ghAPIHelper.java
License:Open Source License
public static List<Alignment> searchReads(Ga4ghProvider provider, String readGroupSetId, String chr, int start, int end, boolean handleError) throws IOException { List<Alignment> alignments = new ArrayList<Alignment>(10000); int maxPages = 10000; JsonPrimitive pageToken = null; StringBuffer result = new StringBuffer(); int counter = 0; while (maxPages-- > 0) { String contentToPost = "{" + "\"readGroupSetIds\": [\"" + readGroupSetId + "\"]" + ", \"referenceName\": \"" + chr + "\"" + ", \"start\": \"" + start + "\"" + ", \"end\": \"" + end + "\"" + ", \"pageSize\": \"10000\"" + (pageToken == null ? "" : ", \"pageToken\": " + pageToken) + "}"; String readString = doPost(provider, "/reads/search", contentToPost, "", handleError); if (readString == null) { return null; }// www. j a v a 2 s. c o m JsonParser parser = new JsonParser(); JsonObject obj = parser.parse(readString).getAsJsonObject(); JsonArray reads = obj.getAsJsonArray("alignments"); Iterator<JsonElement> iter = reads.iterator(); while (iter.hasNext()) { JsonElement next = iter.next(); Ga4ghAlignment alignment = new Ga4ghAlignment(next.getAsJsonObject()); alignments.add(alignment); } pageToken = obj.getAsJsonPrimitive("nextPageToken"); if (pageToken == null || pageToken.getAsString().equals("")) break; } return alignments; }
From source file:org.broad.igv.ga4gh.Ga4ghAPIHelper.java
License:Open Source License
private static String getErrorMessage(HttpURLConnection connection) throws IOException { BufferedReader br = new BufferedReader( new InputStreamReader(new GZIPInputStream(connection.getErrorStream()))); StringBuffer sb = new StringBuffer(); String str = br.readLine();/*from www . ja v a 2s .co m*/ while (str != null) { sb.append(str); str = br.readLine(); } br.close(); JsonParser parser = new JsonParser(); JsonObject obj = parser.parse(sb.toString()).getAsJsonObject(); JsonObject errorObject = obj.getAsJsonObject("error"); if (errorObject != null) { JsonPrimitive msg = errorObject.getAsJsonPrimitive("message"); if (msg != null) return msg.getAsString(); } return sb.toString(); }
From source file:org.cvasilak.jboss.mobile.app.model.ManagementModelBase.java
License:Apache License
public void setValue(JsonElement value) { if (value instanceof JsonPrimitive) { JsonPrimitive primitive = (JsonPrimitive) value; if (primitive.isNumber()) { try { this.value = NumberFormat.getInstance().parse(primitive.getAsString()); } catch (ParseException e) { }/*from www. j a va 2 s . c o m*/ } else if (primitive.isBoolean()) { this.value = primitive.getAsBoolean(); } else if (primitive.isString()) { this.value = primitive.getAsString(); } } else if (value instanceof JsonNull) { this.value = "undefined"; } else if (value instanceof JsonArray) { List<String> list = new ArrayList<String>(); Iterator<JsonElement> iterator = value.getAsJsonArray().iterator(); while (iterator.hasNext()) { JsonElement elem = iterator.next(); if (elem instanceof JsonObject) list.add(elem.toString()); else list.add(elem.getAsString()); } this.value = list; } else if (value instanceof JsonObject) { this.value = value.toString(); } }
From source file:org.cvasilak.jboss.mobile.app.model.OperationParameter.java
License:Apache License
public void setDefaultValue(JsonElement value) { if (value instanceof JsonPrimitive) { JsonPrimitive primitive = (JsonPrimitive) value; if (primitive.isNumber()) { try { this.defaultValue = NumberFormat.getInstance().parse(primitive.getAsString()); } catch (ParseException e) { }/* w w w. ja v a2s. c om*/ } else if (primitive.isBoolean()) { this.defaultValue = primitive.getAsBoolean(); } else if (primitive.isString()) { this.defaultValue = primitive.getAsString(); } } }
From source file:org.dashbuilder.dataprovider.backend.elasticsearch.rest.client.impl.jest.ElasticSearchJestClient.java
License:Apache License
/** * Parses a given value (for a given column type) returned by response JSON query body from EL server. * * @param column The data column definition. * @param valueElement The value element from JSON query response to format. * @return The formatted value for the given column type. *///from ww w . j a va 2 s . c om public static Object parseValue(ElasticSearchDataSetDef definition, ElasticSearchDataSetMetadata metadata, DataColumn column, JsonElement valueElement) { if (column == null || valueElement == null || valueElement.isJsonNull()) return null; if (!valueElement.isJsonPrimitive()) throw new RuntimeException("Not expected JsonElement type to parse from query response."); JsonPrimitive valuePrimitive = valueElement.getAsJsonPrimitive(); ColumnType columnType = column.getColumnType(); if (ColumnType.NUMBER.equals(columnType)) { return valueElement.getAsDouble(); } else if (ColumnType.DATE.equals(columnType)) { // We can expect two return core types from EL server when handling dates: // 1.- String type, using the field pattern defined in the index' mappings, when it's result of a query without aggregations. // 2.- Numeric type, when it's result from a scalar function or a value pickup. if (valuePrimitive.isString()) { DateTimeFormatter formatter = null; String datePattern = metadata.getFieldPattern(column.getId()); if (datePattern == null || datePattern.trim().length() == 0) { // If no custom pattern for date field, use the default by EL -> org.joda.time.format.ISODateTimeFormat#dateOptionalTimeParser formatter = ElasticSearchDataSetProvider.EL_DEFAULT_DATETIME_FORMATTER; } else { // Obtain the date value by parsing using the EL pattern specified for this field. formatter = DateTimeFormat.forPattern(datePattern); } DateTime dateTime = formatter.parseDateTime(valuePrimitive.getAsString()); return dateTime.toDate(); } if (valuePrimitive.isNumber()) { return new Date(valuePrimitive.getAsLong()); } throw new UnsupportedOperationException( "Value core type not supported. Expecting string or number when using date core field types."); } // LABEL, TEXT or grouped DATE column types. String valueAsString = valueElement.getAsString(); ColumnGroup columnGroup = column.getColumnGroup(); // For FIXED date values, remove the unnecessary "0" at first character. (eg: replace month "01" to "1") if (columnGroup != null && GroupStrategy.FIXED.equals(columnGroup.getStrategy()) && valueAsString.startsWith("0")) return valueAsString.substring(1); return valueAsString; }