Added support for writing empty arrays and std.json.
This commit is contained in:
parent
b3229e1d86
commit
223598fd02
|
@ -6,7 +6,6 @@ module handy_http_data.json;
|
||||||
import streams;
|
import streams;
|
||||||
import handy_http_primitives.request;
|
import handy_http_primitives.request;
|
||||||
import handy_http_primitives.response;
|
import handy_http_primitives.response;
|
||||||
import asdf;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads a JSON value from the body of the given HTTP request, parsing it using
|
* Reads a JSON value from the body of the given HTTP request, parsing it using
|
||||||
|
@ -18,6 +17,7 @@ import asdf;
|
||||||
* Returns: The value that was read.
|
* Returns: The value that was read.
|
||||||
*/
|
*/
|
||||||
T readJsonBodyAs(T)(ref ServerHttpRequest request) {
|
T readJsonBodyAs(T)(ref ServerHttpRequest request) {
|
||||||
|
import asdf : deserialize, SerdeException;
|
||||||
try {
|
try {
|
||||||
string requestBody = request.readBodyAsString(false);
|
string requestBody = request.readBodyAsString(false);
|
||||||
return deserialize!T(requestBody);
|
return deserialize!T(requestBody);
|
||||||
|
@ -39,8 +39,17 @@ T readJsonBodyAs(T)(ref ServerHttpRequest request) {
|
||||||
*/
|
*/
|
||||||
void writeJsonBody(T)(ref ServerHttpResponse response, in T bodyContent) {
|
void writeJsonBody(T)(ref ServerHttpResponse response, in T bodyContent) {
|
||||||
import std.conv : to;
|
import std.conv : to;
|
||||||
|
import std.traits : isArray;
|
||||||
|
import std.json;
|
||||||
|
import asdf : serializeToJson, SerdeException;
|
||||||
try {
|
try {
|
||||||
|
static if (isArray!T && bodyContent.length == 0) {
|
||||||
|
string responseBody = "[]";
|
||||||
|
} else static if (is(T == JSONValue)) {
|
||||||
|
string responseBody = bodyContent.toString();
|
||||||
|
} else {
|
||||||
string responseBody = serializeToJson(bodyContent);
|
string responseBody = serializeToJson(bodyContent);
|
||||||
|
}
|
||||||
response.headers.remove("Content-Type");
|
response.headers.remove("Content-Type");
|
||||||
response.headers.remove("Content-Length");
|
response.headers.remove("Content-Length");
|
||||||
response.headers.add("Content-Type", "application/json");
|
response.headers.add("Content-Type", "application/json");
|
||||||
|
|
Loading…
Reference in New Issue