本文整理汇总了Java中com.google.gson.JsonElement.isJsonPrimitive方法的典型用法代码示例。如果您正苦于以下问题:Java JsonElement.isJsonPrimitive方法的具体用法?Java JsonElement.isJsonPrimitive怎么用?Java JsonElement.isJsonPrimitive使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.gson.JsonElement
的用法示例。
在下文中一共展示了JsonElement.isJsonPrimitive方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getItem
import com.google.gson.JsonElement; //导入方法依赖的package包/类
public static Item getItem(JsonElement json, String memberName)
{
if (json.isJsonPrimitive())
{
String s = json.getAsString();
Item item = Item.getByNameOrId(s);
if (item == null)
{
throw new JsonSyntaxException("Expected " + memberName + " to be an item, was unknown string \'" + s + "\'");
}
else
{
return item;
}
}
else
{
throw new JsonSyntaxException("Expected " + memberName + " to be an item, was " + toString(json));
}
}
示例2: parseAnimationFrame
import com.google.gson.JsonElement; //导入方法依赖的package包/类
private AnimationFrame parseAnimationFrame(int frame, JsonElement element)
{
if (element.isJsonPrimitive())
{
return new AnimationFrame(JsonUtils.getInt(element, "frames[" + frame + "]"));
}
else if (element.isJsonObject())
{
JsonObject jsonobject = JsonUtils.getJsonObject(element, "frames[" + frame + "]");
int i = JsonUtils.getInt(jsonobject, "time", -1);
if (jsonobject.has("time"))
{
Validate.inclusiveBetween(1L, 2147483647L, (long)i, "Invalid frame time");
}
int j = JsonUtils.getInt(jsonobject, "index");
Validate.inclusiveBetween(0L, 2147483647L, (long)j, "Invalid frame index");
return new AnimationFrame(j, i);
}
else
{
return null;
}
}
示例3: deserialize
import com.google.gson.JsonElement; //导入方法依赖的package包/类
@Override
public InitPhase deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
{
if (json.isJsonPrimitive() && json.getAsJsonPrimitive().isString())
{
String phase = json.getAsString();
for (InitPhase initPhase : InitPhase.values())
{
if (initPhase.name.equalsIgnoreCase(phase))
return initPhase;
}
throw new JsonParseException("Invalid phase: " + phase);
} else
{
throw new JsonParseException("Invalid phase attribute");
}
}
示例4: 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();
}
}
示例5: 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();
}
示例6: deserialize
import com.google.gson.JsonElement; //导入方法依赖的package包/类
@Override
public Identifier deserialize(final JsonElement jsonElement, final Type type, JsonDeserializationContext context) throws JsonParseException {
Preconditions.checkNotNull(jsonElement);
Preconditions.checkNotNull(type);
Preconditions.checkNotNull(context);
if (!jsonElement.isJsonPrimitive()) {
throw new JsonParseException("Expected a string");
}
return Identifier.parse(jsonElement.getAsString())
.orElseThrow(() -> new JsonParseException("\"" + jsonElement.getAsString() + "\" is not a valid name"));
}
示例7: getString
import com.google.gson.JsonElement; //导入方法依赖的package包/类
/**
* Gets the string value of the given JsonElement. Expects the second parameter to be the name of the element's
* field if an error message needs to be thrown.
*/
public static String getString(JsonElement p_151206_0_, String p_151206_1_)
{
if (p_151206_0_.isJsonPrimitive())
{
return p_151206_0_.getAsString();
}
else
{
throw new JsonSyntaxException("Expected " + p_151206_1_ + " to be a string, was " + toString(p_151206_0_));
}
}
示例8: getBoolean
import com.google.gson.JsonElement; //导入方法依赖的package包/类
/**
* Gets the boolean value of the given JsonElement. Expects the second parameter to be the name of the element's
* field if an error message needs to be thrown.
*/
public static boolean getBoolean(JsonElement p_151216_0_, String p_151216_1_)
{
if (p_151216_0_.isJsonPrimitive())
{
return p_151216_0_.getAsBoolean();
}
else
{
throw new JsonSyntaxException("Expected " + p_151216_1_ + " to be a Boolean, was " + toString(p_151216_0_));
}
}
示例9: getLocation
import com.google.gson.JsonElement; //导入方法依赖的package包/类
private ModelResourceLocation getLocation(String json)
{
JsonElement e = new JsonParser().parse(json);
if(e.isJsonPrimitive() && e.getAsJsonPrimitive().isString())
{
return new ModelResourceLocation(e.getAsString());
}
FMLLog.severe("Expect ModelResourceLocation, got: ", json);
return new ModelResourceLocation("builtin/missing", "missing");
}
示例10: getFloat
import com.google.gson.JsonElement; //导入方法依赖的package包/类
/**
* Gets the float value of the given JsonElement. Expects the second parameter to be the name of the element's
* field if an error message needs to be thrown.
*/
public static float getFloat(JsonElement p_151220_0_, String p_151220_1_)
{
if (p_151220_0_.isJsonPrimitive() && p_151220_0_.getAsJsonPrimitive().isNumber())
{
return p_151220_0_.getAsFloat();
}
else
{
throw new JsonSyntaxException("Expected " + p_151220_1_ + " to be a Float, was " + toString(p_151220_0_));
}
}
示例11: deserialize
import com.google.gson.JsonElement; //导入方法依赖的package包/类
@Override
public BaseComponent deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
if (json.isJsonPrimitive()) {
return new TextComponent(json.getAsString());
}
JsonObject object = json.getAsJsonObject();
if (object.has("translate")) {
return (BaseComponent)context.deserialize(json, TranslatableComponent.class);
}
return (BaseComponent)context.deserialize(json, TextComponent.class);
}
示例12: 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;
}
示例13: getInt
import com.google.gson.JsonElement; //导入方法依赖的package包/类
/**
* Gets the integer value of the given JsonElement. Expects the second parameter to be the name of the element's
* field if an error message needs to be thrown.
*/
public static int getInt(JsonElement json, String memberName)
{
if (json.isJsonPrimitive() && json.getAsJsonPrimitive().isNumber())
{
return json.getAsInt();
}
else
{
throw new JsonSyntaxException("Expected " + memberName + " to be a Int, was " + toString(json));
}
}
示例14: getInt
import com.google.gson.JsonElement; //导入方法依赖的package包/类
/**
* Gets the integer value of the given JsonElement. Expects the second parameter to be the name of the element's
* field if an error message needs to be thrown.
*/
public static int getInt(JsonElement p_151215_0_, String p_151215_1_)
{
if (p_151215_0_.isJsonPrimitive() && p_151215_0_.getAsJsonPrimitive().isNumber())
{
return p_151215_0_.getAsInt();
}
else
{
throw new JsonSyntaxException("Expected " + p_151215_1_ + " to be a Int, was " + toString(p_151215_0_));
}
}
示例15: 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;
}
}