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


Java JsonElement.isJsonPrimitive方法代码示例

本文整理汇总了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));
    }
}
 
开发者ID:F1r3w477,项目名称:CustomWorldGen,代码行数:22,代码来源:JsonUtils.java

示例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;
    }
}
 
开发者ID:F1r3w477,项目名称:CustomWorldGen,代码行数:26,代码来源:AnimationMetadataSectionSerializer.java

示例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");
    }
}
 
开发者ID:cubex2,项目名称:customstuff4,代码行数:20,代码来源:InitPhaseDeserializer.java

示例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();
  }
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:19,代码来源:MapTypeAdapterFactory.java

示例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();

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

示例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"));
}
 
开发者ID:LoopPerfect,项目名称:buckaroo,代码行数:15,代码来源:IdentifierDeserializer.java

示例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_));
    }
}
 
开发者ID:Notoh,项目名称:DecompiledMinecraft,代码行数:16,代码来源:JsonUtils.java

示例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_));
    }
}
 
开发者ID:SkidJava,项目名称:BaseClient,代码行数:16,代码来源:JsonUtils.java

示例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");
}
 
开发者ID:F1r3w477,项目名称:CustomWorldGen,代码行数:11,代码来源:MultiLayerModel.java

示例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_));
    }
}
 
开发者ID:Notoh,项目名称:DecompiledMinecraft,代码行数:16,代码来源:JsonUtils.java

示例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);
}
 
开发者ID:UraniumMC,项目名称:Uranium,代码行数:12,代码来源:ComponentSerializer.java

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

示例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));
    }
}
 
开发者ID:sudofox,项目名称:Backmemed,代码行数:16,代码来源:JsonUtils.java

示例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_));
    }
}
 
开发者ID:Notoh,项目名称:DecompiledMinecraft,代码行数:16,代码来源:JsonUtils.java

示例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;
    }
}
 
开发者ID:F1r3w477,项目名称:CustomWorldGen,代码行数:44,代码来源:JsonUtils.java


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