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>
This commit is contained in:
parent
646315ca4c
commit
bb7b87370a
|
@ -36,6 +36,7 @@ script:
|
||||||
- "dub build dyaml:getting-started"
|
- "dub build dyaml:getting-started"
|
||||||
- "dub build dyaml:representer"
|
- "dub build dyaml:representer"
|
||||||
- "dub build dyaml:resolver"
|
- "dub build dyaml:resolver"
|
||||||
|
- "dub build dyaml:tojson"
|
||||||
- "dub build dyaml:yaml_gen"
|
- "dub build dyaml:yaml_gen"
|
||||||
- "dub build dyaml:yaml_stats"
|
- "dub build dyaml:yaml_stats"
|
||||||
- dub test --build=unittest-cov
|
- dub test --build=unittest-cov
|
||||||
|
|
1
dub.json
1
dub.json
|
@ -13,6 +13,7 @@
|
||||||
"examples/getting_started",
|
"examples/getting_started",
|
||||||
"examples/representer",
|
"examples/representer",
|
||||||
"examples/resolver",
|
"examples/resolver",
|
||||||
|
"examples/tojson",
|
||||||
"examples/yaml_bench",
|
"examples/yaml_bench",
|
||||||
"examples/yaml_gen",
|
"examples/yaml_gen",
|
||||||
"examples/yaml_stats"
|
"examples/yaml_stats"
|
||||||
|
|
8
examples/tojson/dub.json
Normal file
8
examples/tojson/dub.json
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
{
|
||||||
|
"name": "tojson",
|
||||||
|
"targetType": "executable",
|
||||||
|
"dependencies":
|
||||||
|
{
|
||||||
|
"dyaml": "*"
|
||||||
|
}
|
||||||
|
}
|
54
examples/tojson/source/app.d
Normal file
54
examples/tojson/source/app.d
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
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;
|
||||||
|
}
|
Loading…
Reference in a new issue