Error Deserializing JSON string

Report any Hollywood bugs here
Post Reply
User avatar
midwan
Posts: 74
Joined: Sun Jun 19, 2016 1:15 pm
Location: Sweden

Error Deserializing JSON string

Post by midwan »

I've run into a strange problem, while trying to deserialize JSON strings. Or perhaps getting them from hURL, either way.
Sometimes it seems to work, but other times I'm getting an error while deserializing, similar to the below:

Code: Select all

* Failed writing body (0 != 2176)
* Failed writing data
* Closing connection 0
I'm using hURL to make a call to a public REST API, get the JSON back, and parse it (deserialize, and write each item out separately).
I'm always able to get the full JSON using a normal browser, so the server side should be OK.

I suspect that either hURL is not getting back the full response sometimes, or perhaps the deserialization fails due to some other, internal reason?
I added a DebugPrint to write out the full JSON response I got from hURL, and in the example above, it doesn't seem complete:

Code: Select all

Received response in json:  [{"id":"109415452853248522","created_at":"2022-11-27T11:08:02.000Z","in_reply_to_id":null,"in_reply_to_account_id":null,"sensitive":false,"spoiler_text":"","visibility":"public","language":"zh","uri":"https://bgme.me/users/wakanatsu/statuses/109415450678603673","url":"https://bgme.me/@wakanatsu/109415450678603673","replies_count":0,"reblogs_count":0,"favourites_count":0,"edited_at":null,"content":"\u003cp\u003e临近六点,人群已 经被警方从路口驱赶至五原路整条路上,一边陆续后退,一边依然在喊着“放人”的口号\u003cbr\u003e一位骑着电动车的大妈路过抱怨着什么,旁边的一个男生大声朝她喊着 :“我们已经清醒了,是你们还在装睡!”\u003cbr\u003e\u003ca href=\"https://bgme.me/tags/%E4%B8%8A%E6%B5%B7%E4%B9%8C%E9%B2%81%E6%9C%A8%E9%BD%90%E4%B8%AD%E8%B7%AF\" class=\"mention hashtag\" rel=\"nofollow noopener noreferrer\" target=\"_blank\"\u003e#\u003cspan\u003e上海乌鲁木齐中路\u003c/span\u003e\u003c/a\u003e\u003c/p\u003e","reblog":null,"account":{"id":"1241344","username":"wakanatsu","acct":"wakanatsu@bgme.me","display_name":"✨ AA纱障修补恶魔退治拉维 兰","locked":true,"bot":false,"discoverable":false,"group":false,"created_at":"2020-07-10T00:00:00.000Z","note":"\u003cp\u003ePC宝妈、袭来!(暂时不接受 没有原创嘟文的关注请求)\u003c/p\u003e","url":"https://bgme.me/@wakanatsu","avatar":"https://files.mastodon.social/cache/accounts/avatars/001/241/344/original/28a44abd788fd7c7.jpg","avatar_static":"https://files.mastodon.social/cache/accounts/avatars/001/241/344/original/28a44abd788fd7c7.jpg","header":"https://files.mastodon.social/cache/accounts/headers/001/241/344/original/60c33f905f63cb13.jpg","header_static":"https://files.mastodon.social/cache/accounts/headers/001/241/344/original/60c33f905f63cb13.jpg","followers_count":188,"following_count":117,"statuses_count":16355,"last_status_at":"2022-11-27","emojis":[],"fields":[{"name":"50%","value":"同人女日常发癫","verified_at":null},{"name":"20%","value":"妄议朝政和时事","verified_at":null},{"name":"20%","value":"分享吃喝玩乐和猫图","verified_at":null},{"n
I tried limiting down the number of items in the JSON response (from the API side), which seemed to help things a bit, but I'm still getting errors every few attempts, so I'm not sure.

When this occurs, it closes down the whole application, so I can't even retry - which is a real problem. :(

EDIT: I'm using Hollywood 9.1, with the latest versions of hURL (1.2) and RapaGUI (2.1) for this project
User avatar
midwan
Posts: 74
Joined: Sun Jun 19, 2016 1:15 pm
Location: Sweden

Re: Error Deserializing JSON string

Post by midwan »

Update: while debugging this further, I found one another interesting piece to the puzzle. It seems that if I comment out the DeserializeTable function in the callback, and only leave the DebugPrint I had there (to spit out the raw json string I received), it doesn't crash.

Specifically, this is my callback function:

Code: Select all

/*
** Parse JSON response and iterate through items
*/
Function p_ParseResponse(json)
    If #DEBUG Then DebugPrint("Received response in json: ", json)
    table = DeserializeTable(json)
    ForEach(table, p_WriteHtmlView)
EndFunction
If I comment the line that does the DeserializeTable, it works always, with the DebugPrint showing the full JSON string (as expected.
But If I enable it, it will crash with the DebugPrint showing a cut-off JSON string.

How does this make sense?
User avatar
midwan
Posts: 74
Joined: Sun Jun 19, 2016 1:15 pm
Location: Sweden

Re: Error Deserializing JSON string

Post by midwan »

Answering my own question above:

It makes sense if you pay closer attention to the manual, indicating that the callback is not guaranteed to get the full response in one go. It might get chunks of it, with unknown sizes.

I broke out the DeserializeTable function from the callback function, and only trigger it after the callback is done, and now it seems to work without problems.
SamuraiCrow
Posts: 475
Joined: Fri May 15, 2015 5:15 pm
Location: Waterville, Minnesota USA

Re: Error Deserializing JSON string

Post by SamuraiCrow »

Thanks for the tip!
I'm on registered MorphOS using FlowStudio.
Post Reply