· 7 years ago · Oct 12, 2018, 05:50 PM
1package com.siu.android.tennisparis.json;
2
3import com.google.gson.JsonElement;
4
5import java.text.DateFormat;
6import java.text.SimpleDateFormat;
7import java.util.Date;
8
9/**
10 * @author Lukasz Piliszczuk <lukasz.pili AT gmail.com>
11 */
12public class CommonDeserializer {
13
14 private static DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
15
16 public static Long getLong(JsonElement jsonElement) {
17 if (null == jsonElement) {
18 return null;
19 }
20
21 try {
22 return jsonElement.getAsLong();
23 } catch (Exception e) {
24 return null;
25 }
26 }
27
28 public static Integer getInteger(JsonElement jsonElement) {
29 if (null == jsonElement) {
30 return null;
31 }
32
33 try {
34 return jsonElement.getAsInt();
35 } catch (Exception e) {
36 return null;
37 }
38 }
39
40 public static String getString(JsonElement jsonElement) {
41 if (null == jsonElement) {
42 return null;
43 }
44
45 try {
46 return jsonElement.getAsString();
47 } catch (Exception e) {
48 return null;
49 }
50 }
51
52 public static Double getDouble(JsonElement jsonElement) {
53 if (null == jsonElement) {
54 return null;
55 }
56
57 try {
58 return jsonElement.getAsDouble();
59 } catch (Exception e) {
60 return null;
61 }
62 }
63
64 public static Boolean getBoolean(JsonElement jsonElement) {
65 if (null == jsonElement) {
66 return null;
67 }
68
69 try {
70 return jsonElement.getAsBoolean();
71 } catch (Exception e) {
72 return null;
73 }
74 }
75
76 public static Date getDate(JsonElement jsonElement) {
77 if (null == jsonElement) {
78 return null;
79 }
80
81 try {
82 return dateFormat.parse(jsonElement.getAsString());
83 } catch (Exception e) {
84 return null;
85 }
86 }
87}