|
| 1 | +// To parse this JSON data, do |
| 2 | +// |
| 3 | +// final continents = continentsFromJson(jsonString); |
| 4 | + |
| 5 | +import 'dart:convert'; |
| 6 | + |
| 7 | +List<Continents> continentsFromJson(String str) => |
| 8 | + List<Continents>.from(json.decode(str).map((x) => Continents.fromJson(x))); |
| 9 | + |
| 10 | +String continentsToJson(List<Continents> data) => |
| 11 | + json.encode(List<dynamic>.from(data.map((x) => x.toJson()))); |
| 12 | + |
| 13 | +class Continents { |
| 14 | + Continents({ |
| 15 | + this.code, |
| 16 | + this.name, |
| 17 | + this.countries, |
| 18 | + this.links, |
| 19 | + }); |
| 20 | + |
| 21 | + String code; |
| 22 | + String name; |
| 23 | + List<Country> countries; |
| 24 | + Links links; |
| 25 | + |
| 26 | + factory Continents.fromJson(Map<String, dynamic> json) => Continents( |
| 27 | + code: json["code"], |
| 28 | + name: json["name"], |
| 29 | + countries: List<Country>.from( |
| 30 | + json["countries"].map((x) => Country.fromJson(x))), |
| 31 | + links: Links.fromJson(json["_links"]), |
| 32 | + ); |
| 33 | + |
| 34 | + Map<String, dynamic> toJson() => { |
| 35 | + "code": code, |
| 36 | + "name": name, |
| 37 | + "countries": List<dynamic>.from(countries.map((x) => x.toJson())), |
| 38 | + "_links": links.toJson(), |
| 39 | + }; |
| 40 | +} |
| 41 | + |
| 42 | +class Country { |
| 43 | + Country({ |
| 44 | + this.code, |
| 45 | + this.name, |
| 46 | + this.states, |
| 47 | + this.currencyCode, |
| 48 | + this.currencyPos, |
| 49 | + this.decimalSep, |
| 50 | + this.dimensionUnit, |
| 51 | + this.numDecimals, |
| 52 | + this.thousandSep, |
| 53 | + this.weightUnit, |
| 54 | + }); |
| 55 | + |
| 56 | + String code; |
| 57 | + String name; |
| 58 | + List<State> states; |
| 59 | + String currencyCode; |
| 60 | + CurrencyPos currencyPos; |
| 61 | + Sep decimalSep; |
| 62 | + DimensionUnit dimensionUnit; |
| 63 | + int numDecimals; |
| 64 | + Sep thousandSep; |
| 65 | + WeightUnit weightUnit; |
| 66 | + |
| 67 | + factory Country.fromJson(Map<String, dynamic> json) => Country( |
| 68 | + code: json["code"], |
| 69 | + name: json["name"], |
| 70 | + states: List<State>.from(json["states"].map((x) => State.fromJson(x))), |
| 71 | + currencyCode: |
| 72 | + json["currency_code"] == null ? null : json["currency_code"], |
| 73 | + currencyPos: json["currency_pos"] == null |
| 74 | + ? null |
| 75 | + : currencyPosValues.map[json["currency_pos"]], |
| 76 | + decimalSep: json["decimal_sep"] == null |
| 77 | + ? null |
| 78 | + : sepValues.map[json["decimal_sep"]], |
| 79 | + dimensionUnit: json["dimension_unit"] == null |
| 80 | + ? null |
| 81 | + : dimensionUnitValues.map[json["dimension_unit"]], |
| 82 | + numDecimals: json["num_decimals"] == null ? null : json["num_decimals"], |
| 83 | + thousandSep: json["thousand_sep"] == null |
| 84 | + ? null |
| 85 | + : sepValues.map[json["thousand_sep"]], |
| 86 | + weightUnit: json["weight_unit"] == null |
| 87 | + ? null |
| 88 | + : weightUnitValues.map[json["weight_unit"]], |
| 89 | + ); |
| 90 | + |
| 91 | + Map<String, dynamic> toJson() => { |
| 92 | + "code": code, |
| 93 | + "name": name, |
| 94 | + "states": List<dynamic>.from(states.map((x) => x.toJson())), |
| 95 | + "currency_code": currencyCode == null ? null : currencyCode, |
| 96 | + "currency_pos": |
| 97 | + currencyPos == null ? null : currencyPosValues.reverse[currencyPos], |
| 98 | + "decimal_sep": |
| 99 | + decimalSep == null ? null : sepValues.reverse[decimalSep], |
| 100 | + "dimension_unit": dimensionUnit == null |
| 101 | + ? null |
| 102 | + : dimensionUnitValues.reverse[dimensionUnit], |
| 103 | + "num_decimals": numDecimals == null ? null : numDecimals, |
| 104 | + "thousand_sep": |
| 105 | + thousandSep == null ? null : sepValues.reverse[thousandSep], |
| 106 | + "weight_unit": |
| 107 | + weightUnit == null ? null : weightUnitValues.reverse[weightUnit], |
| 108 | + }; |
| 109 | +} |
| 110 | + |
| 111 | +enum CurrencyPos { LEFT, LEFT_SPACE, RIGHT, RIGHT_SPACE } |
| 112 | + |
| 113 | +final currencyPosValues = EnumValues({ |
| 114 | + "left": CurrencyPos.LEFT, |
| 115 | + "left_space": CurrencyPos.LEFT_SPACE, |
| 116 | + "right": CurrencyPos.RIGHT, |
| 117 | + "right_space": CurrencyPos.RIGHT_SPACE |
| 118 | +}); |
| 119 | + |
| 120 | +enum Sep { EMPTY, SEP, PURPLE } |
| 121 | + |
| 122 | +final sepValues = EnumValues({".": Sep.EMPTY, " ": Sep.PURPLE, ",": Sep.SEP}); |
| 123 | + |
| 124 | +enum DimensionUnit { CM, IN } |
| 125 | + |
| 126 | +final dimensionUnitValues = |
| 127 | + EnumValues({"cm": DimensionUnit.CM, "in": DimensionUnit.IN}); |
| 128 | + |
| 129 | +class State { |
| 130 | + State({ |
| 131 | + this.code, |
| 132 | + this.name, |
| 133 | + }); |
| 134 | + |
| 135 | + String code; |
| 136 | + String name; |
| 137 | + |
| 138 | + factory State.fromJson(Map<String, dynamic> json) => State( |
| 139 | + code: json["code"], |
| 140 | + name: json["name"], |
| 141 | + ); |
| 142 | + |
| 143 | + Map<String, dynamic> toJson() => { |
| 144 | + "code": code, |
| 145 | + "name": name, |
| 146 | + }; |
| 147 | +} |
| 148 | + |
| 149 | +enum WeightUnit { KG, OZ } |
| 150 | + |
| 151 | +final weightUnitValues = EnumValues({"kg": WeightUnit.KG, "oz": WeightUnit.OZ}); |
| 152 | + |
| 153 | +class Links { |
| 154 | + Links({ |
| 155 | + this.self, |
| 156 | + this.collection, |
| 157 | + }); |
| 158 | + |
| 159 | + List<Collection> self; |
| 160 | + List<Collection> collection; |
| 161 | + |
| 162 | + factory Links.fromJson(Map<String, dynamic> json) => Links( |
| 163 | + self: List<Collection>.from( |
| 164 | + json["self"].map((x) => Collection.fromJson(x))), |
| 165 | + collection: List<Collection>.from( |
| 166 | + json["collection"].map((x) => Collection.fromJson(x))), |
| 167 | + ); |
| 168 | + |
| 169 | + Map<String, dynamic> toJson() => { |
| 170 | + "self": List<dynamic>.from(self.map((x) => x.toJson())), |
| 171 | + "collection": List<dynamic>.from(collection.map((x) => x.toJson())), |
| 172 | + }; |
| 173 | +} |
| 174 | + |
| 175 | +class Collection { |
| 176 | + Collection({ |
| 177 | + this.href, |
| 178 | + }); |
| 179 | + |
| 180 | + String href; |
| 181 | + |
| 182 | + factory Collection.fromJson(Map<String, dynamic> json) => Collection( |
| 183 | + href: json["href"], |
| 184 | + ); |
| 185 | + |
| 186 | + Map<String, dynamic> toJson() => { |
| 187 | + "href": href, |
| 188 | + }; |
| 189 | +} |
| 190 | + |
| 191 | +class EnumValues<T> { |
| 192 | + Map<String, T> map; |
| 193 | + Map<T, String> reverseMap; |
| 194 | + |
| 195 | + EnumValues(this.map); |
| 196 | + |
| 197 | + Map<T, String> get reverse { |
| 198 | + if (reverseMap == null) { |
| 199 | + reverseMap = map.map((k, v) => new MapEntry(v, k)); |
| 200 | + } |
| 201 | + return reverseMap; |
| 202 | + } |
| 203 | +} |
0 commit comments