Nesting data structures in Rhom/Redis

102 views
Skip to first unread message

David Jantzen

unread,
Feb 12, 2012, 8:24:20 PM2/12/12
to rhom...@googlegroups.com
Hello,

I have a backend resource generating a nested data structure something like this:

outer_hash => { 
    :outer_key => 'outer_value', 
    :inner_array => [ 
        { 
           :inner_key1 => 'inner_value1'
           :inner_key2 => 'inner_value2'
        }, 
        {  :inner_key1 => 'inner_value1'
           :inner_key2 => 'inner_value2'
        }
     ]
}

When it goes into Redis and the property bag table in Rhom, the inner array gets collapsed into a single string with all keys and values concatenated together. What are some good ways of working around this? I've read suggestions like prefixing all of the inner items and bringing them up to the top level like so:

outer_hash => {
    :outer_key => 'outer_value', 
    :inner_array_0_inner_key1=> 'inner_value1',
    :inner_array_0_inner_key2 => 'inner_value2'
    :inner_array_1_inner_key1=> 'inner_value1',
    :inner_array_1_inner_key2=> 'inner_value2',
}

Is there a nicer way of doing this? Any way to instruct Redis and Rhom how to handle the nested structures?

Thanks much,
David

Burgess Lars-KDWC48

unread,
Feb 13, 2012, 12:04:14 PM2/13/12
to <rhomobile@googlegroups.com>
No, each object in Rhom and RhoConnect is a single hash.  Nested data structures should be done either with json or by using your own serialization techniques.

-Lars

--
You received this message because you are subscribed to the Google Groups "rhomobile" group.
To post to this group, send email to rhom...@googlegroups.com.
To unsubscribe from this group, send email to rhomobile+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rhomobile?hl=en.

David Jantzen

unread,
Feb 14, 2012, 12:32:45 AM2/14/12
to rhom...@googlegroups.com
Hi Lars, thanks for the response. 

Now, if I serialize it myself in some format, it looks like I could perform Redis/Rhoconnect writes in my SourceAdapter's 'sync' method, correct? How would I deserialize it in Rhoconnect when it comes back out of Redis? Or would it just get handed to the Rhodes mobile app as JSON?

I guess there would also be model specific code in the Rhom class as well. Seems like it would require overrides to 'find' and 'save' or something. Is there an example for this kind of thing anywhere?

David

Burgess Lars-KDWC48

unread,
Feb 14, 2012, 3:03:43 AM2/14/12
to <rhomobile@googlegroups.com>
It would just treat it as the rest of the attributes, strings.  Whenever you need to access this particular attribute you would need to parse it with json.

-Lars

David Jantzen

unread,
Feb 15, 2012, 10:28:41 PM2/15/12
to rhom...@googlegroups.com
That makes sense, but I'm running into a strange error when try to use JSON inside of the Rhodes app:

 02/15/2012 19:25:48:626 b0457000              RhoRuby| require_compiled: json
I 02/15/2012 19:25:48:626 b0457000              RhoRuby| require_compiled: json/common
I 02/15/2012 19:25:48:628 b0457000              RhoRuby| require_compiled: json/version
I 02/15/2012 19:25:48:629 b0457000              RhoRuby| require_compiled: json/pure
I 02/15/2012 19:25:48:630 b0457000              RhoRuby| require_compiled: json/pure/generator
I 02/15/2012 19:25:48:633 b0457000                  APP| App error: undefined method `parse' for JSON:Module

My code is pretty straightforward:

    require 'json'
    raw_routine = Routine.find(@params['id'])
    @routine = JSON.parse(raw_routine['json'])

This is Rhodes 3.3.2. What am I doing wrong?

Thanks,
David

David Jantzen

unread,
Feb 16, 2012, 9:58:22 PM2/16/12
to rhom...@googlegroups.com
Aha, use Rho::JSON.parse. It sure would be nice if the regular JSON.parse method were overridden to say this.
Reply all
Reply to author
Forward
0 new messages