dyaml/examples/tojson/source/app.d
Cameron Ross bb7b87370a add tojson subpackage for converting yaml docs to json (#171)
add tojson subpackage for converting yaml docs to json
merged-on-behalf-of: BBasile <BBasile@users.noreply.github.com>
2018-06-21 05:34:07 +02:00

55 lines
1 KiB
D

module dyaml.tojson;
import std.datetime;
import std.json;
import std.stdio;
import dyaml;
void main()
{
auto doc = Loader.fromFile(stdin).load();
auto json = doc.toJSON;
writeln(json.toPrettyString);
}
JSONValue toJSON(Node node)
{
JSONValue output;
if (node.isSequence)
{
output = JSONValue(string[].init);
foreach (Node seqNode; node)
{
output.array ~= seqNode.toJSON();
}
}
else if (node.isMapping)
{
output = JSONValue(string[string].init);
foreach (Node keyNode, Node valueNode; node)
{
output[keyNode.as!string] = valueNode.toJSON();
}
}
else if (node.isString)
{
output = node.as!string;
}
else if (node.isInt)
{
output = node.as!long;
}
else if (node.isFloat)
{
output = node.as!real;
}
else if (node.isBool)
{
output = node.as!bool;
}
else if (node.isTime)
{
output = node.as!SysTime.toISOExtString();
}
return output;
}