Converter.dart 4.1 KB

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