Created
May 27, 2012 07:33
-
-
Save Kami/2802627 to your computer and use it in GitHub Desktop.
Swiz - de-serialization example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var swiz = require('swiz'); | |
| var O = swiz.struct.Obj; | |
| var F = swiz.struct.Field; | |
| var defs = [ | |
| O('Server', | |
| { | |
| 'fields': [ | |
| F('id', {'src': 'hash_id', 'desc': 'hash ID for the server', 'attribute': true}), | |
| F('is_active', {'src': 'active', 'desc': 'is the server active?', 'coerceTo': 'boolean'}), | |
| F('name', {'src' : 'get_name', 'desc' : 'name', 'attribute': true}), | |
| F('agent_name'), | |
| F('ipaddress' , {'src' : 'get_public_address'}), | |
| F('public_ips', {'singular': 'ip', 'filterFrom': ['public'], 'coerceTo': 'array'}), | |
| F('state', {'enumerated' : {inactive: 0, active: 1}, 'filterFrom': ['public']}), | |
| F('opts', {'src': 'options'}), | |
| F('data') | |
| ], | |
| 'plural': 'servers' | |
| }), | |
| O('ServerOpts', | |
| { | |
| 'fields': [ | |
| F('option1', {'src': 'opt1'}), | |
| F('option2', {'src': 'opt2'}), | |
| F('option3', {'src': 'opt3'}), | |
| ], | |
| 'singular': 'serverOpts' | |
| }) | |
| ]; | |
| var xml = '<server id="15245" name="server #1"><is_active>true</is_active><agent_name>some agent</agent_name><public_ips><ip>123.45.55.44</ip><ip>122.123.32.2</ip></public_ips><state>active</state><opts><serverOpts><option1>defaultval</option1><option2>defaultval</option2><option3>something</option3></serverOpts></opts><data><foo>thingone</foo><bar>thingtwo</bar></data></server>'; | |
| var sw = new swiz.Swiz(defs, {stripNulls: false}); | |
| sw.deserialize(swiz.SERIALIZATION.SERIALIZATION_XML, 1, xml, function(err, obj) { | |
| console.log('Deserialized:'); | |
| console.log(obj); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment