本文整理汇总了Java中com.google.gson.JsonPrimitive.isNumber方法的典型用法代码示例。如果您正苦于以下问题:Java JsonPrimitive.isNumber方法的具体用法?Java JsonPrimitive.isNumber怎么用?Java JsonPrimitive.isNumber使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.gson.JsonPrimitive
的用法示例。
在下文中一共展示了JsonPrimitive.isNumber方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: keyToString
import com.google.gson.JsonPrimitive; //导入方法依赖的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();
}
}
示例2: convert
import com.google.gson.JsonPrimitive; //导入方法依赖的package包/类
@Override
public JsonPrimitive convert(JsonPrimitive value) {
if (value == null) {
return null;
}
if (value.isNumber()) {
return new JsonPrimitive(String.valueOf(value.getAsInt()));
}
try {
Integer.parseInt(value.getAsString());
// if it parses correctly, it is validated:
return value;
} catch (NumberFormatException ex) {
throw new ConverterException(value, this);
}
}
示例3: Parameter
import com.google.gson.JsonPrimitive; //导入方法依赖的package包/类
public Parameter(JsonObject param) {
try {
setNameAndType(param.get("name").getAsString(), param.get("type").getAsString());
if(param.get("value").isJsonObject()){
setJsonContent(param.getAsJsonObject("value"));
} else {
JsonPrimitive value = param.getAsJsonPrimitive("value");
if(value.isString()) {
setStringContent(value.getAsString());
} else if(value.isNumber()) {
setDoubleContent(value.getAsDouble());
} else if(value.isBoolean()) {
setBooleanContent(value.getAsBoolean());
}
}
} catch (Exception e){
throw new IllegalArgumentException("invalid parameter structure");
}
}
示例4: decodeFromPrimitive
import com.google.gson.JsonPrimitive; //导入方法依赖的package包/类
private Variant decodeFromPrimitive ( final JsonElement json )
{
final JsonPrimitive jsonPrim = (JsonPrimitive)json;
if ( jsonPrim.isBoolean () )
{
return Variant.valueOf ( jsonPrim.getAsBoolean () );
}
else if ( jsonPrim.isNumber () )
{
return Variant.valueOf ( jsonPrim.getAsNumber () );
}
else
{
return VariantEditor.toVariant ( jsonPrim.getAsString () );
}
}
示例5: findType
import com.google.gson.JsonPrimitive; //导入方法依赖的package包/类
private String findType(JsonElement value) throws ValidationException
{
if (!value.isJsonPrimitive()){
throw new ValidationException("value is an invalid type");
}
JsonPrimitive primitiveValue = (JsonPrimitive) value;
if (primitiveValue.isNumber() || (primitiveValue.isString() && Util.isNumber(value.getAsString())))
{
String v = value.getAsString();
if (!v.contains("."))
{
return "long";
}
else
{
return "double";
}
}
else
return "string";
}
示例6: keyToString
import com.google.gson.JsonPrimitive; //导入方法依赖的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();
}
}
示例7: deserialize
import com.google.gson.JsonPrimitive; //导入方法依赖的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();
}
示例8: unwarpPrimitive
import com.google.gson.JsonPrimitive; //导入方法依赖的package包/类
@Override
public Object unwarpPrimitive(JsonPrimitive primitive) {
if (primitive.isBoolean()) {
return primitive.getAsBoolean();
} else if (primitive.isNumber()) {
return primitive.getAsNumber();
} else if (primitive.isString()) {
return primitive.getAsString();
} else {
throw new IllegalArgumentException("Unknown primitive type: " + primitive);
}
}
示例9: deserialize
import com.google.gson.JsonPrimitive; //导入方法依赖的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;
}
示例10: getTypeFor
import com.google.gson.JsonPrimitive; //导入方法依赖的package包/类
private org.eclipse.leshan.core.model.ResourceModel.Type getTypeFor(JsonPrimitive val) {
if (val.isBoolean())
return org.eclipse.leshan.core.model.ResourceModel.Type.BOOLEAN;
if (val.isString())
return org.eclipse.leshan.core.model.ResourceModel.Type.STRING;
if (val.isNumber()) {
if (val.getAsDouble() == val.getAsLong()) {
return org.eclipse.leshan.core.model.ResourceModel.Type.INTEGER;
} else {
return org.eclipse.leshan.core.model.ResourceModel.Type.FLOAT;
}
}
// use string as default value
return org.eclipse.leshan.core.model.ResourceModel.Type.STRING;
}
示例11: write
import com.google.gson.JsonPrimitive; //导入方法依赖的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());
}
}
示例12: toString
import com.google.gson.JsonPrimitive; //导入方法依赖的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;
}
}
示例13: toString
import com.google.gson.JsonPrimitive; //导入方法依赖的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;
}
}
示例14: peek
import com.google.gson.JsonPrimitive; //导入方法依赖的package包/类
public JsonToken peek() throws IOException {
if (this.stack.isEmpty()) {
return JsonToken.END_DOCUMENT;
}
Iterator<?> o = peekStack();
if (o instanceof Iterator) {
boolean isObject = this.stack.get(this.stack.size() - 2) instanceof JsonObject;
Iterator<?> iterator = o;
if (!iterator.hasNext()) {
return isObject ? JsonToken.END_OBJECT : JsonToken.END_ARRAY;
} else {
if (isObject) {
return JsonToken.NAME;
}
this.stack.add(iterator.next());
return peek();
}
} else if (o instanceof JsonObject) {
return JsonToken.BEGIN_OBJECT;
} else {
if (o instanceof JsonArray) {
return JsonToken.BEGIN_ARRAY;
}
if (o instanceof JsonPrimitive) {
JsonPrimitive primitive = (JsonPrimitive) o;
if (primitive.isString()) {
return JsonToken.STRING;
}
if (primitive.isBoolean()) {
return JsonToken.BOOLEAN;
}
if (primitive.isNumber()) {
return JsonToken.NUMBER;
}
throw new AssertionError();
} else if (o instanceof JsonNull) {
return JsonToken.NULL;
} else {
if (o == SENTINEL_CLOSED) {
throw new IllegalStateException("JsonReader is closed");
}
throw new AssertionError();
}
}
}
示例15: peek
import com.google.gson.JsonPrimitive; //导入方法依赖的package包/类
@Override public JsonToken peek() throws IOException {
if (stack.isEmpty()) {
return JsonToken.END_DOCUMENT;
}
Object o = peekStack();
if (o instanceof Iterator) {
boolean isObject = stack.get(stack.size() - 2) instanceof JsonObject;
Iterator<?> iterator = (Iterator<?>) o;
if (iterator.hasNext()) {
if (isObject) {
return JsonToken.NAME;
} else {
stack.add(iterator.next());
return peek();
}
} else {
return isObject ? JsonToken.END_OBJECT : JsonToken.END_ARRAY;
}
} else if (o instanceof JsonObject) {
return JsonToken.BEGIN_OBJECT;
} else if (o instanceof JsonArray) {
return JsonToken.BEGIN_ARRAY;
} else if (o instanceof JsonPrimitive) {
JsonPrimitive primitive = (JsonPrimitive) o;
if (primitive.isString()) {
return JsonToken.STRING;
} else if (primitive.isBoolean()) {
return JsonToken.BOOLEAN;
} else if (primitive.isNumber()) {
return JsonToken.NUMBER;
} else {
throw new AssertionError();
}
} else if (o instanceof JsonNull) {
return JsonToken.NULL;
} else if (o == SENTINEL_CLOSED) {
throw new IllegalStateException("JsonReader is closed");
} else {
throw new AssertionError();
}
}