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: 
tembenite
Visitor

Convert Object or roArray to String

Is there an automatic way to convert an object to a String?
0 Kudos
4 REPLIES 4
tembenite
Visitor

Re: Convert Object or roArray to String

Nevermind, in poking around the examples I found:

Function tostr(any)

Within generalUtils.brs

This seems to do what I want.
0 Kudos
kbenson
Visitor

Re: Convert Object or roArray to String

Check out librokudev: https://github.com/rokudev

rdSerialize will take most simple data structures (without built in components) and turn it into a string that you can eval to get the data structure back.
https://github.com/rokudev/librokudev/b ... ialize.brs

rdJSON has both builder and parser functions to correctly turn (most?) JSON into a roku data structure or vice-versa.
https://github.com/rokudev/librokudev/b ... rdJSON.brs
-- GandK Labs
Check out Reversi! in the channel store!
0 Kudos
tembenite
Visitor

Re: Convert Object or roArray to String

I came up with the following:

Function toStr(v As Dynamic, levels = 1 As Integer, debug = 5 As Integer) As String
Log("toStr", 7, "vType:" + type(v) + " levels:" + Stri(levels), debug)
out = ""
if v = invalid return "invalid"
if type(v)= "roString" return v
if type(v) = "roFloat" OR type(v) = "Float" return Str(v)
if type(v) = "roInt" OR type(v) = "Integer" return Stri(v)
if type(v) = "roTimespan" return Stri(v.TotalMilliseconds()) + "ms"
if type(v) = "roDateTime" return Stri(v.asSeconds())
If type(v) = "roBoolean" Then
if(v) Then return "True" Else return "False"
End If
If type(v) = "roArray" Then
If levels > 0 Then
i = 0
out = chr(11) + "--- Start roArray ---" + chr(13) + chr(10)
for each e in v
out = out + chr(11) + "[" + Stri(i) + "]" + toStr(e, levels - 1) + chr(13) + chr(10)
i = i + 1
next
out = out + chr(11) + "--- End roArray ---" + chr(13) + chr(10)
return out
Else
return "{roArray}"
EndIf
End If
If type(v) = "roAssociativeArray" Then
out = chr(11) + "--- Start roAssociativeArray ---" + chr(13) + chr(10)
for each e in v
out = out + chr(11) + "[" + e + "]" + toStr(v[e], levels - 1) + chr(13) + chr(10)
next
out = out + chr(11) + "--- End roAssociativeArray ---" + chr(13) + chr(10)
return out
Else
return "{roAssociativeArray}"
End If
return "{unknown}"
End Function
0 Kudos
chaklasiyanikun
Roku Guru

Re: Convert Object or roArray to String

I found similar issue. Is there available any good solution. I checked my variable datatype using Type(Myval). It's roArray and I'm trying to convert with string. I tried above solution add genaraliutilities and Add ToStr() in Code. and Before Adding Utilities Simply I write ToStr() But both way doesn't work.

Is there any solution for this?

0 Kudos
Need Assistance?
Welcome to the Roku Community! Feel free to search our Community for answers or post your question to get help.

Become a Roku Streaming Expert!

Share your expertise, help fellow streamers, and unlock exclusive rewards as part of the Roku Community. Learn more.