diff tests/testJson.ur @ 22:8de201d70b91

Implement JSON typeclass for polymorphic variants.
author Edward Z. Yang <ezyang@mit.edu>
date Wed, 02 May 2012 11:47:37 -0400
parents 189245a3c075
children 9d6b931fbd13
line wrap: on
line diff
--- a/tests/testJson.ur	Thu Jan 05 18:04:04 2012 -0500
+++ b/tests/testJson.ur	Wed May 02 11:47:37 2012 -0400
@@ -7,7 +7,11 @@
                 State : string,
                 PostalCode : string}
 
-type phoneNumber = {Type_ : string,
+type phoneType = variant [Mobile = string,
+                          LandLine = unit,
+                          Secret = unit]
+
+type phoneNumber = {Type_ : phoneType,
                     Number : string}
 
 type person = {FirstName : string,
@@ -23,8 +27,8 @@
                                   City = "Hoserville",
                                   State = "QQ",
                                   PostalCode = "66666"},
-                       PhoneNumber = {Type_ = "mobile", Number = "1234"}
-                                         :: {Type_ = "secret", Number = "ssssh"}
+                       PhoneNumber = {Type_ = make [#Mobile] "Verizon", Number = "1234"}
+                                         :: {Type_ = make [#Secret] (), Number = "ssssh"}
                                          :: []}
 
 val json_address : json address = json_record {StreetAddress = "streetAddress",
@@ -32,6 +36,10 @@
                                                State = "state",
                                                PostalCode = "postalCode"}
 
+val json_phoneType : json phoneType = json_variant {Mobile = "mobile",
+                                                    LandLine = "landline",
+                                                    Secret = "secret"}
+
 val json_phoneNumber : json phoneNumber = json_record {Type_ = "type",
                                                        Number = "number"}
 
@@ -49,7 +57,7 @@
   <b>City:</b> {[p.Address.City]}<br/>
   <b>State:</b> {[p.Address.State]}<br/>
   <b>Postal code:</b> {[p.Address.PostalCode]}<br/>
-  <b>Phone numbers:</b> {List.mapX (fn pn => <xml>{[pn.Type_]} => {[pn.Number]}; </xml>) p.PhoneNumber}<br/>
+  <b>Phone numbers:</b> {List.mapX (fn pn => <xml>{[pn.Number]}; </xml>) p.PhoneNumber}<br/>
 </xml>
 
 fun parse r = return <xml><body>