Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. package com.auth0.android.jwt;
  2.  
  3. import android.support.annotation.NonNull;
  4. import android.support.annotation.Nullable;
  5.  
  6. import com.google.gson.Gson;
  7. import com.google.gson.JsonArray;
  8. import com.google.gson.JsonElement;
  9. import com.google.gson.JsonSyntaxException;
  10.  
  11. import java.lang.reflect.Array;
  12. import java.util.ArrayList;
  13. import java.util.Date;
  14. import java.util.List;
  15.  
  16. /**
  17. * The ClaimImpl class implements the Claim interface.
  18. */
  19. @SuppressWarnings("WeakerAccess")
  20. class ClaimImpl extends BaseClaim {
  21.  
  22. private final JsonElement value;
  23.  
  24. ClaimImpl(@NonNull JsonElement value) {
  25. this.value = value;
  26. }
  27.  
  28. @Override
  29. @Nullable
  30. public Boolean asBoolean() {
  31. if (!value.isJsonPrimitive()) {
  32. return null;
  33. }
  34. return value.getAsBoolean();
  35. }
  36.  
  37. @Override
  38. @Nullable
  39. public Integer asInt() {
  40. if (!value.isJsonPrimitive()) {
  41. return null;
  42. }
  43. return value.getAsInt();
  44. }
  45.  
  46. @Override
  47. @Nullable
  48. public Long asLong() {
  49. if (!value.isJsonPrimitive()) {
  50. return null;
  51. }
  52. return value.getAsLong();
  53. }
  54.  
  55. @Override
  56. @Nullable
  57. public Double asDouble() {
  58. if (!value.isJsonPrimitive()) {
  59. return null;
  60. }
  61. return value.getAsDouble();
  62. }
  63.  
  64. @Override
  65. @Nullable
  66. public String asString() {
  67. if (!value.isJsonPrimitive()) {
  68. return null;
  69. }
  70. return value.getAsString();
  71. }
  72.  
  73. @Override
  74. @Nullable
  75. public Date asDate() {
  76. if (!value.isJsonPrimitive()) {
  77. return null;
  78. }
  79. long ms = Long.parseLong(value.getAsString()) * 1000;
  80. return new Date(ms);
  81. }
  82.  
  83. @Override
  84. @SuppressWarnings("unchecked")
  85. public <T> T[] asArray(Class<T> tClazz) throws DecodeException {
  86. try {
  87. if (!value.isJsonArray() || value.isJsonNull()) {
  88. return (T[]) Array.newInstance(tClazz, 0);
  89. }
  90. Gson gson = new Gson();
  91. JsonArray jsonArr = value.getAsJsonArray();
  92. T[] arr = (T[]) Array.newInstance(tClazz, jsonArr.size());
  93. for (int i = 0; i < jsonArr.size(); i++) {
  94. arr[i] = gson.fromJson(jsonArr.get(i), tClazz);
  95. }
  96. return arr;
  97. } catch (JsonSyntaxException e) {
  98. throw new DecodeException("Failed to decode claim as array", e);
  99. }
  100. }
  101.  
  102. @Override
  103. public <T> List<T> asList(Class<T> tClazz) throws DecodeException {
  104. try {
  105. if (!value.isJsonArray() || value.isJsonNull()) {
  106. return new ArrayList<>();
  107. }
  108. Gson gson = new Gson();
  109. JsonArray jsonArr = value.getAsJsonArray();
  110. List<T> list = new ArrayList<>();
  111. for (int i = 0; i < jsonArr.size(); i++) {
  112. list.add(gson.fromJson(jsonArr.get(i), tClazz));
  113. }
  114. return list;
  115. } catch (JsonSyntaxException e) {
  116. throw new DecodeException("Failed to decode claim as list", e);
  117. }
  118. }
  119.  
  120. @Override
  121. public <T> T asObject(Class<T> tClazz) throws DecodeException {
  122. try {
  123. if (value.isJsonNull()) {
  124. return null;
  125. }
  126. return new Gson().fromJson(value, tClazz);
  127. } catch (JsonSyntaxException e) {
  128. throw new DecodeException("Failed to decode claim as " + tClazz.getSimpleName(), e);
  129. }
  130. }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement