|
7 | 7 | import android.util.Base64;
|
8 | 8 |
|
9 | 9 | import com.google.gson.Gson;
|
| 10 | +import com.google.gson.JsonArray; |
| 11 | +import com.google.gson.JsonElement; |
| 12 | +import com.google.gson.JsonParser; |
10 | 13 | import com.google.gson.reflect.TypeToken;
|
11 | 14 |
|
12 | 15 | import java.io.ByteArrayInputStream;
|
|
15 | 18 | import java.io.ObjectInputStream;
|
16 | 19 | import java.io.ObjectOutputStream;
|
17 | 20 | import java.io.StreamCorruptedException;
|
18 |
| -import java.lang.reflect.Type; |
19 | 21 | import java.util.ArrayList;
|
20 | 22 | import java.util.List;
|
21 | 23 |
|
@@ -360,64 +362,70 @@ public static void clear() {
|
360 | 362 | editor.commit();
|
361 | 363 | }
|
362 | 364 |
|
| 365 | + |
363 | 366 | /**
|
364 |
| - * 保存对象(任意对象类型)可传 List, Map等。 |
| 367 | + * 保存对象(所有类型) |
365 | 368 | *
|
366 | 369 | * @param key
|
367 | 370 | * @param t
|
368 | 371 | * @param <T>
|
369 | 372 | */
|
370 | 373 | public static <T> void setT(String key, T t) {
|
371 |
| - Gson go = new Gson(); |
372 |
| - String str = go.toJson(t); |
373 |
| - setString(key, str); |
| 374 | + setString(key, new Gson().toJson(t)); |
374 | 375 | }
|
375 | 376 |
|
376 |
| - public static Object getObject(String key, Class clazz) { |
377 |
| - Object o = null; |
| 377 | + |
| 378 | + /** |
| 379 | + * 获取保存的对象 |
| 380 | + * |
| 381 | + * @param key |
| 382 | + * @param tClass |
| 383 | + * @param <T> |
| 384 | + * @return |
| 385 | + */ |
| 386 | + public static <T> T getT(String key, Class<T> tClass) { |
| 387 | + T t = null; |
378 | 388 | String str = getString(key, "");
|
379 | 389 | if (!TextUtils.isEmpty(str)) {
|
380 | 390 | Gson go = new Gson();
|
381 |
| - o = go.fromJson(str, clazz); |
| 391 | + t = go.fromJson(str, tClass); |
382 | 392 | }
|
383 |
| - return o; |
| 393 | + return t; |
384 | 394 | }
|
385 | 395 |
|
386 | 396 | /**
|
387 |
| - * recommend to use getT |
| 397 | + * 获取保存的对象,支持所有类型对象。 |
| 398 | + * (主要获取集合的时候使用) |
388 | 399 | *
|
389 | 400 | * @param key
|
390 | 401 | * @param type
|
391 | 402 | * @return
|
392 | 403 | */
|
393 |
| - @Deprecated |
394 |
| - public static Object getObject(String key, Type type) { |
395 |
| - Object o = null; |
| 404 | + public static <T> T getT(String key, TypeToken<T> type) { |
| 405 | + T o = null; |
396 | 406 | String str = getString(key, "");
|
397 | 407 | if (!TextUtils.isEmpty(str)) {
|
398 | 408 | Gson gson = new Gson();
|
399 |
| - o = gson.fromJson(str, type); |
| 409 | + o = gson.fromJson(str, type.getType()); |
400 | 410 | }
|
401 | 411 | return o;
|
402 | 412 | }
|
403 | 413 |
|
404 | 414 | /**
|
405 |
| - * 获取保存的对象 |
| 415 | + * 获取保存的对象(支持非集合类Class) |
406 | 416 | *
|
407 |
| - * @param key key |
408 |
| - * @param t 对象类型 |
| 417 | + * @param key k |
409 | 418 | * @param <T>
|
410 | 419 | * @return t
|
411 | 420 | */
|
412 |
| - public static <T> T getT(String key, T t) { |
413 |
| - |
| 421 | + public static <T> List<T> getList(String key, Class<T> cls) { |
| 422 | + List<T> list = new ArrayList<>(); |
414 | 423 | String str = getString(key, "");
|
415 |
| - if (!TextUtils.isEmpty(str)) { |
416 |
| - Gson gson = new Gson(); |
417 |
| - t = gson.fromJson(str, new TypeToken<T>() { |
418 |
| - }.getType()); |
| 424 | + JsonArray array = new JsonParser().parse(str).getAsJsonArray(); |
| 425 | + for (final JsonElement elem : array) { |
| 426 | + list.add(new Gson().fromJson(elem, cls)); |
419 | 427 | }
|
420 |
| -// return o; |
421 |
| - return t; |
| 428 | + return list; |
422 | 429 | }
|
| 430 | + |
423 | 431 | }
|
0 commit comments