1 module json.d; 2 3 public { 4 import json.parser : parseJson; 5 import json.parser.lexer : StandardCompliant; 6 import json.value; 7 } 8 9 // test standard-compliant JSON parsing 10 @system unittest 11 { 12 import std.algorithm; 13 14 immutable text = `{ 15 "firstName": "John", 16 "lastName": "Doe", 17 "age": 35, 18 19 "phoneNumbers": [ 20 "605-555-1234" 21 ], 22 23 "transactions": [ 24 123.4, 25 -500, 26 -1e5, 27 2e+5, 28 2E-5, 29 2.5E2, 30 75 31 ] 32 }`; 33 34 immutable json = text.parseJson(); 35 assert( json.isObject ); 36 assert( json["firstName"].isString && json["lastName"].isString ); 37 assert( json["age"].isUnsigned ); 38 assert( json["phoneNumbers"].isArray ); 39 assert( json["transactions"].all!( x => x.isNumber ) ); 40 41 JsonValue default_; 42 assert( default_.isNull ); 43 44 JsonValue true_ = true; 45 JsonValue false_ = false; 46 47 assert( true_ ); 48 assert( !false_ ); 49 } 50 51 // test non-standard parsing 52 @system unittest 53 { 54 immutable text = `{ 55 unquoted: 'single-quoted string', // trailling comma 56 57 /* multi-line comment 58 /* with another one nested inside */ 59 */}`; 60 61 immutable json = text.parseJson( StandardCompliant.no ); 62 63 assert( json.isObject ); 64 assert( json["unquoted"].isString && json["unquoted"] == "single-quoted string" ); 65 }