www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Issues with Vibe.d Dynamic HTML with JSON

reply SamwiseFilmore <mggmugginsmc gmail.com> writes:
I've got a serialized JSON structure that looks something like 
this:

{
     "title": "Webpage title",
     "major_categories": [
         {
             "title": "Major Category title",
             "categories": [
                 {
                     "title": "Minor Category title",
                     "links": [
                         {
                             "url": "http://myurl.something.com",
                             "label": "Text to display"
                         }
                     ]
                 }
             ]
         }
     ]
}

Of course the arrays have more elements (the entire file is about 
450 lines). My thought was to simply iterate through this 
structure using std.json and vibe.d/diet and basically build a 
webpage that way. My D get request handler is fairly simple:

void get() {
     JSONValue json = parseJSON(new File("links.json", 
"r").readln('\x00'));
     string title = json["title"].str;
     JSONValue[] major_categories = json["major_categories"].array;
     render!("index.dt", title, major_categories);
}

My Diet is a little more complex, with three layers of foreach 
loops:

doctype html
html
     head

     body


         - foreach (major; major_categories)

             - foreach (minor; major["categories"].array)

                 p
                     - foreach (item; minor["links"].array)


                         br

I realize all that is hard to read, sorry. :/

Anyway, my problem is that when I get about to line 320 of the 
JSON, the diet stops generating HTML. I've spit out the whole 
JSON run through the parser and back to string, and that has all 
the data, including the stuff that is cut out in the Diet. 
Basically I'm pretty sure it's not std.json (not 100% sure, since 
the operations I do in the diet are more complex, but 
essentially...). Does anyone have any insight to shine on this?
Nov 01 2017
parent reply bauss <jj_1337 live.dk> writes:
On Thursday, 2 November 2017 at 04:00:10 UTC, SamwiseFilmore 
wrote:
 I've got a serialized JSON structure that looks something like 
 this:

 [...]
Do you get a response back with rendered html or does the connection get dropped? Have you tried to cut down the amount of data and see if it will render it all? That should eliminate whether it's the amount of data or what.
Nov 02 2017
parent reply SamwiseFilmore <mggmugginsmc gmail.com> writes:
On Thursday, 2 November 2017 at 08:40:28 UTC, bauss wrote:
 Do you get a response back with rendered html or does the 
 connection get dropped?
No, the html does come in, and the whole content of the rendered page is sent to the browser. The page has closing head and body tags.
 Have you tried to cut down the amount of data and see if it 
 will render it all? That should eliminate whether it's the 
 amount of data or what.
I'll play with that. Would there be any reason for my data to get randomly truncated?
Nov 02 2017
parent reply bauss <jj_1337 live.dk> writes:
On Thursday, 2 November 2017 at 16:23:55 UTC, SamwiseFilmore 
wrote:
 On Thursday, 2 November 2017 at 08:40:28 UTC, bauss wrote:
 Do you get a response back with rendered html or does the 
 connection get dropped?
No, the html does come in, and the whole content of the rendered page is sent to the browser. The page has closing head and body tags.
 Have you tried to cut down the amount of data and see if it 
 will render it all? That should eliminate whether it's the 
 amount of data or what.
I'll play with that. Would there be any reason for my data to get randomly truncated?
Before you did: render!("index.dt", title, major_categories); Have you tried to check the contents of "major_categories" making sure it's all there. Just to figure out whether the problem is in the view rendering or the json parsing.
Nov 02 2017
parent reply bauss <jj_1337 live.dk> writes:
On Thursday, 2 November 2017 at 18:48:10 UTC, bauss wrote:
 On Thursday, 2 November 2017 at 16:23:55 UTC, SamwiseFilmore 
 wrote:
 On Thursday, 2 November 2017 at 08:40:28 UTC, bauss wrote:
 [...]
No, the html does come in, and the whole content of the rendered page is sent to the browser. The page has closing head and body tags.
 [...]
I'll play with that. Would there be any reason for my data to get randomly truncated?
Before you did: render!("index.dt", title, major_categories); Have you tried to check the contents of "major_categories" making sure it's all there. Just to figure out whether the problem is in the view rendering or the json parsing.
Also alternatively have you tried "vibe.data.json"? http://vibed.org/api/vibe.data.json/
Nov 02 2017
parent SamwiseFilmore <mggmugginsmc gmail.com> writes:
On Thursday, 2 November 2017 at 18:51:09 UTC, bauss wrote:
 On Thursday, 2 November 2017 at 18:48:10 UTC, bauss wrote:

 Before you did:
 render!("index.dt", title, major_categories);

 Have you tried to check the contents of "major_categories" 
 making sure it's all there. Just to figure out whether the 
 problem is in the view rendering or the json parsing.
I checked, it seemed as though the JSON was all there.
 Also alternatively have you tried "vibe.data.json"?

 http://vibed.org/api/vibe.data.json/
I just converted my code to use vibe's library instead of the phobos Json implementation. Now I'm not getting any output whatsoever. I'm wondering if it's an issue with Diet not wanting to do that many levels of nested iterations... Also the vibe.data.json.Json.toString() method does something awful to that Json string. After I fix that I might be able to get more debugging info... Here's my Diet. I basically just pass a Json object to render!: doctype html html head body - import std.stdio : writeln; - import vibe.data.json : Json; - foreach (major; json["major_categories"].get!(Json[])) { - string major_category_name = major["title"].get!string; - major_category_name.writeln(); - foreach (minor; major["categories"].get!(Json[])) { p - foreach (item; minor["links"].get!(Json[])) { br - } - } - }
Nov 02 2017