20,741
edits
(Created page with "{{Stub}} == Objective == == Background == Nasal's hash syntax is already close enough to JSON that Nasal could even serialie a property tree without requiring much work, ana...") |
m (→References) |
||
Line 107: | Line 107: | ||
debug.dump(result); | debug.dump(result); | ||
</syntaxhighlight> | |||
== Finally == | |||
the original Nasal example, already contains a helper function to serialie a Nasal type:<ref>{{cite web | |||
|url = https://forum.flightgear.org/viewtopic.php?p=257123#p257123 | |||
|title = <nowiki> Re: String manipulation and file I/O (pm2thread) </nowiki> | |||
|author = <nowiki> Hooray </nowiki> | |||
|date = Sep 12th, 2015 | |||
|added = Sep 12th, 2015 | |||
|script_version = 0.40 | |||
}}</ref> | |||
http://plausible.org/nasal/sample.nas | |||
<syntaxhighlight lang="nasal">## | |||
## A rockin' metaprogramming hack. Generates and returns a deep copy | |||
## of the object in valid Nasal syntax. A warning to those who might | |||
## want to use this: it ignores function objects (which cannot be | |||
## inspected from Nasal) and replaces them with nil references. It | |||
## also makes no attempt to escape special characters in strings, which | |||
## can break re-parsing in strange (and possibly insecure!) ways. | |||
## | |||
dump = func(o) { | |||
result = ""; | |||
if(typeof(o) == "scalar") { | |||
n = num(o); | |||
if(n == nil) { result = result ~ '"' ~ o ~ '"'; } | |||
else { result = result ~ o; } | |||
} elsif(typeof(o) == "vector") { | |||
result = result ~ "[ "; | |||
if(size(o) > 0) { result = result ~ dump(o[0]); } | |||
for(i=1; i<size(o); i=i+1) { | |||
result = result ~ ", " ~ dump(o[i]); | |||
} | |||
result = result ~ " ]"; | |||
} elsif(typeof(o) == "hash") { | |||
ks = keys(o); | |||
result = result ~ "{ "; | |||
if(size(o) > 0) { | |||
k = ks[0]; | |||
result = result ~ k ~ ":" ~ dump(o[k]); | |||
} | |||
for(i=1; i<size(o); i=i+1) { | |||
k = ks[i]; | |||
result = result ~ ", " ~ k ~ " : " ~ dump(o[k]); | |||
} | |||
result = result ~ " }"; | |||
} else { | |||
result = result ~ "nil"; | |||
} | |||
return result; | |||
} | |||
</syntaxhighlight> | </syntaxhighlight> | ||