本文整理匯總了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
}