本文整理汇总了Java中com.google.gson.JsonElement.isJsonArray方法的典型用法代码示例。如果您正苦于以下问题:Java JsonElement.isJsonArray方法的具体用法?Java JsonElement.isJsonArray怎么用?Java JsonElement.isJsonArray使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.gson.JsonElement
的用法示例。
在下文中一共展示了JsonElement.isJsonArray方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: parseFloatArray
import com.google.gson.JsonElement; //导入方法依赖的package包/类
public static float[] parseFloatArray(JsonElement e, int length, String prefix)
{
if (!e.isJsonArray()) throw new JsonParseException(prefix + ": expected an array, got: " + e);
JsonArray t = e.getAsJsonArray();
if (t.size() != length) throw new JsonParseException(prefix + ": expected an array of length " + length + ", got: " + t.size());
float[] ret = new float[length];
for (int i = 0; i < length; i++)
{
try
{
ret[i] = t.get(i).getAsNumber().floatValue();
}
catch (ClassCastException ex)
{
throw new JsonParseException(prefix + " element: expected number, got: " + t.get(i));
}
}
return ret;
}
示例2: deserialize
import com.google.gson.JsonElement; //导入方法依赖的package包/类
public VariantList deserialize(JsonElement p_deserialize_1_, Type p_deserialize_2_, JsonDeserializationContext p_deserialize_3_) throws JsonParseException
{
List<Variant> list = Lists.<Variant>newArrayList();
if (p_deserialize_1_.isJsonArray())
{
JsonArray jsonarray = p_deserialize_1_.getAsJsonArray();
if (jsonarray.size() == 0)
{
throw new JsonParseException("Empty variant array");
}
for (JsonElement jsonelement : jsonarray)
{
list.add((Variant)p_deserialize_3_.deserialize(jsonelement, Variant.class));
}
}
else
{
list.add((Variant)p_deserialize_3_.deserialize(p_deserialize_1_, Variant.class));
}
return new VariantList(list);
}
示例3: parseVariants
import com.google.gson.JsonElement; //导入方法依赖的package包/类
protected ModelBlockDefinition.Variants parseVariants(JsonDeserializationContext p_178335_1_, Entry<String, JsonElement> p_178335_2_)
{
String s = (String)p_178335_2_.getKey();
List<ModelBlockDefinition.Variant> list = Lists.<ModelBlockDefinition.Variant>newArrayList();
JsonElement jsonelement = (JsonElement)p_178335_2_.getValue();
if (jsonelement.isJsonArray())
{
for (JsonElement jsonelement1 : jsonelement.getAsJsonArray())
{
list.add((ModelBlockDefinition.Variant)p_178335_1_.deserialize(jsonelement1, ModelBlockDefinition.Variant.class));
}
}
else
{
list.add((ModelBlockDefinition.Variant)p_178335_1_.deserialize(jsonelement, ModelBlockDefinition.Variant.class));
}
return new ModelBlockDefinition.Variants(s, list);
}
示例4: TopLevelResponse
import com.google.gson.JsonElement; //导入方法依赖的package包/类
public TopLevelResponse(JsonObject fields) throws InvalidGraphQLException {
JsonElement errorsElement = fields.get(ERRORS_KEY);
JsonElement dataElement = fields.get(DATA_KEY);
if (dataElement != null && dataElement.isJsonNull()) {
dataElement = null;
}
if (errorsElement == null && dataElement == null) {
throw new InvalidGraphQLException("Response must contain a top-level 'data' or 'errors' entry");
}
if (dataElement != null) {
if (!dataElement.isJsonObject()) {
throw new InvalidGraphQLException("'data' entry in response must be a map");
}
this.data = dataElement.getAsJsonObject();
}
if (errorsElement != null) {
if (!errorsElement.isJsonArray()) {
throw new InvalidGraphQLException("'errors' entry in response must be an array");
}
for (JsonElement error : errorsElement.getAsJsonArray()) {
errors.add(new Error(error.isJsonObject() ? error.getAsJsonObject() : new JsonObject()));
}
}
}
示例5: calculateDataType
import com.google.gson.JsonElement; //导入方法依赖的package包/类
/**
* Calculate the datatype of a key, based on an example value presented to this method. This method mostly
* works as expected, however fails in some cases where it interprets a field as an INT, where in fact it
* should be of type BIGINT. The tool cannot currently handle this automatically, however can be fixed
* manually in the code.
*
* @param key Key of the field being tested.
* @param value An example value of this field.
* @return The correct datatype.
*/
private String calculateDataType(String key, JsonElement value) {
if (value.isJsonArray()) {
return "TEXT[]";
} else {
// enter hacks here...
if (key.equals("mono_time")) return "BIGINT";
else {
String testValue = value.getAsString();
try {
//noinspection ResultOfMethodCallIgnored
Integer.parseInt(testValue);
return "INT";
} catch (NumberFormatException nfe) {
try {
//noinspection ResultOfMethodCallIgnored
Float.parseFloat(testValue);
return "REAL";
} catch (NumberFormatException nfe2) {
boolean x = Boolean.parseBoolean(testValue);
if (x) return "BOOLEAN";
else if (testValue.equalsIgnoreCase("false")) return "BOOLEAN";
else return "TEXT";
}
}
}
}
}
示例6: getJsonArray
import com.google.gson.JsonElement; //导入方法依赖的package包/类
/**
* Gets the given JsonElement as a JsonArray. Expects the second parameter to be the name of the element's field if
* an error message needs to be thrown.
*/
public static JsonArray getJsonArray(JsonElement json, String memberName)
{
if (json.isJsonArray())
{
return json.getAsJsonArray();
}
else
{
throw new JsonSyntaxException("Expected " + memberName + " to be a JsonArray, was " + toString(json));
}
}
示例7: fromJson
import com.google.gson.JsonElement; //导入方法依赖的package包/类
public void fromJson(JsonElement json)
{
if (json.isJsonArray())
{
for (JsonElement jsonelement : json.getAsJsonArray())
{
this.add(jsonelement.getAsString());
}
}
}
示例8: asArrayOrSingle
import com.google.gson.JsonElement; //导入方法依赖的package包/类
public static Stream<JsonElement> asArrayOrSingle(JsonElement element) {
if (element.isJsonArray()) {
Stream.Builder<JsonElement> builder = Stream.builder();
for (JsonElement el : element.getAsJsonArray()) {
builder.add(el);
}
return builder.build();
} else {
return Stream.of(element);
}
}
示例9: getProperty
import com.google.gson.JsonElement; //导入方法依赖的package包/类
/**
* This method performs a top-down recursion on a json tree to find given key
*
* @param key
* : property whose value we need to retrieve
* @param jsonObject
* : json object to traverse
* @return JsonElement if found or null if element not found
*/
public static JsonElement getProperty(String key, JsonObject jsonObject) {
// check current level for the key before descending
if (jsonObject.has(key)) {
return jsonObject.get(key);
}
// get all entry sets
Set<Entry<String, JsonElement>> entries = jsonObject.entrySet();
for (Entry<String, JsonElement> entry : entries) {
// cache the current value since retrieval is done so much
JsonElement curVal = entry.getValue();
if (curVal.isJsonArray()) {
for (JsonElement el : curVal.getAsJsonArray()) {
// recursively traverse the sub-tree
if (el.isJsonObject()) {
JsonElement res = getProperty(key, (JsonObject) el);
if (res != null) {
return res;
}
}
}
} else if (curVal.isJsonObject()) {
// traverse sub-node
return getProperty(key, curVal.getAsJsonObject());
}
}
return null;
}
示例10: read
import com.google.gson.JsonElement; //导入方法依赖的package包/类
public Object read(JsonElement in) {
if (in.isJsonArray()) {
List<Object> list = new ArrayList<Object>();
JsonArray arr = in.getAsJsonArray();
for (JsonElement anArr : arr) {
list.add(read(anArr));
}
return list;
} else if (in.isJsonObject()) {
Map<String, Object> map = new LinkedTreeMap<String, Object>();
JsonObject obj = in.getAsJsonObject();
Set<Map.Entry<String, JsonElement>> entitySet = obj.entrySet();
for (Map.Entry<String, JsonElement> entry: entitySet) {
map.put(entry.getKey(), read(entry.getValue()));
}
return map;
} else if (in.isJsonPrimitive()) {
JsonPrimitive prim = in.getAsJsonPrimitive();
if (prim.isBoolean()) {
return prim.getAsBoolean();
} else if (prim.isString()) {
return prim.getAsString();
} else if (prim.isNumber()) {
Number num = prim.getAsNumber();
// here you can handle double int/long values
// and return any type you want
// this solution will transform 3.0 float to long values
if (Math.abs(Math.ceil(num.doubleValue())-num.longValue()) < .0000001) {
return num.intValue();
} else {
return num.doubleValue();
}
}
}
return null;
}
示例11: getJsonType
import com.google.gson.JsonElement; //导入方法依赖的package包/类
private String getJsonType(JsonElement element) {
if (element == null) return "null";
if (element.isJsonArray()) return "json array";
if (element.isJsonPrimitive()) return "json primitive " + getJsonPrimitiveType(element.getAsJsonPrimitive());
if (element.isJsonNull()) return "json null";
if (element.isJsonObject()) return "json object";
return "unknown";
}
示例12: getJsonArray
import com.google.gson.JsonElement; //导入方法依赖的package包/类
/**
* Gets the given JsonElement as a JsonArray. Expects the second parameter to be the name of the element's field if
* an error message needs to be thrown.
*/
public static JsonArray getJsonArray(JsonElement p_151207_0_, String p_151207_1_)
{
if (p_151207_0_.isJsonArray())
{
return p_151207_0_.getAsJsonArray();
}
else
{
throw new JsonSyntaxException("Expected " + p_151207_1_ + " to be a JsonArray, was " + toString(p_151207_0_));
}
}
示例13: fetch
import com.google.gson.JsonElement; //导入方法依赖的package包/类
public JsonArray fetch(EnumType entityType) throws IOException {
JsonElement element = getFetcher().fetch(entityType, null);
if (element == null) {
return null;
}
if (element.isJsonArray()) {
return element.getAsJsonArray();
} else {
throw new JsonParseException("Invalid response from DataSourceInput. Expected "
+ "a JsonArray, but fetched "+element.getClass().getName()
+". Entity fetcher is "+getFetcher());
} }
示例14: getAsArray
import com.google.gson.JsonElement; //导入方法依赖的package包/类
public static JsonArray getAsArray(JsonObject source, Enum<?> sourceProperty) {
if (source == null) {
return null;
}
JsonElement el = source.get(sourceProperty.name());
if (el==null || !el.isJsonArray()) {
return null;
}
return el.getAsJsonArray();
}
示例15: setRelatedVideos
import com.google.gson.JsonElement; //导入方法依赖的package包/类
@Deprecated
private void setRelatedVideos(JsonObject origin, JsonObject dest) {
JsonArray related = getAsArray(origin, InputJsonKeys.VendorAPISource.Topics.related);
if (related == null) {
return;
}
for (JsonElement el: related) {
if (!el.isJsonObject()) {
continue;
}
JsonObject obj = el.getAsJsonObject();
if (!obj.has("name") || !obj.has("values")) {
continue;
}
if (InputJsonKeys.VendorAPISource.Topics.RELATED_NAME_VIDEO.equals(
obj.getAsJsonPrimitive("name").getAsString())) {
JsonElement values = obj.get("values");
if (!values.isJsonArray()) {
continue;
}
// As per the data specification, related content is formatted as
// "video1 title1\nvideo2 title2\n..."
StringBuilder relatedContentStr = new StringBuilder();
for (JsonElement value: values.getAsJsonArray()) {
String relatedSessionId = value.getAsString();
JsonObject relatedVideo = videoSessionsById.get(relatedSessionId);
if (relatedVideo != null) {
JsonElement vid = get(relatedVideo, OutputJsonKeys.VideoLibrary.vid);
JsonElement title = get(relatedVideo, OutputJsonKeys.VideoLibrary.title);
if (vid != null && title != null) {
relatedContentStr.append(vid.getAsString()).append(" ")
.append(title.getAsString()).append("\n");
}
}
}
set(new JsonPrimitive(relatedContentStr.toString()),
dest, OutputJsonKeys.Sessions.relatedContent);
}
}
}