|
| 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 | +} |
0 commit comments