本文整理汇总了Java中com.google.gson.JsonPrimitive.getAsNumber方法的典型用法代码示例。如果您正苦于以下问题:Java JsonPrimitive.getAsNumber方法的具体用法?Java JsonPrimitive.getAsNumber怎么用?Java JsonPrimitive.getAsNumber使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.gson.JsonPrimitive
的用法示例。
在下文中一共展示了JsonPrimitive.getAsNumber方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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);
}
}
示例2: read
import com.google.gson.JsonPrimitive; //导入方法依赖的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;
}
示例3: importPreferencesFromJson
import com.google.gson.JsonPrimitive; //导入方法依赖的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
}