1 module hunt.framework.http.JsonResponse; 2 3 import hunt.framework.http.Response; 4 5 import std.conv; 6 import std.datetime; 7 import std.json; 8 9 import hunt.logging; 10 import hunt.serialization.JsonSerializer; 11 import hunt.util.MimeType; 12 13 // import hunt.framework.http.cookie; 14 // import hunt.framework.util.String; 15 // import hunt.framework.Version; 16 // import hunt.framework.http.Request; 17 18 // import hunt.http.codec.http.model.HttpHeader; 19 20 21 /** 22 * Response represents an HTTP response in JSON format. 23 * 24 * Note that this class does not force the returned JSON content to be an 25 * object. It is however recommended that you do return an object as it 26 * protects yourself against XSSI and JSON-JavaScript Hijacking. 27 * 28 * @see https://www.owasp.org/index.php/OWASP_AJAX_Security_Guidelines#Always_return_JSON_with_an_Object_on_the_outside 29 * 30 */ 31 class JsonResponse : Response { 32 33 this() { 34 super(); 35 } 36 37 this(T)(T data) { 38 super(); 39 this.setJson(data.toJson()); 40 } 41 42 /** 43 * Get the json_decoded data from the response. 44 * 45 * @return JSONValue 46 */ 47 // JSONValue getData() 48 // { 49 // return parseJSON(getContent()); 50 // } 51 52 /** 53 * Sets a raw string containing a JSON document to be sent. 54 * 55 * @param string data 56 * 57 * @return this 58 */ 59 JsonResponse setJson(JSONValue data) { 60 this.setContent(data.toString(), MimeType.APPLICATION_JSON_UTF_8.toString()); 61 return this; 62 } 63 }