Skip to content
Browse files

Working on the automatic serialization example

  • Loading branch information...
1 parent 7a1c669 commit aa65beb40a2bc946d1e051e636c611ef1dea42ef @rehno-lindeque committed Apr 21, 2012
Showing with 24 additions and 5 deletions.
  1. +24 −5 README.markdown
View
29 README.markdown
@@ -170,20 +170,39 @@ leveraging `Object.getOwnPropertyNames`.
result = mathEval(formula);
```
-#### Stateful visitors
+#### Automatic serialization (can be used as an alternative to JSON)
+
+Interestingly, if we can easily serialize and deserialize our ADT's we could
+actually use them as an alternative to JSON - a safely executable kind of JSON.
```javascript
- TODO
+ // Serialize expression
+ mathCons = adt('plus', 'mul'),
+ expr = mathCons.mul(mathCons.plus(5.0,22), mathCons.mul(0.1,0.1)),
+ exprSerialized = adt.serialize(expr);
+ // exprSerialized == "(mul (plus 5.0 22) (mul 0.1 0.1))"
```
-#### Automatic serialization (can be used as an alternative to JSON)
+```javascript
+ // Deserialize expression
+ mathEval = adt({
+ plus: function(a,b) { return a + b; },
+ mul: function(a,b) { return a * b; }
+ }),
+ exprSerialized = "(mul (plus 5.0 22) (mul 0.1 0.1))",
+ exprDeserialized = adt.deserialize(exprSerialized),
+ result = mathEval(exprDeserialized),
+ detailedResult = exprString + " = " + String(result);
+```
+
+By the way, can you guess what `serialize` and `deserialize` look like?
+
+#### Stateful visitors
```javascript
TODO
```
-This can be used as an alternative to JSON - a safe, executable kind of JSON
-
#### ADT's with object constructors (??)
**TODO... POSSIBLY...**

0 comments on commit aa65beb

Please sign in to comment.
Something went wrong with that request. Please try again.