www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - VibeD - takeing out value from json

reply holo <holosian gmail.com> writes:
Hello

I wrote such code to test restAPI client:

///////
//app.d
///////

import vibe.d;
import sites.frontpage;
import msfrontpage;

shared static this()
{
	// the router will match incoming HTTP requests to the proper 
routes
	auto router = new URLRouter;

	// registers each method of WebChat in the router
   auto api = new 
RestInterfaceClient!IfFrontPageAPI("http://127.0.0.1:8090/");
	router.registerWebInterface(new SilesianCloud(api));
	// match incoming requests to files in the public/ folder
	router.get("*", serveStaticFiles("public/"));

	auto settings = new HTTPServerSettings;
	settings.port = 8080;
	settings.bindAddresses = ["::1", "127.0.0.1"];
	listenHTTP(settings, router);
	logInfo("Please open http://127.0.0.1:8080/ in your browser.");
}


//////////////
//frontpage.d
//////////////
module sites.frontpage;


import vibe.d;
import msfrontpage;



final class SilesianCloud {

   this(RestInterfaceClient!(IfFrontPageAPI) tmp)
   {
       this.frontpageapi = tmp;
   }

   public:
   // GET /
   void get()
   {
     Json result = frontpageapi.getHome();
     logInfo(result.toString);
     auto header = result["heading"].get!string;
     logInfo(result["heading"].get!string);
     render!("frontpage.dt",header);
   }

   private:
     RestInterfaceClient!(IfFrontPageAPI) frontpageapi;
}

But it looks like my value is not "extracted" from Json in proper 
string, even logInfo(header) shows "nothing" and in web broser im 
getting:

500 - Internal Server Error

Internal Server Error

Internal error information:
std.json.JSONException std/json.d(1330): Got JSON of type array, 
expected object.
----------------
../../.dub/packages/vibe-d-0.7.31/vibe-d/source/vibe/data/json.d:1054
const(pure  safe void function(immutable(char)[]))
vibe.data.json.Json.checkType!(vibe.data.json.Json[immutable
char)[]]).checkType [0x88f762]
../../.dub/packages/vibe-d-0.7.31/vibe-d/source/vibe/data/json.d:339 ref
vibe.data.json.Json vibe.data.json.Json.opIndex(immutable(char)[]) [0x977d4e]
source/sites/frontpage.d:20 void 
sites.frontpage.SilesianCloud.get() [0x7d129c]
../../.dub/packages/vibe-d-0.7.31/vibe-d/source/vibe/web/web.d:822 void
vibe.web.web.handleRequest!("get", void sites.frontpage.SilesianCloud.get(),
sites.frontpage.SilesianCloud).handleRequest(vibe.http.serve
.HTTPServerRequest, vibe.http.server.HTTPServerResponse,
sites.frontpage.SilesianCloud, vibe.web.web.WebInterfaceSettings) [0x7bd220]
../../.dub/packages/vibe-d-0.7.31/vibe-d/source/vibe/web/web.d:185 void
vibe.web.web.__T20registerWebInterfaceHTC5sites9frontpage13SilesianCloudVE4vibe3web6common11MethodStylei5Z.registerWebInterface(vibe.ht
p.router.URLRouter, sites.frontpage.SilesianCloud,
vibe.web.web.WebInterfaceSettings).__lambda4!(vibe.http.serve
.HTTPServerRequest, vibe.http.server.HTTPServerResponse).__lambda4(vibe.http.serve
.HTTPServerRequest, vibe.http.server.HTTPServerResponse) [0x7bca20]
../../.dub/packages/vibe-d-0.7.31/vibe-d/source/vibe/http/router.d:268
_D4vibe4http6router9URLRouter13handleRequestMFC4vibe4http6server17HTTPServerRequestC4vibe4http6server18HTTPServerResponseZ21__T9__lambda4TmTAAyaZ
__lambda4MFmMAAyaZb [0x864a0e]
../../.dub/packages/vibe-d-0.7.31/vibe-d/source/vibe/http/router.d:595
const(bool function(immutable(char)[], scope bool delegate(ulong, scope
immutable(char)[][])))
vibe.http.router.MatchTree!(vibe.http.router.Route).MatchTree.doMatch [0x8657f5]
../../.dub/packages/vibe-d-0.7.31/vibe-d/source/vibe/http/router.d:528 bool
vibe.http.router.MatchTree!(vibe.http.router.Route).MatchTree.matc
(immutable(char)[], scope bool delegate(ulong, scope immutable(char)[][]))
[0x865047]
../../.dub/packages/vibe-d-0.7.31/vibe-d/source/vibe/http/router.d:261 void
vibe.http.router.URLRouter.handleRequest(vibe.http.server.HTTPServerRequest,
vibe.http.server.HTTPServerResponse) [0x8646ad]
../../.dub/packages/vibe-d-0.7.31/vibe-d/source/vibe/http/server.d:1981 bool
vibe.http.server.handleRequest(vibe.core.stream.Stream,
vibe.core.net.TCPConnection, vibe.http.server.HTTPListenInfo, ref
vibe.http.server.HTTPServerSettings, ref bool) [0x8a7490]
../../.dub/packages/vibe-d-0.7.31/vibe-d/source/vibe/http/server.d:1737 void
vibe.http.server.handleHTTPConnection(vibe.core.net.TCPConnection,
vibe.http.server.HTTPListenInfo) [0x8a5a9a]
../../.dub/packages/vibe-d-0.7.31/vibe-d/source/vibe/http/server.d:1618 void
vibe.http.server.listenHTTPPlain(vibe.http.server.HTTPServerSettings).doListen(vibe.http.se
ver.HTTPListenInfo, bool, bool).__lambda4(vibe.core.net.TCPConnection)
[0x8a5538]
../../.dub/packages/vibe-d-0.7.31/vibe-d/source/vibe/core/drivers
libevent2_tcp.d:618 void vibe.core.drivers.libevent2_tcp.ClientTask.execute()
[0x964098]
../../.dub/packages/vibe-d-0.7.31/vibe-d/source/vibe/core/core.d:595 void
vibe.core.core.makeTaskFuncInfo!(void delegate()).makeTaskFuncInfo(ref void
delegate()).callDelegate(vibe.core.core.TaskFuncInfo*) [0x81193f]
../../.dub/packages/vibe-d-0.7.31/vibe-d/source/vibe/core/core.d:1224 void
vibe.core.core.CoreTask.run() [0x8f398a]
??:? void core.thread.Fiber.run() [0x9ea6e5]
??:? fiber_entryPoint [0x9ea45f]
??:? [0xffffffff]

What am i doing wrong?

//holo
Jul 25
parent holo <holosian gmail.com> writes:
There was need to change one line from:

auto header = result["heading"].to!string;

to:

auto header = result[0]["heading"].to!string;
Jul 25