574
edits
(9 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
{{Stub}} | {{Stub}} | ||
== Objective == | == Objective == | ||
# 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 | |||
<syntaxhighlight lang="javascript"> | |||
var myVector = [1,2,3,4,5]; | |||
var serialized = serialize(myVector); # [1,2,3,4,5] | |||
unserialize(serialized); | |||
</syntaxhighlight> | |||
== Background == | == Background == | ||
Nasal's hash syntax is already close enough to JSON that Nasal could even | Nasal's hash syntax is already close enough to JSON that Nasal could even serialize a property tree without requiring much work, analogous to props.getValues() which is already traversing a property tree node and which turns it into a Nasal data structure (hash). | ||
<ref>{{cite web | <ref>{{cite web | ||
Line 16: | Line 25: | ||
Another possibility would be using a subset of JSON, i.e. to | Another possibility would be using a subset of JSON, i.e. to serialize/unserialize Nasal data structures as scalars, vectors and hashes - which should be pretty straightforward. The format would then be plain text, and it would just be evaluated via Nasal's compile()/call() APIs to turn data structures back into Nasal types.<ref>{{cite web | ||
|url = https://forum.flightgear.org/viewtopic.php?p=257091#p257091 | |url = https://forum.flightgear.org/viewtopic.php?p=257091#p257091 | ||
|title = <nowiki> Re: String manipulation and file I/O (pm2thread) </nowiki> | |title = <nowiki> Re: String manipulation and file I/O (pm2thread) </nowiki> | ||
Line 49: | Line 58: | ||
|script_version = 0.40 | |script_version = 0.40 | ||
}}</ref> | }}</ref> | ||
== Proof of concept == | == Proof of concept == | ||
Line 59: | 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 68: | 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 79: | 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 93: | 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 116: | 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 124: | 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 165: | Line 174: | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== References == | == References == | ||
{{Appendix}} | {{Appendix}} |
edits