@@ -48,8 +48,10 @@ private JSONValue defaultToJSONImpl(T, SerializationOptions options)(in T object
4848 return JSONValue (jsonRange);
4949}
5050
51- // AA for simple types
52- private JSONValue defaultToJSONImpl (T, SerializationOptions options)(in T object) if (isAssociativeArray! T && isBuiltinType! (KeyType! T) && ! isAssociativeArray! (KeyType! T))
51+ // AA for simple types, excluding those handled by the JSONValue constructor
52+ private JSONValue defaultToJSONImpl (T, SerializationOptions options)(in T object)
53+ if (isAssociativeArray! T && isBuiltinType! (KeyType! T) && ! isAssociativeArray! (KeyType! T) &&
54+ ! __traits(compiles, (in T t) { JSONValue(t);}))
5355{
5456 JSONValue[string ] jsonAA;
5557 foreach (key, value; object)
@@ -206,24 +208,28 @@ unittest
206208 assert (aa.toJSON.toString == q{{" 0" :" a" ," 1" :" b" }});
207209 Point[int ] aaStruct = [0 : Point(- 1 , 1 ), 1 : Point(2 , 0 )];
208210 assertEqual(aaStruct.toJSON.toString, q{{" 0" :{" x" :- 1 ," y" :1 }," 1" :{" x" :2 ," y" :0 }}});
211+ assertEqual([" key" : " value" ].toJSON.toString, q{{" key" :" value" }});
209212}
210213
211214// / Associative array containing struct
212215unittest
213216{
214- struct Inner {
215- string str;
216- }
217- assertEqual([" test" : Inner(" test2" )].toJSON().toString, q{{" test" :{" str" :" test2" }}});
217+ assertEqual([" test" : SimpleStruct(" test2" )].toJSON().toString, q{{" test" :{" str" :" test2" }}});
218218}
219219
220220// / Associative array with struct key
221221unittest
222222{
223- struct Inner {
224- string str;
225- }
226- assertEqual([Inner(" key" ): " value" , Inner(" key2" ): " value2" ].toJSON().toString, q{{" {\" str\" :\" key\" }" :" value" ," {\" str\" :\" key2\" }" :" value2" }});
223+ assertEqual([SimpleStruct(" key" ): " value" , SimpleStruct(" key2" ): " value2" ].toJSON().toString, q{{" {\" str\" :\" key\" }" :" value" ," {\" str\" :\" key2\" }" :" value2" }});
224+ }
225+
226+ // / struct with inner struct and AA
227+ unittest
228+ {
229+ auto testStruct = StructWithStructAndAA([" key1" : " value1" ], [" key2" : StructWithStructAndAA.Inner(" value2" )]);
230+ auto converted = testStruct.toJSON();
231+ assertEqual(converted[" stringToInner" ].toString, q{{" key2" :{" str" :" value2" }}});
232+ assertEqual(converted[" stringToString" ].toString, q{{" key1" :" value1" }});
227233}
228234
229235// / Unnamed tuples
@@ -660,40 +666,39 @@ unittest
660666// / Associative array containing struct
661667unittest
662668{
663- struct Inner {
664- string str;
665- }
666- auto parsed = fromJSON! (Inner[string ])(parseJSON(q{{" key" : {" str" : " value" }}}));
667- assertEqual(parsed , [" key" : Inner(" value" )]);
669+ auto parsed = fromJSON! (SimpleStruct[string ])(parseJSON(q{{" key" : {" str" : " value" }}}));
670+ assertEqual(parsed , [" key" : SimpleStruct(" value" )]);
668671}
669672
670673// / Associative array with struct key
671674unittest
672675{
673- struct Inner {
674- string str;
675- }
676676 JSONValue value = parseJSON(q{{" {\" str\" :\" key\" }" :" value" , " {\" str\" :\" key2\" }" :" value2" }});
677- auto parsed = fromJSON! (string [Inner ])(value);
677+ auto parsed = fromJSON! (string [SimpleStruct ])(value);
678678 assertEqual(
679679 parsed
680- , [Inner(" key" ): " value" , Inner(" key2" ): " value2" ]);
680+ , [SimpleStruct(" key" ): " value" , SimpleStruct(" key2" ): " value2" ]);
681+ }
682+
683+ // / struct with inner struct and AA
684+ unittest
685+ {
686+ auto testStruct = StructWithStructAndAA([" key1" : " value1" ], [" key2" : StructWithStructAndAA.Inner(" value2" )]);
687+ auto testJSON = parseJSON(q{{" stringToInner" :{" key2" :{" str" :" value2" }}," stringToString" :{" key1" :" value1" }}});
688+ assertEqual(fromJSON! StructWithStructAndAA(testJSON), testStruct);
681689}
682690
683691// / Error reporting from inner objects
684692unittest
685693{
686694 import std.exception : collectExceptionMsg;
687695 import std.algorithm : canFind;
688- struct Inner {
689- string str;
690- }
691696 void throwFunc () {
692- fromJSON! (string [Inner ])(parseJSON(q{{" {\" str\" : \" key1\" }" : " value" , " key2" :" value2" }}));
697+ fromJSON! (string [SimpleStruct ])(parseJSON(q{{" {\" str\" : \" key1\" }" : " value" , " key2" :" value2" }}));
693698 }
694699 auto errorMessage = collectExceptionMsg(throwFunc());
695700 assert (errorMessage.canFind(" key2" ));
696- assert (errorMessage.canFind(" string[Inner ]" ));
701+ assert (errorMessage.canFind(" string[SimpleStruct ]" ));
697702 assert (! errorMessage.canFind(" key1" ));
698703}
699704
0 commit comments