当前位置: 首页>>代码示例>>Java>>正文


Java JsonElement.getAsJsonPrimitive方法代码示例

本文整理汇总了Java中com.google.gson.JsonElement.getAsJsonPrimitive方法的典型用法代码示例。如果您正苦于以下问题:Java JsonElement.getAsJsonPrimitive方法的具体用法?Java JsonElement.getAsJsonPrimitive怎么用?Java JsonElement.getAsJsonPrimitive使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.google.gson.JsonElement的用法示例。


在下文中一共展示了JsonElement.getAsJsonPrimitive方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: parseValues

import com.google.gson.JsonElement; //导入方法依赖的package包/类
public static List<KvEntry> parseValues(JsonObject valuesObject) {
  List<KvEntry> result = new ArrayList<>();
  for (Entry<String, JsonElement> valueEntry : valuesObject.entrySet()) {
    JsonElement element = valueEntry.getValue();
    if (element.isJsonPrimitive()) {
      JsonPrimitive value = element.getAsJsonPrimitive();
      if (value.isString()) {
        result.add(new StringDataEntry(valueEntry.getKey(), value.getAsString()));
      } else if (value.isBoolean()) {
        result.add(new BooleanDataEntry(valueEntry.getKey(), value.getAsBoolean()));
      } else if (value.isNumber()) {
        if (value.getAsString().contains(".")) {
          result.add(new DoubleDataEntry(valueEntry.getKey(), value.getAsDouble()));
        } else {
          result.add(new LongDataEntry(valueEntry.getKey(), value.getAsLong()));
        }
      } else {
        throw new JsonSyntaxException("Can't parse value: " + value);
      }
    } else {
      throw new JsonSyntaxException("Can't parse value: " + element);
    }
  }
  return result;
}
 
开发者ID:osswangxining,项目名称:iotplatform,代码行数:26,代码来源:JsonConverter.java

示例2: keyToString

import com.google.gson.JsonElement; //导入方法依赖的package包/类
private String keyToString(JsonElement keyElement) {
    if (keyElement.isJsonPrimitive()) {
        JsonPrimitive primitive = keyElement.getAsJsonPrimitive();
        if (primitive.isNumber()) {
            return String.valueOf(primitive.getAsNumber());
        }
        if (primitive.isBoolean()) {
            return Boolean.toString(primitive.getAsBoolean());
        }
        if (primitive.isString()) {
            return primitive.getAsString();
        }
        throw new AssertionError();
    } else if (keyElement.isJsonNull()) {
        return "null";
    } else {
        throw new AssertionError();
    }
}
 
开发者ID:JackChan1999,项目名称:letv,代码行数:20,代码来源:MapTypeAdapterFactory.java

示例3: keyToString

import com.google.gson.JsonElement; //导入方法依赖的package包/类
private String keyToString(JsonElement keyElement) {
  if (keyElement.isJsonPrimitive()) {
    JsonPrimitive primitive = keyElement.getAsJsonPrimitive();
    if (primitive.isNumber()) {
      return String.valueOf(primitive.getAsNumber());
    } else if (primitive.isBoolean()) {
      return Boolean.toString(primitive.getAsBoolean());
    } else if (primitive.isString()) {
      return primitive.getAsString();
    } else {
      throw new AssertionError();
    }
  } else if (keyElement.isJsonNull()) {
    return "null";
  } else {
    throw new AssertionError();
  }
}
 
开发者ID:odoo-mobile-intern,项目名称:odoo-work,代码行数:19,代码来源:MapTypeAdapterFactory.java

示例4: deserialize

