Json Lexer - ParsedJsonDataSet

Discussion to talk about software related topics only.
seulater
Posts: 445
Joined: Fri Apr 25, 2008 5:26 am

Json Lexer - ParsedJsonDataSet

Post by seulater »

Using NNDK 2.7.7 on a MOD5270B

I am trying to create a json string to reply back to my web client.

Code: Select all

ParsedJsonDataSet JsonOutObject;

JsonOutObject.StartBuilding();
CreateJsonString(packJSON,"deviceInfo");
JsonOutObject.Add("password","ThePassowrd")
JsonOutObject.Add("alarm","0");
JsonOutObject.DoneBuilding();
JsonOutObject.PrintObject(true);
It formats all the data correctly, but it does not add the "\n\n" at the end which is needed at the end, otherwise the clients browser will not recognize it as a json reply. How can I add "\n\n" to this?
User avatar
pbreed
Posts: 1080
Joined: Thu Apr 24, 2008 3:58 pm

Re: Json Lexer - ParsedJsonDataSet

Post by pbreed »

just printf a \n\n?
seulater
Posts: 445
Joined: Fri Apr 25, 2008 5:26 am

Re: Json Lexer - ParsedJsonDataSet

Post by seulater »

just printf a \n\n?
But that wont put it into the JsonOutObject.
User avatar
pbreed
Posts: 1080
Joined: Thu Apr 24, 2008 3:58 pm

Re: Json Lexer - ParsedJsonDataSet

Post by pbreed »

No \n\n required at the end of the JASON for valid JSON
So whatever protocl that requires the \n\n at the end... requires more than JSON...

Some how your going to send your JsonOutObject somewhere...
Say a fd:

JsonOutObject.PrintObjectToFd( fd);
write(fd,"\n\n",2);
or fdprintf("fd,"\n\n");
seulater
Posts: 445
Joined: Fri Apr 25, 2008 5:26 am

Re: Json Lexer - ParsedJsonDataSet

Post by seulater »

I did try that yesterday,

Code: Select all

JsonOutObject.PrintObjectToFd( ws_fd);
write(ws_fd,"\n\n",2);
But since its not all send as one string it did not work.
I wound up doing this and it worked.

Code: Select all

memset(packJSON,'\n',sizeof(packJSON));
JsonOutObject.PrintObjectToBuffer(packJSON, strlen(packJSON));
writestring(ws_fd, packJSON);


It's ugly, but i was just after trying to get it working for now, will go back later and make it better. This is why i was asking if there was a way to inject the '\n\n" in the json object.
User avatar
pbreed
Posts: 1080
Joined: Thu Apr 24, 2008 3:58 pm

Re: Json Lexer - ParsedJsonDataSet

Post by pbreed »

memset(packJSON,'\n',sizeof(packJSON));
memset does not null terminate the buffer.....
JsonOutObject.PrintObjectToBuffer(packJSON, strlen(packJSON)); <-----Potential problem

JsonOutObject.PrintObjectToBuffer(packJSON, sizeof(packJSON));


writestring(ws_fd, packJSON);
seulater
Posts: 445
Joined: Fri Apr 25, 2008 5:26 am

Re: Json Lexer - ParsedJsonDataSet

Post by seulater »

JsonOutObject.PrintObjectToBuffer(packJSON, strlen(packJSON)); <-----Potential problem

JsonOutObject.PrintObjectToBuffer(packJSON, sizeof(packJSON));

writestring(ws_fd, packJSON);
Curious, after JsonOutObject.DoneBuilding(); Wouldent sizeof pack the whole size of packJSON?
Shouldn't I really use strlen because that will only pack the actual string length size it is?

In my mind I look at it this way (not saying I am correct),
char buffer[1000];
sprintf(buffer,"test");

If i use strlen on buffer i will get 4, if i use sizeof i will get 1000. Why do you suggest I use sizeof on packJSON, wouldn't that pack more data that's not even in there ?
seulater
Posts: 445
Joined: Fri Apr 25, 2008 5:26 am

Re: Json Lexer - ParsedJsonDataSet

Post by seulater »

FWIW, I just tested this.

JsonOutObject.PrintObjectToBuffer(packJSON, sizeof(packJSON));
printf("Size[%u][%u]\r\n", strlen(packJSON), sizeof(packJSON) );

This is what it printed, am i missing something ? aren't I packing more using sizeof?
Size[412][1000]
seulater
Posts: 445
Joined: Fri Apr 25, 2008 5:26 am

Re: Json Lexer - ParsedJsonDataSet

Post by seulater »

I have to admit I am at a loss why this works, but it does.

JsonOutObject.PrintObjectToBuffer(packJSON, sizeof(packJSON));
fdprintf(ws_fd, "%s",packJSON);

Yet this does not.

JsonOutObject.PrintObjectToBuffer(packJSON, strlen(packJSON));
fdprintf(ws_fd, "%s\n\n",packJSON);


Thank you for all the help. If your up to it, maybe you can explain why. If not i understand. I would rather learn then just guess and use it.
User avatar
pbreed
Posts: 1080
Joined: Thu Apr 24, 2008 3:58 pm

Re: Json Lexer - ParsedJsonDataSet

Post by pbreed »

The number in PrintObjectTobuffer is the MAX size that it is allowed to use.
So we start with:
char packJSON[1000];
you then do memset..
memset(packJSON,'\n',sizeof(packJSON));
you fill the 1000 bytes with \n....

what will strlen(packJSON) return?
strlen starts at the beginning of packJSON and moves forward till it finds a null.
we filled All of packJSON with \n so strlen won't find a 0 there...
it will walk off the end of packJSON into other memory looking for the null...
it may never find one...in which case it will eventually trap.....
what were 100% sure of is it won't return 1000....>



What I think you really want to do is this:
(I'm not the web socket expert, but I think this does it...)

WebSocket::ws_setoption( ws_fd, SO_NOPUSH); //Allows multiple writes
JsonOutObject.PrintObjectToFd( ws_fd);
write(ws_fd,"\n\n",2);
WebSocket::ws_clroption( ws_fd, SO_NOPUSH);
WebSocket::ws_flush(ws_fd);
















if the JSON is smaller than that it does not use the buffer...
Post Reply