Converter.dart 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import 'package:sport/bean/UpdateInfo.dart';
  2. import 'package:sport/bean/comment.dart';
  3. import 'package:sport/bean/comment_post.dart';
  4. import 'package:sport/bean/forum.dart';
  5. import 'package:sport/bean/game.dart';
  6. import 'package:sport/bean/game_add_entity.dart';
  7. import 'package:sport/bean/game_record.dart';
  8. import 'package:sport/bean/game_record_sum.dart';
  9. import 'package:sport/bean/hardware.dart';
  10. import 'package:sport/bean/image.dart';
  11. import 'package:sport/bean/jog/detail.dart';
  12. import 'package:sport/bean/jog/record.dart';
  13. import 'package:sport/bean/jog/sum.dart';
  14. import 'package:sport/bean/login.dart';
  15. import 'package:sport/bean/message.dart';
  16. import 'package:sport/bean/notice.dart';
  17. import 'package:sport/bean/post.dart';
  18. import 'package:sport/bean/post_user.dart';
  19. import 'package:sport/bean/rank_game_info.dart';
  20. import 'package:sport/bean/rank_info.dart';
  21. import 'package:sport/bean/share_info.dart';
  22. import 'package:sport/bean/shop.dart';
  23. import 'package:sport/bean/sport_detail.dart';
  24. import 'package:sport/bean/sport_index.dart';
  25. import 'package:sport/bean/sport_step.dart';
  26. import 'package:sport/bean/sport_target.dart';
  27. import 'package:sport/bean/sport_target_index.dart';
  28. import 'package:sport/bean/sport_target_record.dart';
  29. import 'package:sport/bean/sport_target_today.dart';
  30. import 'package:sport/bean/user_friend.dart';
  31. import 'package:sport/bean/user_info.dart';
  32. import 'package:sport/bean/exer_index.dart';
  33. import 'package:sport/bean/achievement_rule.dart';
  34. import 'package:sport/bean/friend_list.dart';
  35. import 'package:sport/bean/friend_relation.dart';
  36. import 'package:sport/bean/game_record_history.dart';
  37. class Converter {
  38. static T? fromJson<T>(json) {
  39. if (json == null) return null;
  40. var name = T.toString();
  41. switch (name) {
  42. case "User":
  43. return User.fromJson(json) as T;
  44. case "UserInfo":
  45. return UserInfo.fromJson(json) as T;
  46. case "RankInfo":
  47. return RankInfo.fromJson(json) as T;
  48. case "Forum":
  49. return Forum.fromJson(json) as T;
  50. case "Image":
  51. return Image.fromJson(json) as T;
  52. case "Post":
  53. return Post.fromJson(json) as T;
  54. case "Comment":
  55. return Comment.fromJson(json) as T;
  56. case "CommentPost":
  57. return CommentPost.fromJson(json) as T;
  58. case "PostUser":
  59. return PostUser.fromJson(json) as T;
  60. case "SportIndex":
  61. return SportIndex.fromJson(json) as T;
  62. case "SportTarget":
  63. return SportTarget.fromJson(json) as T;
  64. case "SportTargetToday":
  65. return SportTargetToday.fromJson(json) as T;
  66. case "SportTargetIndex":
  67. return SportTargetIndex.fromJson(json) as T;
  68. case "SportTargetRecord":
  69. return SportTargetRecord.fromJson(json) as T;
  70. case "GameRecordSum":
  71. return GameRecordSum.fromJson(json) as T;
  72. case "GameRecord":
  73. return GameRecord.fromJson(json) as T;
  74. case "SportDetail":
  75. return SportDetail.fromJson(json) as T;
  76. case "SportDetailSimple":
  77. return SportDetailSimple.fromJson(json) as T;
  78. case "RecordsTodaySum":
  79. return RecordsTodaySum.fromJson(json) as T;
  80. case "Notice":
  81. return Notice.fromJson(json) as T;
  82. case "UserFriend":
  83. return UserFriend.fromJson(json) as T;
  84. case "GameInfoData":
  85. return GameInfoData.fromJson(json) as T;
  86. case "RankInfoData":
  87. return RankInfoData.fromJson(json) as T;
  88. case "RankGameInfoData":
  89. return RankGameInfoData.fromJson(json) as T;
  90. case "UpdateInfo":
  91. return UpdateInfo.fromJson(json) as T;
  92. case "SportStep":
  93. return SportStep.fromJson(json) as T;
  94. case "Message":
  95. return Message.fromJson(json) as T;
  96. case "NewFriend":
  97. return NewFriend.fromJson(json) as T;
  98. case "MessageInstance":
  99. return MessageInstance.fromJson(json) as T;
  100. case "ChatMessageInstance":
  101. return ChatMessageInstance.fromJson(json) as T;
  102. case "ChatOnlineInfo":
  103. return ChatOnlineInfo.fromJson(json) as T;
  104. case "MessageUser":
  105. return MessageUser.fromJson(json) as T;
  106. case "MessagePostUser":
  107. return MessagePostUser.fromJson(json) as T;
  108. case "ShareInfo":
  109. return ShareInfo.fromJson(json) as T;
  110. case "Share":
  111. return Share.fromJson(json) as T;
  112. case "ShopIndex":
  113. return ShopIndex.fromJson(json) as T;
  114. case "ScoreList":
  115. return ScoreList.fromJson(json) as T;
  116. case "JogDetail":
  117. return JogDetail.fromJson(json) as T;
  118. case "JogSum":
  119. return JogSum.fromJson(json) as T;
  120. case "JogRecord":
  121. return JogRecord.fromJson(json) as T;
  122. case "ExerIndex":
  123. return ExerIndex.fromJson(json) as T;
  124. case "AchievementRule":
  125. return AchievementRule.fromJson(json) as T;
  126. case "FriendList":
  127. return FriendList.fromJson(json) as T;
  128. case "FriendRelation":
  129. return FriendRelation.fromJson(json) as T;
  130. case "GameRecordHistory":
  131. return GameRecordHistory.fromJson(json) as T;
  132. case "Hardware":
  133. return Hardware.fromJson(json) as T;
  134. case "LoginInfo":
  135. return LoginInfo.fromJson(json) as T;
  136. case "SportHistory":
  137. return SportHistory.fromJson(json) as T;
  138. case "GameAddEntity":
  139. return GameAddEntity.fromJson(json) as T;
  140. case "String":
  141. return "$json" as T;
  142. }
  143. return json as T;
  144. }
  145. static double toDouble(dynamic num) {
  146. if (num == null) return 0.0;
  147. return (num is int)
  148. ? num.toDouble()
  149. : (num is String)
  150. ? double.parse(num)
  151. : num;
  152. }
  153. static int toInt(dynamic v) {
  154. if (v == null) return 0;
  155. if (v is String) {
  156. try {
  157. return int.parse(v);
  158. } catch (e) {
  159. return 0;
  160. }
  161. } else if (v is double) {
  162. try {
  163. return v.toInt();
  164. } catch (e) {
  165. return 0;
  166. }
  167. } else if (v is int) {
  168. return v;
  169. } else if (v is num) {
  170. return v.toInt();
  171. }
  172. return 0;
  173. }
  174. }