import com.google.gson.JsonElement; //导入方法依赖的package包/类
@Override
public Holder deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException {
  JsonObject root = (JsonObject) json;

  ImmutableHolder.Builder builder = ImmutableHolder.builder();

  if (root.has("id")) {
    builder.id(root.get("id").getAsString());
  }

  JsonElement value = root.get(VALUE_PROPERTY);
  if (value == null) {
    throw new JsonParseException(String.format("%s not found for %s in JSON", VALUE_PROPERTY, type));
  }

  if (value.isJsonObject()) {
    final String valueTypeName = value.getAsJsonObject().get(Holder.TYPE_PROPERTY).getAsString();
    try {
      Class<?> valueType = Class.forName(valueTypeName);
      builder.value(context.deserialize(value, valueType));
    } catch (ClassNotFoundException e) {
      throw new JsonParseException(String.format("Couldn't construct value class %s for %s", valueTypeName, type), e);
    }
  } else if (value.isJsonPrimitive()) {
    final JsonPrimitive primitive = value.getAsJsonPrimitive();
    if (primitive.isString()) {
      builder.value(primitive.getAsString());
    } else if (primitive.isNumber()) {
      builder.value(primitive.getAsInt());
    } else if (primitive.isBoolean()) {
      builder.value(primitive.getAsBoolean());
    }
  } else {
    throw new JsonParseException(
        String.format("Couldn't deserialize %s : %s. Not a primitive or object", VALUE_PROPERTY, value));
  }

  return builder.build();

}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:41,代码来源:HolderJsonSerializer.java

示例5: deserialize

import com.google.gson.JsonElement; //导入方法依赖的package包/类
@Override
public UnnamedField deserialize(JsonElement json,
                                Type typeOfT,
                                JsonDeserializationContext context) throws JsonParseException {
    if (!json.isJsonPrimitive()) {
        throw new JsonParseException("Is not a primitive: " + json.toString());
    }

    JsonPrimitive jsonPrimitive = json.getAsJsonPrimitive();

    UnnamedField result = null;

    if (jsonPrimitive.isBoolean()) {
        result = FieldFabric.create(jsonPrimitive.getAsBoolean());
    } else if (jsonPrimitive.isString()) {
        result = FieldFabric.create(jsonPrimitive.getAsString());
    } else if (jsonPrimitive.isNumber()) {
        String numberString = jsonPrimitive.getAsString();
        if (numberString.contains(".")) {
            result = FieldFabric.create(jsonPrimitive.getAsDouble());
        } else {
            result = FieldFabric.create(jsonPrimitive.getAsLong());
        }
    }

    if (result == null) {
        throw new JsonParseException("JsonElement is of unsupported type");
    }

    return result;
}
 
开发者ID:NomenSvyat,项目名称:SwitchBoxPlugin,代码行数:32,代码来源:UnnamedFieldDeserializer.java

示例6: 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;
}
 
开发者ID:Huawei,项目名称:Server_Management_Common_eSightApi,代码行数:38,代码来源:JsonUtil.java

示例7: write

import com.google.gson.JsonElement; //导入方法依赖的package包/类
public void write(JsonWriter out, JsonElement value) throws IOException {
    if (value == null || value.isJsonNull()) {
        out.nullValue();
    } else if (value.isJsonPrimitive()) {
        JsonPrimitive primitive = value.getAsJsonPrimitive();
        if (primitive.isNumber()) {
            out.value(primitive.getAsNumber());
        } else if (primitive.isBoolean()) {
            out.value(primitive.getAsBoolean());
        } else {
            out.value(primitive.getAsString());
        }
    } else if (value.isJsonArray()) {
        out.beginArray();
        i$ = value.getAsJsonArray().iterator();
        while (i$.hasNext()) {
            write(out, (JsonElement) i$.next());
        }
        out.endArray();
    } else if (value.isJsonObject()) {
        out.beginObject();
        for (Entry<String, JsonElement> e : value.getAsJsonObject().entrySet()) {
            out.name((String) e.getKey());
            write(out, (JsonElement) e.getValue());
        }
        out.endObject();
    } else {
        throw new IllegalArgumentException("Couldn't write " + value.getClass());
    }
}
 
开发者ID:JackChan1999,项目名称:letv,代码行数:31,代码来源:TypeAdapters.java

示例8: importPreferencesFromJson

import com.google.gson.JsonElement; //导入方法依赖的package包/类
@SuppressLint("ApplySharedPref")
private static void importPreferencesFromJson(Context context, Reader reader) {
    SharedPreferences.Editor prefs = PreferenceManager.getDefaultSharedPreferences(context).edit();
    SettingsHelper.getInstance(context).clear();
    JsonObject obj = SettingsHelper.getGson().fromJson(reader, JsonObject.class);
    for (Map.Entry<String, JsonElement> entry : obj.entrySet()) {
        JsonElement el = entry.getValue();
        if (el.isJsonArray()) {
            Set<String> items = new HashSet<>();
            for (JsonElement child : el.getAsJsonArray())
                items.add(child.getAsString());
            prefs.putStringSet(entry.getKey(), items);
        } else {
            JsonPrimitive primitive = el.getAsJsonPrimitive();
            if (primitive.isBoolean()) {
                prefs.putBoolean(entry.getKey(), primitive.getAsBoolean());
            } else if (primitive.isNumber()) {
                Number number = primitive.getAsNumber();
                if (number instanceof Float || number instanceof Double)
                    prefs.putFloat(entry.getKey(), number.floatValue());
                else if (number instanceof Long)
                    prefs.putLong(entry.getKey(), number.longValue());
                else
                    prefs.putInt(entry.getKey(), number.intValue());
            } else if (primitive.isString()) {
                prefs.putString(entry.getKey(), primitive.getAsString());
            }
        }
    }
    prefs.commit(); // This will be called asynchronously
}
 
开发者ID:MCMrARM,项目名称:revolution-irc,代码行数:32,代码来源:BackupManager.java

示例9: write

import com.google.gson.JsonElement; //导入方法依赖的package包/类
public void write(JsonWriter out, JsonElement value) throws IOException {
    if (value == null || value.isJsonNull()) {
        out.nullValue();
    } else if (value.isJsonPrimitive()) {
        JsonPrimitive primitive = value.getAsJsonPrimitive();
        if (primitive.isNumber()) {
            out.value(primitive.getAsNumber());
        } else if (primitive.isBoolean()) {
            out.value(primitive.getAsBoolean());
        } else {
            out.value(primitive.getAsString());
        }
    } else if (value.isJsonArray()) {
        out.beginArray();
        Iterator it = value.getAsJsonArray().iterator();
        while (it.hasNext()) {
            write(out, (JsonElement) it.next());
        }
        out.endArray();
    } else if (value.isJsonObject()) {
        out.beginObject();
        for (Entry<String, JsonElement> e : value.getAsJsonObject().entrySet()) {
            out.name((String) e.getKey());
            write(out, (JsonElement) e.getValue());
        }
        out.endObject();
    } else {
        throw new IllegalArgumentException("Couldn't write " + value.getClass());
    }
}
 
开发者ID:JackChan1999,项目名称:boohee_v5.6,代码行数:31,代码来源:TypeAdapters.java

示例10: translateScenarioArguments

import com.google.gson.JsonElement; //导入方法依赖的package包/类
private void translateScenarioArguments(List<Object> parameters) {
    // TODO: Lookup and replace the list of parameters
    // with data from the test data file

    if(testData != null) {
        for(int i=0;i<parameters.size();i++) {
            if(parameters.get(i) instanceof String) {
                String raw = (String)parameters.get(i);
                if(raw.startsWith("$(") && raw.endsWith(")")) {
                    raw = raw.substring("$(".length(), raw.length() - ")".length());

                    String[] pathElements = raw.split("\\.");
                    if(pathElements != null && pathElements.length > 0) {
                        JsonElement elemPointer = testData;
                        for(String element : pathElements) {
                            if(elemPointer.isJsonObject() && elemPointer.getAsJsonObject().has(element)) {
                                elemPointer = elemPointer.getAsJsonObject().get(element);
                                continue;
                            }
                            else {
                                elemPointer = null;
                                break;
                            }
                        }
                        if(elemPointer != null && elemPointer.isJsonPrimitive()) {
                            JsonPrimitive primitive = elemPointer.getAsJsonPrimitive();
                            if(primitive.isString() || primitive.isNumber())
                                parameters.set(i, primitive.getAsString());
                            else if(primitive.isBoolean())
                                parameters.set(i, primitive.getAsBoolean());
                        }
                    }
                }
            }
        }
    }
}
 
