Advertisement
Guest User

Untitled

a guest
Oct 7th, 2013
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. library removeme;
  2.  
  3. import 'dart:math';
  4. import 'package:ebisu/ebisu_utils.dart' as ebisu_utils;
  5. // custom <additional imports>
  6. import 'package:plus/pprint.dart';
  7. // end <additional imports>
  8.  
  9.  
  10. class Person {
  11.   String birthDate;
  12.   String deathDate;
  13.   String retirementDate;
  14.  
  15.   // custom <class Person>
  16.   // end <class Person>
  17.  
  18.   Map toJson() {
  19.     return {
  20.     "birthDate": ebisu_utils.toJson(birthDate),
  21.     "deathDate": ebisu_utils.toJson(deathDate),
  22.     "retirementDate": ebisu_utils.toJson(retirementDate),
  23.     };
  24.   }
  25.  
  26.   static Person fromJson(String json) {
  27.     Map jsonMap = convert.JSON.decode(json);
  28.     Person result = new Person();
  29.     result._fromJsonMapImpl(jsonMap);
  30.     return result;
  31.   }
  32.  
  33.   static Person fromJsonMap(Map jsonMap) {
  34.     Person result = new Person();
  35.     result._fromJsonMapImpl(jsonMap);
  36.     return result;
  37.   }
  38.  
  39.   void _fromJsonMapImpl(Map jsonMap) {
  40.     birthDate = jsonMap["birthDate"];
  41.     deathDate = jsonMap["deathDate"];
  42.     retirementDate = jsonMap["retirementDate"];
  43.   }
  44.  
  45.   static Map randJson() {
  46.     return {
  47.     "birthDate": ebisu_utils.randJson(_randomJsonGenerator, String),
  48.     "deathDate": ebisu_utils.randJson(_randomJsonGenerator, String),
  49.     "retirementDate": ebisu_utils.randJson(_randomJsonGenerator, String),
  50.     };
  51.   }
  52.  
  53. }
  54. person() => new Person();
  55.  
  56. class Asset {
  57.   String name;
  58.   int quantity = 1;
  59.   num unitValue = 1.0;
  60.  
  61.   // custom <class Asset>
  62.   // end <class Asset>
  63.  
  64.   Map toJson() {
  65.     return {
  66.     "name": ebisu_utils.toJson(name),
  67.     "quantity": ebisu_utils.toJson(quantity),
  68.     "unitValue": ebisu_utils.toJson(unitValue),
  69.     };
  70.   }
  71.  
  72.   static Asset fromJson(String json) {
  73.     Map jsonMap = convert.JSON.decode(json);
  74.     Asset result = new Asset();
  75.     result._fromJsonMapImpl(jsonMap);
  76.     return result;
  77.   }
  78.  
  79.   static Asset fromJsonMap(Map jsonMap) {
  80.     Asset result = new Asset();
  81.     result._fromJsonMapImpl(jsonMap);
  82.     return result;
  83.   }
  84.  
  85.   void _fromJsonMapImpl(Map jsonMap) {
  86.     name = jsonMap["name"];
  87.     quantity = jsonMap["quantity"];
  88.     unitValue = jsonMap["unitValue"];
  89.   }
  90.  
  91.   static Map randJson() {
  92.     return {
  93.     "name": ebisu_utils.randJson(_randomJsonGenerator, String),
  94.     "quantity": ebisu_utils.randJson(_randomJsonGenerator, int),
  95.     "unitValue": ebisu_utils.randJson(_randomJsonGenerator, num),
  96.     };
  97.   }
  98.  
  99. }
  100. asset() => new Asset();
  101.  
  102. class Dossier {
  103.   Map<String,Person> family;
  104.   Map<String,Asset> assets;
  105.  
  106.   // custom <class Dossier>
  107.   // end <class Dossier>
  108.  
  109.   Map toJson() {
  110.     return {
  111.     "family": ebisu_utils.toJson(family),
  112.     "assets": ebisu_utils.toJson(assets),
  113.     };
  114.   }
  115.  
  116.   static Dossier fromJson(String json) {
  117.     Map jsonMap = convert.JSON.decode(json);
  118.     Dossier result = new Dossier();
  119.     result._fromJsonMapImpl(jsonMap);
  120.     return result;
  121.   }
  122.  
  123.   static Dossier fromJsonMap(Map jsonMap) {
  124.     Dossier result = new Dossier();
  125.     result._fromJsonMapImpl(jsonMap);
  126.     return result;
  127.   }
  128.  
  129.   void _fromJsonMapImpl(Map jsonMap) {
  130.    
  131.     // family is Map<String,Person>
  132.     family = {};
  133.     jsonMap["family"].forEach((k,v) {
  134.       family[k] = (v is Map)?
  135.       Person.fromJsonMap(v) :
  136.       Person.fromJson(v);
  137.     });
  138.  
  139.     // assets is Map<String,Asset>
  140.     assets = {};
  141.     jsonMap["assets"].forEach((k,v) {
  142.       assets[k] = (v is Map)?
  143.       Asset.fromJsonMap(v) :
  144.       Asset.fromJson(v);
  145.     });
  146.   }
  147.  
  148.   static Map randJson() {
  149.     return {
  150.     "family":
  151.        ebisu_utils.randJsonMap(_randomJsonGenerator,
  152.         () => Person.randJson(),
  153.         "family"),
  154.     "assets":
  155.        ebisu_utils.randJsonMap(_randomJsonGenerator,
  156.         () => Asset.randJson(),
  157.         "assets"),
  158.     };
  159.   }
  160.  
  161. }
  162. dossier() => new Dossier();
  163.  
  164. Random _randomJsonGenerator = new Random(0);
  165.  
  166. // custom <library removeme>
  167.  
  168. main() {
  169.   auto d = dossier()
  170.     ..family = {
  171.       "father" : person()
  172.       ..birthDate = "2001/1/1"
  173.       ..deathDate = "2101/1/1"
  174.       ..retirementDate = "2100/1/1", // Obamacare
  175.       "mother" : person()
  176.       ..birthDate = "2005/1/1"
  177.       ..deathDate = "2125/1/1"
  178.       ..retirementDate = "2100/1/1",
  179.     }
  180.   ..assets = {
  181.     "house" : asset()
  182.     ..name = "Home on the Hill"
  183.     ..unitValue = 120000,
  184.     "car" : asset()
  185.     ..name = "Dodge Dart"
  186.     ..unitValue = 500
  187.   };
  188.   print(jp(d.toJson()));
  189. }
  190.  
  191. // end <library removeme>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement