Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
chaochichen
Visitor

ParseJson truncates large integer values

Hi,

I am working on a video streaming channel application which retrieves video metadata from a web service in JSON format. The retrieved JSON data contain video assets' original upload time in milliseconds since Epoch. For instance, a video file was uploaded on "02/28/2013 20:41:44" (1362112904015 Epoch in milliseconds), the retrieved JSON data would look like:

{
"url": "http://www.SomeCDN.com/videos/example.mp4", "upload_time": 1362112904015, ...
}

I used a simple BrightScript code to parse the JSON result:
getVideoRequest = CreateObject("roUrlTransfer")
getVideoRequest.SetURL("http://api.example.com/services/rest/getVideo?file_id=123456")
response = ParseJson(getVideoRequest.GetToString())

The parsed "upload_time" is truncated to 608271183 (04/10/1989 21:13:03) from its original 1362112904015. Obviously, the ParseJson() can't properly handle larger than 4-byte integer values.

Is there a way to instruct ParseJson() function to use DOUBLE's to store numbers that are larger than 4-byte integers? Or, are there better BrightScript JSON parsers out there?

Thanks,
Charles
0 Kudos
2 REPLIES 2
TheEndless
Channel Surfer

Re: ParseJson truncates large integer values

I've run into this before as well. My solution was to use RegEx to convert the values to strings before parsing, then do the conversion in BrightScript:

regex = CreateObject("roRegex", Chr(34) + "upload_time" + Chr(34) + ":\s([0-9]+),", "i")
json = regex.ReplaceAll(json, Chr(34) + "upload_time" + Chr(34) + ": " + Chr(34) + "\1" + Chr(34) + ",")

This can be slow on larger JSON strings, but, while obviously not ideal, it seems to work well otherwise.

You may need to modify the spacing around the colon, depending on how your JSON is formatted. I could have accounted for it automatically in the regex, but I'm too lazy.. 😛
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos
RokuJoel
Binge Watcher

Re: ParseJson truncates large integer values

I filed a bug on this last week as Brightcoves's JSON has this issue. A fix of some kind is in the works, although it might not be for a while. In the meantime, Regex to fix the issue or switching to XML are your best bets, some APIs like Brightcove have this as an option, appending &output=mrss or &output=rss to your url will give you an xml formatted string you can work with.

- Joel
0 Kudos