开发者ID:andrewjc,项目名称:kheera-testrunner-android,代码行数:38,代码来源:FeatureFileExecutor.java

示例11: replaceWithTestData

import com.google.gson.JsonElement; //导入方法依赖的package包/类
private static String replaceWithTestData(final JsonElement sourceTestData, String inputString) {
if(sourceTestData != null) {
        if(inputString != null) {
            if(inputString.startsWith("$(") && inputString.endsWith(")")) {
                inputString = inputString.substring("$(".length(), inputString.length() - ")".length());

                String[] pathElements = inputString.split("\\.");
                if(pathElements != null && pathElements.length > 0) {
                    JsonElement elemPointer = sourceTestData;
                    for(String element : pathElements) {
                        if(elemPointer.isJsonObject() && elemPointer.getAsJsonObject().has(element)) {
                            elemPointer = elemPointer.getAsJsonObject().get(element);
                            continue;
                        }
                        else {
                            elemPointer = null;
                            break;
                        }
                    }
                    if(elemPointer != null && elemPointer.isJsonPrimitive()) {
                        JsonPrimitive primitive = elemPointer.getAsJsonPrimitive();
                        if(primitive.isString() || primitive.isNumber())
                            return primitive.getAsString();
                        else if(primitive.isBoolean())
                            return primitive.getAsBoolean()?"true":"false";
                    }
                }
            }
        }
    }
    return inputString;
}
 
开发者ID:andrewjc,项目名称:kheera-testrunner-android,代码行数:33,代码来源:TableConverter.java

示例12: toString

import com.google.gson.JsonElement; //导入方法依赖的package包/类
/**
 * Gets a human-readable description of the given JsonElement's type.  For example: "a number (4)"
 */
public static String toString(JsonElement p_151222_0_)
{
    String s = org.apache.commons.lang3.StringUtils.abbreviateMiddle(String.valueOf((Object)p_151222_0_), "...", 10);

    if (p_151222_0_ == null)
    {
        return "null (missing)";
    }
    else if (p_151222_0_.isJsonNull())
    {
        return "null (json)";
    }
    else if (p_151222_0_.isJsonArray())
    {
        return "an array (" + s + ")";
    }
    else if (p_151222_0_.isJsonObject())
    {
        return "an object (" + s + ")";
    }
    else
    {
        if (p_151222_0_.isJsonPrimitive())
        {
            JsonPrimitive jsonprimitive = p_151222_0_.getAsJsonPrimitive();

            if (jsonprimitive.isNumber())
            {
                return "a number (" + s + ")";
            }

            if (jsonprimitive.isBoolean())
            {
                return "a boolean (" + s + ")";
            }
        }

        return s;
    }
}
 
开发者ID:Notoh,项目名称:DecompiledMinecraft,代码行数:44,代码来源:JsonUtils.java

示例13: toString

import com.google.gson.JsonElement; //导入方法依赖的package包/类
/**
 * Gets a human-readable description of the given JsonElement's type.  For example: "a number (4)"
 */
public static String toString(JsonElement json)
{
    String s = org.apache.commons.lang3.StringUtils.abbreviateMiddle(String.valueOf((Object)json), "...", 10);

    if (json == null)
    {
        return "null (missing)";
    }
    else if (json.isJsonNull())
    {
        return "null (json)";
    }
    else if (json.isJsonArray())
    {
        return "an array (" + s + ")";
    }
    else if (json.isJsonObject())
    {
        return "an object (" + s + ")";
    }
    else
    {
        if (json.isJsonPrimitive())
        {
            JsonPrimitive jsonprimitive = json.getAsJsonPrimitive();

            if (jsonprimitive.isNumber())
            {
                return "a number (" + s + ")";
            }

            if (jsonprimitive.isBoolean())
            {
                return "a boolean (" + s + ")";
            }
        }

        return s;
    }
}
 
开发者ID:sudofox,项目名称:Backmemed,代码行数:44,代码来源:JsonUtils.java


注:本文中的com.google.gson.JsonElement.getAsJsonPrimitive方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。