Skip to content

Commit 4429456

Browse files
committed
Add json conversion as subpackage and array of tables (partial)
1 parent d707ae0 commit 4429456

File tree

5 files changed

+250
-40
lines changed

5 files changed

+250
-40
lines changed

README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
D implementation of [Tom's Obvious, Minimal Language](https://github.com/toml-lang/toml/blob/master/README.md) [v0.4.0](https://github.com/toml-lang/toml/blob/master/versions/en/toml-v0.4.0.md)
2+
3+
4+
[![Build Status](https://travis-ci.org/Kripth/toml.svg?branch=master)](https://travis-ci.org/Kripth/toml)
5+
[![Code Coverage](https://codecov.io/gh/Kripth/toml/branch/master/graph/badge.svg)](https://codecov.io/gh/Kripth/toml)
6+
[![DUB Package](https://img.shields.io/dub/v/toml.svg)](https://code.dlang.org/packages/toml)
7+
[![DUB Downloads](https://img.shields.io/dub/dt/toml.svg)](https://code.dlang.org/packages/toml)
8+
9+
# Usage
10+
11+
```d
12+
import toml;
13+
14+
TOMLDocument doc;
15+
16+
doc = parseTOML("example = 1");
17+
assert(doc["example"].integer == 1);
18+
19+
doc = parseTOML(`
20+
bool = true
21+
integer = 42
22+
floating = 1e2
23+
string = "string"
24+
`)
25+
assert(doc["bool"] == true);
26+
assert(doc["integer"] == 42);
27+
assert(doc["floating"] == 1e2);
28+
assert(doc["string"] == "string");
29+
30+
// from a file
31+
import std.file : read;
32+
doc = parseTOML(cast(string)read("/path/to/file.toml"));
33+
```
34+
35+
# Missing features
36+
37+
- Array of tables
38+
- Fractional time in dates

conv/json/toml/json.d

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
// Written in the D programming language.
2+
3+
/**
4+
*
5+
* Conversion between JSON and TOML.
6+
*
7+
* License: $(HTTP https://github.com/Kripth/toml/blob/master/LICENSE, MIT)
8+
* Authors: Kripth
9+
* References: $(LINK https://github.com/toml-lang/toml/blob/master/README.md)
10+
* Source: $(HTTP https://github.com/Kripth/toml, Kripth/_toml.d)
11+
*
12+
*/
13+
module toml.json;
14+
15+
import std.json : JSONValue, JSON_TYPE;
16+
17+
import toml.toml : TOMLDocument, TOMLValue, TOML_TYPE;
18+
19+
/**
20+
* Converts a TOMLValue to a JSONValue.
21+
*/
22+
JSONValue toJSON(TOMLValue toml) {
23+
final switch(toml.type) with(TOML_TYPE) {
24+
case BOOL: return JSONValue(toml.boolean);
25+
case STRING: return JSONValue(toml.str);
26+
case INTEGER: return JSONValue(toml.integer);
27+
case FLOAT: return JSONValue(toml.floating);
28+
case OFFSET_DATETIME: return JSONValue(toml.offsetDatetime.toISOExtString());
29+
case LOCAL_DATETIME: return JSONValue(toml.localDatetime.toISOExtString());
30+
case LOCAL_DATE: return JSONValue(toml.localDate.toISOExtString());
31+
case LOCAL_TIME: return JSONValue(toml.localTime.toISOExtString());
32+
case ARRAY:
33+
JSONValue[] ret;
34+
foreach(value ; toml.array) {
35+
ret ~= toJSON(value);
36+
}
37+
return JSONValue(ret);
38+
case TABLE:
39+
JSONValue[string] ret;
40+
foreach(key, value; toml.table) {
41+
ret[key] = toJSON(value);
42+
}
43+
return JSONValue(ret);
44+
}
45+
}
46+
47+
/// ditto
48+
JSONValue toJSON(TOMLDocument doc) {
49+
return toJSON(TOMLValue(doc.table));
50+
}
51+
52+
///
53+
unittest {
54+
55+
import std.datetime;
56+
57+
assert(toJSON(TOMLValue(true)).type == JSON_TYPE.TRUE);
58+
assert(toJSON(TOMLValue("string")).str == "string");
59+
assert(toJSON(TOMLValue(42)) == JSONValue(42));
60+
assert(toJSON(TOMLValue(.1)) == JSONValue(.1));
61+
assert(toJSON(TOMLValue(SysTime.fromISOExtString("1979-05-27T07:32:00Z"))).str == "1979-05-27T07:32:00Z");
62+
assert(toJSON(TOMLValue(DateTime.fromISOExtString("1979-05-27T07:32:00"))).str == "1979-05-27T07:32:00");
63+
assert(toJSON(TOMLValue(Date.fromISOExtString("1979-05-27"))).str == "1979-05-27");
64+
assert(toJSON(TOMLValue(TimeOfDay.fromISOExtString("07:32:00"))).str == "07:32:00");
65+
assert(toJSON(TOMLValue([1, 2, 3])) == JSONValue([1, 2, 3]));
66+
assert(toJSON(TOMLDocument(["a": TOMLValue(0), "b": TOMLValue(1)])) == JSONValue(["a": 0, "b": 1]));
67+
}
68+
69+
/**
70+
* Convert a JSONValue to a TOMLValue.
71+
*/
72+
TOMLValue toTOML(JSONValue json) {
73+
final switch(json.type) with(JSON_TYPE) {
74+
case NULL: return TOMLValue("null");
75+
case TRUE: return TOMLValue(true);
76+
case FALSE: return TOMLValue(false);
77+
case STRING: return TOMLValue(json.str);
78+
case INTEGER: return TOMLValue(json.integer);
79+
case UINTEGER: return TOMLValue(cast(long)json.uinteger);
80+
case FLOAT: return TOMLValue(json.floating);
81+
case ARRAY:
82+
TOMLValue[] ret;
83+
foreach(value ; json.array) {
84+
ret ~= toTOML(value);
85+
}
86+
return TOMLValue(ret);
87+
case OBJECT:
88+
TOMLValue[string] ret;
89+
foreach(key, value; json.object) {
90+
ret[key] = toTOML(value);
91+
}
92+
return TOMLValue(ret);
93+
}
94+
}
95+
96+
///
97+
unittest {
98+
99+
assert(toTOML(JSONValue.init) == "null");
100+
assert(toTOML(JSONValue(true)).type == TOML_TYPE.BOOL);
101+
assert(toTOML(JSONValue(false)) == false);
102+
assert(toTOML(JSONValue("test")) == "test");
103+
assert(toTOML(JSONValue(42)) == 42);
104+
assert(toTOML(JSONValue(ulong.max)) == -1);
105+
assert(toTOML(JSONValue(.1)) == .1);
106+
assert(toTOML(JSONValue([1, 2, 3])) == [1, 2, 3]);
107+
assert(toTOML(JSONValue(["a": 1, "b": 2])) == ["a": 1, "b": 2]);
108+
109+
}

dub.sdl

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,20 @@
11
name "toml"
2-
description "TOML implementation for D"
2+
description "D implementation of TOML"
33
authors "Kripth"
44
license "MIT"
5+
subPackage {
6+
name "json"
7+
sourcePaths "conv/json"
8+
importPaths "conv/json"
9+
dependency "toml" version="*"
10+
}
11+
buildType "unittest" {
12+
sourcePaths "conv/json"
13+
importPaths "conv/json"
14+
buildOptions "unittests" "debugMode" "debugInfo"
15+
}
16+
buildType "unittest-cov" {
17+
sourcePaths "conv/json"
18+
importPaths "conv/json"
19+
buildOptions "unittests" "coverage" "debugMode" "debugInfo"
20+
}

src/toml/package.d

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Written in the D programming language.
2+
3+
/**
4+
*
5+
* Tom's Obvious, Minimal Language (v0.4.0).
6+
*
7+
* License: $(HTTP https://github.com/Kripth/toml/blob/master/LICENSE, MIT)
8+
* Authors: Kripth
9+
* References: $(LINK https://github.com/toml-lang/toml/blob/master/README.md)
10+
* Source: $(HTTP https://github.com/Kripth/toml, Kripth/_toml.d)
11+
*
12+
*/
13+
module toml;
14+
15+
public import toml.toml : TOML_TYPE, TOMLDocument, TOMLValue, parseTOML, TOMLException, TOMLParserException;

0 commit comments

Comments
 (0)