574
edits
(4 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
{{Stub}} | {{Stub}} | ||
== Objective == | == Objective == | ||
# serialization: convert a Nasal data structure to a string using valid Nasal syntax | # serialization: convert a Nasal data structure (vector, hash, scalar) to a string using valid Nasal syntax | ||
# deserialization: compile that string to a temporary Nasal function, call it to retrieve the original data structure | # deserialization: compile that string to a temporary Nasal function, call it to retrieve the original data structure | ||
Line 66: | Line 66: | ||
var serializeVector = func(vector) { | var serializeVector = func(vector) { | ||
retVal = "["; | retVal = "["; | ||
foreach(var item; vector) { | foreach(var item; vector) { | ||
retVal ~= serialize(item) ~ ','; | retVal ~= serialize(item) ~ ','; | ||
} | } | ||
Line 75: | Line 75: | ||
var serializeHash = func(hash) { | var serializeHash = func(hash) { | ||
var retVal = "{"; | var retVal = "{"; | ||
foreach(var member; keys(hash)) { | foreach(var member; keys(hash)) { | ||
retVal ~= serialize(member) ~ ','; | retVal ~= serialize(member) ~ ','; | ||
} | } | ||
Line 86: | Line 86: | ||
var serialize = func (data) { | var serialize = func (data) { | ||
var type = typeof(data); | var type = typeof(data); | ||
if (type == "scalar") return data; | if (type == "scalar") return data; | ||
if (type == "vector") return serializeVector(data); | if (type == "vector") return serializeVector(data); | ||
if (type == "hash") return serializeHash(data); | if (type == "hash") return serializeHash(data); | ||
if (type == "ghost" or type == "func") return "nil"; | if (type == "ghost" or type == "func") return "nil"; | ||
die("cannot serialize unsupported data type:", type); | die("cannot serialize unsupported data type:", type); | ||
} | } | ||
Line 100: | Line 100: | ||
# populate the vector with elements with [index, index^2] | # populate the vector with elements with [index, index^2] | ||
for (var i=0;i<=40;i+=1) { | for (var i=0;i<=40;i+=1) { | ||
append(myVector,[i, i*i]); | append(myVector,[i, i*i]); | ||
} | } | ||
Line 123: | Line 123: | ||
== Finally == | == Finally == | ||
the original Nasal example, already contains a helper function to | the original Nasal example, already contains a helper function to serialize a Nasal type:<ref>{{cite web | ||
|url = https://forum.flightgear.org/viewtopic.php?p=257123#p257123 | |url = https://forum.flightgear.org/viewtopic.php?p=257123#p257123 | ||
|title = <nowiki> Re: String manipulation and file I/O (pm2thread) </nowiki> | |title = <nowiki> Re: String manipulation and file I/O (pm2thread) </nowiki> | ||
Line 131: | Line 131: | ||
|script_version = 0.40 | |script_version = 0.40 | ||
}}</ref> | }}</ref> | ||
We can also look at the implementation of debug.dump() and props.dump() respectively. | |||
http://plausible.org/nasal/sample.nas | http://plausible.org/nasal/sample.nas | ||
Line 172: | Line 174: | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== References == | == References == | ||
{{Appendix}} | {{Appendix}} |
edits