Your Digital Media Has Never Looked So Good

 
kurtn718
Topic Author
Posts: 2
Joined: Tue Feb 11, 2014 10:17 pm

Built-in ToJSON function

Tue Feb 11, 2014 10:24 pm

Hi. Is there a built-in ToJSON function, similar to ParseJSON?

I couldn't find one in the docs, but I was able to write the following function taking an roAssociativeArray as the input - which works for what I need.

If there is a built-in function, I'll probably replace the code below with that function. If one doesn't exist, then this is my first contribution to the forum. :) If there is a way to write the code better, please offer suggestions.

Thanks,

Kurt
-------
Function ToJSON(jsonData as Object) as String
jsonRequest = "{"
for each key in jsonData
if jsonRequest <> "{" then
jsonRequest = jsonRequest + ","
endif

jsonRequest = jsonRequest + chr(34) + key + chr(34) + " : " + chr(34) + jsonData[key] + chr(34)
end for
jsonRequest = jsonRequest + "}"

return jsonRequest
End Function
 
User avatar
RokuMarkn
Roku Engineering
Posts: 1581
Joined: Mon Jun 09, 2008 9:20 am

Re: Built-in ToJSON function

Wed Feb 12, 2014 10:42 am

There is an undocumented global function which does what you want:
FormatJSON(aa as Object) as String
I'm not sure why this isn't documented, probably an oversight.

--Mark
 
User avatar
TheEndless
** Valued Community Member **
Posts: 9231
Joined: Mon Oct 04, 2004 10:15 am
Location: US
Contact:

Re: Built-in ToJSON function

Wed Feb 12, 2014 11:14 am

RokuMarkn wrote:
There is an undocumented global function which does what you want:
FormatJSON(aa as Object) as String
I'm not sure why this isn't documented, probably an oversight.

--Mark

Wow.. that should definitely be documented! I've written and refined three different versions of the same functionality so far in BS.. :P

Any chance it's also available in the 3.1 firmware?
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)
 
Rek
Posts: 97
Joined: Mon Jul 08, 2013 9:22 am

Re: Built-in ToJSON function

Wed Feb 12, 2014 1:45 pm

TheEndless wrote:
RokuMarkn wrote:
There is an undocumented global function which does what you want:
FormatJSON(aa as Object) as String
I'm not sure why this isn't documented, probably an oversight.

--Mark

Wow.. that should definitely be documented! I've written and refined three different versions of the same functionality so far in BS.. :P

Any chance it's also available in the 3.1 firmware?


Agreed, I hit the same problem and ended up relying on librokudev's rdJSONBuilder function.
 
malloys
Posts: 208
Joined: Sat Jul 24, 2010 12:26 pm

Re: Built-in ToJSON function

Mon Feb 17, 2014 12:16 pm

TheEndless wrote:
RokuMarkn wrote:
There is an undocumented global function which does what you want:
FormatJSON(aa as Object) as String
I'm not sure why this isn't documented, probably an oversight.

--Mark

Wow.. that should definitely be documented! I've written and refined three different versions of the same functionality so far in BS.. :P

Any chance it's also available in the 3.1 firmware?


Bump.

Roku support...
Any answer on the 3.1 question?
Also, it appears the FormatJSON global function is still not in the online docs. - Can we get the docs updated?
BTW... it's hard to be 100% sure the docs haven't been updated, since the developer wiki's dashboard functionality that listed recent changes went missing sometime in the last month or two - any chance we can get that reinstated? That was a very useful feature.
 
EnTerr
** Valued Community Member **
Posts: 3834
Joined: Sun Jan 02, 2011 2:41 am

Re: Built-in ToJSON function

Mon Feb 17, 2014 2:01 pm

malloys wrote:
Bump.
Roku support...
Any answer on the 3.1 question?
Also, it appears the FormatJSON global function is still not in the online docs. - Can we get the docs updated?
BTW... it's hard to be 100% sure the docs haven't been updated, since the developer wiki's dashboard functionality that listed recent changes went missing sometime in the last month or two - any chance we can get that reinstated? That was a very useful feature.

Useful it is indeed, though in a corner hard to find: http://sdkdocs.roku.com/pages/recentlyu ... key=sdkdoc
Good luck (you will need it) with the first two items.
 
malloys
Posts: 208
Joined: Sat Jul 24, 2010 12:26 pm

Re: Built-in ToJSON function

Mon Feb 17, 2014 2:53 pm

EnTerr wrote:
malloys wrote:
Bump.
Roku support...
Any answer on the 3.1 question?
Also, it appears the FormatJSON global function is still not in the online docs. - Can we get the docs updated?
BTW... it's hard to be 100% sure the docs haven't been updated, since the developer wiki's dashboard functionality that listed recent changes went missing sometime in the last month or two - any chance we can get that reinstated? That was a very useful feature.

Useful it is indeed, though in a corner hard to find: http://sdkdocs.roku.com/pages/recentlyu ... key=sdkdoc


Thanks - discovered that myself a few minutes after posting.
FYI, the view I was referring to was the Dashboard URL/level itself ( http://sdkdocs.roku.com/dashboard.action ), but this looks similar enough.

In any case, I think we all agree that FormatJSON() still needs to be documented in the BrightScript Language Guide.

Cheers
 
User avatar
RokuMarkn
Roku Engineering
Posts: 1581
Joined: Mon Jun 09, 2008 9:20 am

Re: Built-in ToJSON function

Mon Feb 17, 2014 4:44 pm

It's not currently in the 3.1 firmware.

--Mark
 
User avatar
RokuJoel
Posts: 1758
Joined: Mon Nov 14, 2011 5:22 pm

Re: Built-in ToJSON function

Tue Feb 18, 2014 11:52 am

Before functions like FormatJSON are published in the documentation, they need to go through an API review process. Anything that hasn't, is potentially subject to change, so basing a public channel on an upublished function is somewhat risky.

- Joel
 
User avatar
gonzotek
** Valued Community Member **
Posts: 2206
Joined: Thu May 06, 2010 12:40 pm
Contact:

Re: Built-in ToJSON function

Tue Feb 18, 2014 2:25 pm

RokuJoel wrote:
Before functions like FormatJSON are published in the documentation, they need to go through an API review process. Anything that hasn't, is potentially subject to change, so basing a public channel on an upublished function is somewhat risky.

- Joel
You could tag such features as *experimental* or similar in the documentation(with a formal statement that they're subject to change/removal) and then receive potentially useful feedback on them like bugs, enhancement requests, and discussion of the feature documentation, all prior to having to officially support it. The way I see it, if it's going to be discussed in the forum anyway, it might as well be properly organized. There's a lot of precedent for documenting not-yet-released features in, just for instance, web browsers and web technologies.
Remoku.tv - A free web app for Roku Remote Control!
Want to control your Roku from nearly any phone, computer or tablet? Get started at http://help.remoku.tv
by Apps4TV - Applications for television and beyond: http://www.apps4tv.com
 
User avatar
TheEndless
** Valued Community Member **
Posts: 9231
Joined: Mon Oct 04, 2004 10:15 am
Location: US
Contact:

Re: Built-in ToJSON function

Sun Feb 23, 2014 2:19 pm

I think I found out why the FormatJSON function isn't published... Any objects with circular references will blow up the Roku.

For example:
    parentObject = {}
    childObject = {}
    parentObject.Child = childObject
    childObject.Parent = parentObject
    ?FormatJSON(parentObject)  ' <== Roku go BOOM
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)
 
EnTerr
** Valued Community Member **
Posts: 3834
Joined: Sun Jan 02, 2011 2:41 am

Re: Built-in ToJSON function

Sun Feb 23, 2014 11:57 pm

TheEndless wrote:
I think I found out why the FormatJSON function isn't published... Any objects with circular references will blow up the Roku.

Fun discovery (YAWRR)!
One would consider this a bug only if they think BRS shouldn't have a reboot command 8)

Musings: JSON cannot per-se represent structures with data cycles. It covers only a subset of "directed acyclic graphs". Non-corrupt trees, to be more specific (tree data structure where the root has no parent and each other node has exactly 1 parent). If not obvious how those are related, imagine drawing dictionary's root as a node with edges (labeled with the keys) pointing to value nodes. Array node A will have edges (labeled 0, 1, ...) pointing to the values as nodes A[0], A[1]... and that's it.

The example gives is a corrupt tree - "corrupted" by a cycle. And that can be done even with a single node:
InfiniteLoop = {}  'Cupertino, CA 95014
InfiniteLoop.next = InfiniteLoop
formatJSON(InfiniteLoop)

But there can be acyclic corrupted tree too, for example this
BrightScript Debugger> A = [1]      
BrightScript Debugger> t = {left:A, right:A}
BrightScript Debugger> ?formatJSON(t)
{"left":[1],"right":[1]}
This does not cause crash but the serialization does not represent the fact that both "left" and "right" were pointing to the same array (or node A has two parents, left and right). There are libraries that can correctly handle serialization of general directed graphs (e.g. Python's "pickle") but that's not really in JSON's job description. So it's the wrong thing to send such data to JSON.

The easiest way to wash hands is to document that the function works correct only for "proper trees". More elaborate one would be to change the code so during serialization it keeps track of already visited nodes (in a set/AA) and if a node is re-visited to blow up with "corrupt tree" error; that will take care of both cases above.

PS. Bonus bug in formatJSON - it only seems to work for AAs, but not for lists. Not a proper JSON:
BrightScript Debugger> ?formatJSON([1,2,3])

 
EnTerr
** Valued Community Member **
Posts: 3834
Joined: Sun Jan 02, 2011 2:41 am

Re: Built-in ToJSON function

Tue Oct 28, 2014 6:58 pm

As an update, at some point over the last 8 months, formatJSON has lost its undocumented status and has quietly been officiated a legal permanent resident. El papeles: BrightScript Language Reference # 7.19 FormatJson()

Seems some additional work was put into it. In particular `formatJSON([1, 2, 3])` works now. In addition, cyclic structures no more reboot Roku
BrightScript Debugger> h = {}: h.h = h
BrightScript Debugger> s = formatJSON(h)
BRIGHTSCRIPT: ERROR: FormatJSON: Maximum nesting depth exceeded
BrightScript Debugger> ? s

but the behavior with that or an "acyclic corrupted tree" remains undocumented.

Sadly, still does not work on firmware 3 models. All in all, i recommend using my toJSON() instead.
 
User avatar
RokuKevin
Roku Engineering
Posts: 796
Joined: Tue Sep 22, 2009 2:29 pm

Re: Built-in ToJSON function

Wed Oct 29, 2014 10:10 am

Please note that we're only updating the firmware on Pico devices in extreme circumstances to roll out security updates or other patches for critical bugs that require a hotfix.

Enhancements to the API like adding FormatJSON() function to the API will not be added to the v3.1 branch.

I would encourage you to utilize the firmware implementation on newer platforms to take advantage of the optimizations we can do in native code that cannot be done in Brightscript and implement your own brightscript version to support v3.1. There are several ways to implement an abstraction layer around this but you could do something simple like:

function toJSON(json as Object, flags = 0 as Integer) as String
if firmwareFourOrGreater() then
return formatJSON(json, flags)
else
REM your implementation here
end if
end function

Function firmwareFourOrGreater() As Boolean
version = CreateObject("roDeviceInfo").GetVersion()

major = Mid(version, 3, 1)

if Val(major) < 4 then
return false
end if
return true
End Function


--Kevin
 
EnTerr
** Valued Community Member **
Posts: 3834
Joined: Sun Jan 02, 2011 2:41 am

Re: Built-in ToJSON function

Wed Oct 29, 2014 2:32 pm

RokuKevin wrote:
Please note that we're only updating the firmware on Pico devices in extreme circumstances to roll out security updates or other patches for critical bugs that require a hotfix.

Enhancements to the API like adding FormatJSON() function to the API will not be added to the v3.1 branch.

"Pico"... it must be code-name for the NXP MIPS platform.

RokuKevin -
this sounds like an end-of-life for the 1xxx/2xxx models, can you post notice prominently (this forum and/or SDK wiki)? Something to state teh legacy boxes will always stay on firmware 3.x (so we don't have to ass-ume what to check for) and no new features/enhancements will be added but critical/security fixes.

And also, could you please, please (und noch ein Zucker Berg oben) mark clearly in the docs which functions are not supported in fw3? There are only 2 levels of the platform to speak of, so cannot say is too hard to maintain. formatJSON() is currently not marked with version#, implication being it works everywhere. Such un-labeling is not an isolated case, I have burned myself before.

Who is online

Users browsing this forum: No registered users and 6 guests