RejectedSoftware Forums

Sign up

How to return a JSON through POST using registerRestInterface?

I am not sure how to implement this via registerRestInterface.

I've tried:

Json rest_api(string param0, string param1) {
    Json j;
    j.id = "1";
    return j;
}

which causes an exception with message:

...Got JSON of type undefined, expected object....

So, is there a formal way to return a JSON through POST?

Thanks.

Re: How to return a JSON through POST using registerRestInterface?

Am 21.01.2014 09:23, schrieb Kai-Heng Feng:

I am not sure how to implement this via registerRestInterface.

I've tried:

 Json rest_api(string param0, string param1) {
     Json j;
     j.id = "1";
     return j;
 }

which causes an exception with message:

 ...Got JSON of type undefined, expected object....

So, is there a formal way to return a JSON through POST?

Thanks.

This approach is supposed to be correct, the error is caused by the
Json value not being initialized to Type.object. Please try to
replace Json j; with Json j = Json.emptyObject; and see if it works.

Re: How to return a JSON through POST using registerRestInterface?

On Tue, 21 Jan 2014 09:47:34 +0100, Sönke Ludwig wrote:

Am 21.01.2014 09:23, schrieb Kai-Heng Feng:

I am not sure how to implement this via registerRestInterface.

I've tried:

 Json rest_api(string param0, string param1) {
     Json j;
     j.id = "1";
     return j;
 }

which causes an exception with message:

 ...Got JSON of type undefined, expected object....

So, is there a formal way to return a JSON through POST?

Thanks.

This approach is supposed to be correct, the error is caused by the
Json value not being initialized to Type.object. Please try to
replace Json j; with Json j = Json.emptyObject; and see if it works.

Thanks! It works flawlessly.

I came from C++ world,
I assumed that since Json is a struct, it should be default-initialized.
But in D world, struct is initialized by a factory function, am I right?

Re: How to return a JSON through POST using registerRestInterface?

On Tue, 21 Jan 2014 08:59:28 GMT, Kai-Heng Feng wrote:

I came from C++ world,
I assumed that since Json is a struct, it should be default-initialized.
But in D world, struct is initialized by a factory function, am I right?

It is indeed default initialized (in contrast to C++, all values are default initialized in D unless = void; is used, not only struct types), but the default in this case was chosen to be Json.Type.undefined.

Another possibility to handle this transparently would be to let a member assignment such as j.id = ...; automatically set the type of j to object, but when initially writing this, I wanted to start with a rather strongly typed interface and evaluate more relaxed features later (because it's always easy to relax such restrictions, but not the other way around).

Re: How to return a JSON through POST using registerRestInterface?

On Tue, 21 Jan 2014 09:23:06 GMT, Sönke Ludwig wrote:

On Tue, 21 Jan 2014 08:59:28 GMT, Kai-Heng Feng wrote:

I came from C++ world,
I assumed that since Json is a struct, it should be default-initialized.
But in D world, struct is initialized by a factory function, am I right?

It is indeed default initialized (in contrast to C++, all values are default initialized in D unless = void; is used, not only struct types), but the default in this case was chosen to be Json.Type.undefined.

Another possibility to handle this transparently would be to let a member assignment such as j.id = ...; automatically set the type of j to object, but when initially writing this, I wanted to start with a rather strongly typed interface and evaluate more relaxed features later (because it's always easy to relax such restrictions, but not the other way around).

Thanks! Your answer explained my confusion.
Kudos for this great framework!