1 module hjson.asdf; 2 3 public import asdf.asdf : Asdf; 4 5 /** Parses a Hjson value into ASDF representation. 6 Params: 7 hjson = string containing the Hjson. 8 Throws: 9 `HjsonException` when passed invalid Hjson. 10 Returns: 11 ASDF containing the parsed Hjson value. 12 */ 13 Asdf parseHjsonToAsdf(string hjson) 14 { 15 import asdf.serialization : asdfSerializer; 16 import hjson.parser : parseHjson; 17 18 auto serializer = asdfSerializer(); 19 hjson.parseHjson(serializer); 20 return serializer.app.result; 21 } 22 23 /** Converts a single Hjson value directly into JSON. 24 Params: 25 hjson = Hjson to convert. 26 Throws: 27 `HjsonException` when passed invalid Hjson. 28 Returns: 29 `hjson` converted into JSON. 30 */ 31 string hjsonToJson(string hjson) 32 { 33 import asdf.serialization : jsonSerializer; 34 import hjson.parser : parseHjson; 35 import std.array : appender; 36 37 auto app = appender!string(); 38 auto serializer = jsonSerializer(&app.put!(const(char)[])); 39 hjson.parseHjson(serializer); 40 serializer.flush; 41 42 return app.data; 43 } 44 45 version(unittest): 46 version(Have_unit_threaded): 47 48 import std.format : format; 49 import asdf.jsonparser : parseJson; 50 import std.range : chain, only, iota; 51 52 import unit_threaded; 53 54 static foreach(testName; [ 55 "charset", 56 "charset2", 57 "comments", 58 "empty", 59 "kan", 60 "keys", 61 "oa", 62 "passSingle", 63 "stringify1", 64 "strings", 65 "strings2", 66 "trail" 67 ]) { 68 @Tags("asdf") 69 @(testName~"-parsing") unittest 70 { 71 immutable json = import(testName~"_result.json"), 72 hjsonResult = import(testName~"_result.hjson"), 73 hjsonTest = import(testName~"_test.hjson"); 74 75 hjsonTest.parseHjsonToAsdf.should == json.parseJson; 76 hjsonResult.parseHjsonToAsdf.should == json.parseJson; 77 json.parseHjsonToAsdf.should == json.parseJson; 78 } 79 80 @Tags("asdf") 81 @(testName~"-conversion") unittest 82 { 83 immutable json = import(testName~"_result.json"), 84 hjsonResult = import(testName~"_result.hjson"), 85 hjsonTest = import(testName~"_test.hjson"); 86 87 hjsonTest.hjsonToJson.parseJson.should == json.parseJson; 88 hjsonResult.hjsonToJson.parseJson.should == json.parseJson; 89 json.hjsonToJson.parseJson.should == json.parseJson; 90 } 91 } 92 static foreach(testName; [ 93 "mltabs", 94 "pass2", 95 "pass3", 96 "pass4" 97 ]) { 98 @Tags("asdf") 99 @(testName~"-parsing")unittest 100 { 101 immutable hjson = import(testName~"_result.hjson"), 102 json = import(testName~"_result.json"); 103 104 hjson.parseHjsonToAsdf.should == json.parseJson; 105 json.parseHjsonToAsdf.should == json.parseJson; 106 } 107 108 @Tags("asdf") 109 @(testName~"-conversion") unittest 110 { 111 immutable hjson = import(testName~"_result.hjson"), 112 json = import(testName~"_result.json"); 113 114 hjson.hjsonToJson.parseJson.should == json.parseJson; 115 json.hjsonToJson.parseJson.should == json.parseJson; 116 } 117 } 118 119 @Tags("asdf") 120 @ShouldFail("This fails because ASDF parses doubles at higher precision than std.conv.to") 121 @("pass1-parsing") unittest 122 { 123 immutable hjson = import("pass1_result.hjson"), 124 json = import("pass1_result.json"); 125 126 hjson.parseHjsonToAsdf.should == json.parseJson; 127 json.parseHjsonToAsdf.should == json.parseJson; 128 }