\n and json.decode Freezes Server

I am pulling information from a mysql db via PerformHttpRequest. Some of the information I am obtaining is a few sentences or so, but sometimes there is a line break needed in the description. Somewhere along the way (I think in the PHP script), the line breaks are translated into a ‘\n’. I put all the information into a table in my server, but whenever I try to json.decode anything that has a \n in it, the server hangs. No errors or anything. All response just stops.

I thought of doing a string.gsub, but I would like to maintain the format the text is in, especially if I wish to display it out to a page using NUI. Does anybody have any ideas on what I can do to get around this issue? Thanks!

Just to double check, you’re putting a “test” json.decode when the script is loaded, right?

For some reason it’s needed I order to use the decode function (I don’t know why). It looks something like the code below (on my phone so, can’t double check)

local testObj, pos, testErro = json.decode("{'test':'test'}")

@Havoc No, I was not using that test decode. However, I do have a couple other encode/decode things that work perfectly. The only time I have an issue is when \n is included in any of the text. Then the server just completely hangs. I did throw in the test thing, but no luck.

I’ve confirmed this with my own little test here:

[code]local testbullshit = {}
testbullshit[‘one’] = {one = “one”, two = “two”, three = “three \n\nSUP three”}
testbullshit[‘two’] = {one = “one”, two = “two”, three = “three three”}
testbullshit[‘three’] = {one = “one”, two = “two”, three = “three”}
testbullshit[‘four’] = {one = “one”, two = “two”, three = “three three \n\nSUP”}

local testbullshit1 = json.encode(testbullshit)
print(testbullshit1)
–local testbullshit2 = json.decode(testbullshit1)
–print(testbullshit2)[/code]

Those line where I decode it (commented out) is where the server hangs.

Have you tried escaping the newline characters?

I’ve just found this stack overflow question which seems to be a similar issue to the one you’re running into.

@Havoc Yep. Even if I escape all the \n, still hangs when I try to decode.

@Havoc Right after the sever hung, I got a phone call, so I didn’t close the server. After about 5 minutes, this error came up and the server was responsive again

That line 124 is the local testbullshit2 = json.decode(testbullshit1)

The only thing I can think of is to use another json library and see if that handles new lines better than dkjson.

I can’t really see the screenshot properly (I’m going to blame mobile imgur for that :stuck_out_tongue:) but, I believe line 435 in the dkjson lib that’s causing the issue.

So, either use another library and see if you get better results or, wait for someone smarter than me to help you :slight_smile:

@Havoc I have no idea how I would even use a different json library. :sweat_smile: Not uh… too familiar with that. I need smart people! lol

Well, I’d look for a pure lua json library. This should give you some lua code that you can drop into a file that you can either load via resource (just add it to the __resource.lua file?). Or load it in with the other system resources (I don’t know if you could just drop the new file into the folder where the other files are or what).

I’ll take a look into it later on. I ran another test with thislocal testbullshitty = {} testbullshitty['one'] = {one = "one \n two"} local testbullshitty1 = json.encode(testbullshitty) print(testbullshitty1) local testbullshitty2 = json.decode(testbullshitty1) print(testbullshitty2)
Oddly enough… there were no errors

Also, as for the library, I assume you mean something like this here?: https://gist.github.com/tylerneylon/59f4bcf316be525b30ab

I tried a few other libraries (hopefully correctly?), but still wasn’t getting any luck. I did do some digging into the dkjson.lua, and it does automatically escape the \n. I have no idea what could be going wrong with this. I’ll probably just have to live without the \n, or find some other way to do it. I’ll probably just see if I can replace the \n with a <br> or something.

Edit: So, on the PHP side, before json_encoding it, I just replaced the line breaks with a <br>. I tried to do <br />, but it gave me issues because of the / again, even though it was escaped – odd. This should work for me for now… I hope.