Converter.dart 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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_record.dart';
  7. import 'package:sport/bean/game_record_sum.dart';
  8. import 'package:sport/bean/image.dart';
  9. import 'package:sport/bean/message.dart';
  10. import 'package:sport/bean/notice.dart';
  11. import 'package:sport/bean/post.dart';
  12. import 'package:sport/bean/post_user.dart';
  13. import 'package:sport/bean/rank_game_info.dart';
  14. import 'package:sport/bean/rank_info.dart';
  15. import 'package:sport/bean/share_info.dart';
  16. import 'package:sport/bean/shop.dart';
  17. import 'package:sport/bean/sport_detail.dart';
  18. import 'package:sport/bean/sport_index.dart';
  19. import 'package:sport/bean/sport_step.dart';
  20. import 'package:sport/bean/sport_target.dart';
  21. import 'package:sport/bean/sport_target_index.dart';
  22. import 'package:sport/bean/sport_target_record.dart';
  23. import 'package:sport/bean/sport_target_today.dart';
  24. import 'package:sport/bean/user_friend.dart';
  25. import 'package:sport/bean/user_info.dart';
  26. class Converter {
  27. static T fromJson<T>(json) {
  28. if (json == null) return null;
  29. var name = T.toString();
  30. switch (name) {
  31. case "UserInfo":
  32. return UserInfo.fromJson(json) as T;
  33. case "RankInfo":
  34. return RankInfo.fromJson(json) as T;
  35. case "Forum":
  36. return Forum.fromJson(json) as T;
  37. case "Image":
  38. return Image.fromJson(json) as T;
  39. case "Post":
  40. return Post.fromJson(json) as T;
  41. case "Comment":
  42. return Comment.fromJson(json) as T;
  43. case "CommentPost":
  44. return CommentPost.fromJson(json) as T;
  45. case "PostUser":
  46. return PostUser.fromJson(json) as T;
  47. case "SportIndex":
  48. return SportIndex.fromJson(json) as T;
  49. case "SportTarget":
  50. return SportTarget.fromJson(json) as T;
  51. case "SportTargetToday":
  52. return SportTargetToday.fromJson(json) as T;
  53. case "SportTargetIndex":
  54. return SportTargetIndex.fromJson(json) as T;
  55. case "SportTargetRecord":
  56. return SportTargetRecord.fromJson(json) as T;
  57. case "GameRecordSum":
  58. return GameRecordSum.fromJson(json) as T;
  59. case "GameRecord":
  60. return GameRecord.fromJson(json) as T;
  61. case "SportDetail":
  62. return SportDetail.fromJson(json) as T;
  63. case "SportDetailSimple":
  64. return SportDetailSimple.fromJson(json) as T;
  65. case "RecordsTodaySum":
  66. return RecordsTodaySum.fromJson(json) as T;
  67. case "Notice":
  68. return Notice.fromJson(json) as T;
  69. case "UserFriend":
  70. return UserFriend.fromJson(json) as T;
  71. case "GameInfoData":
  72. return GameInfoData.fromJson(json) as T;
  73. case "RankInfoData":
  74. return RankInfoData.fromJson(json) as T;
  75. case "RankGameInfoData":
  76. return RankGameInfoData.fromJson(json) as T;
  77. case "UpdateInfo":
  78. return UpdateInfo.fromJson(json) as T;
  79. case "SportStep":
  80. return SportStep.fromJson(json) as T;
  81. case "Message":
  82. return Message.fromJson(json) as T;
  83. case "NewFriend":
  84. return NewFriend.fromJson(json) as T;
  85. case "MessageInstance":
  86. return MessageInstance.fromJson(json) as T;
  87. case "ChatMessageInstance":
  88. return ChatMessageInstance.fromJson(json) as T;
  89. case "ChatOnlineInfo":
  90. return ChatOnlineInfo.fromJson(json) as T;
  91. case "MessageUser":
  92. return MessageUser.fromJson(json) as T;
  93. case "MessagePostUser":
  94. return MessagePostUser.fromJson(json) as T;
  95. case "ShareInfo":
  96. return ShareInfo.fromJson(json) as T;
  97. case "Share":
  98. return Share.fromJson(json) as T;
  99. case "ShopIndex":
  100. return ShopIndex.fromJson(json) as T;
  101. case "ScoreList":
  102. return ScoreList.fromJson(json) as T;
  103. }
  104. return json as T;
  105. }
  106. static double toDouble(dynamic num) {
  107. if (num == null) return 0.0;
  108. return (num is int) ? num.toDouble() : (num is String) ? double.parse(num) : num;
  109. }
  110. static int toInt(dynamic num) {
  111. if (num == null) return 0;
  112. if (num is String) {
  113. try {
  114. return int.parse(num);
  115. } catch (e) {
  116. return 0;
  117. }
  118. } else {
  119. return num;
  120. }
  121. }
  122. }