Note that there are some explanatory texts on larger screens.

plurals
  1. PORead json serialised objects back from a file
    primarykey
    data
    text
    <p>I am aiming to serialise a set of objects into a file so as to create a backup. I have the start of that working, using a methods on the models (simplified here, assuming I have two ActiveRecords foo and bar): </p> <pre><code>def backup(file, foo, bar) file.write(foo.to_json(root: true)) file.write(bar.to_json(root: true)) end </code></pre> <p>This gives me a file as I desire, in this case with two records: </p> <pre><code>{"foo":{"Account_id":1,"Name":"F","created_at":"2013-04-16T10:06:19Z","id":1,"updated_at":"2013-04-20T11:36:23Z"}} {"bar":{"Account_id":1,"Name":"B","created_at":"2013-04-16T10:06:19Z","id":1,"updated_at":"2013-04-20T11:36:23Z"}} </code></pre> <p>At a later date I then want to read that backup in and reinstantiate those objects, probably then persisting them back to the database. My aim is to iterate through the file checking the type of each object, then instantiating the right object. </p> <p>I have part of the logic, but not yet all of it, I haven't worked out how I determine the type of each serialised object before I instantiate it. The code I have for a restore is as follows: </p> <pre><code>def restore(file) file.each_line do |line| **&lt;some magic that parses my line into objectType and objectHash&gt;** case objectType when :foo Foo.new.from_json(objectHash) Foo.process Foo.save! when :bar Bar.new.from_json(objectHash) Bar.process Bar.save! end end end </code></pre> <p>What I'm looking for is the bit that goes in the "some magic" section. I can just write the code to parse the line directly to determine whether it's a foo or a bar, but I feel like there's probably some tricky Rails/Ruby way to do this that is automatic. Unfortunately, in this case Google is not being my friend. All I can see are pages that are focused on json in the web requests, but not parsing json back in this way. Is there something I'm missing, or should I just write the code to split the string directly and read the object type?</p> <p>If I do write the code to split the string directly, I would write something along the lines of: </p> <pre><code>objectType = line[/^{"(\w*)"=&gt;(.*)}/, 1] objectHash = line[/{"(\w*)"=&gt;(.*)}/, 2] </code></pre> <p>This is pretty ugly and I'm sure there's a better way (which I'm still looking into), but I'm not sure that this is even the right approach v's there being something that automatically looks at a json representation and knows from the root value what object to instantiate.</p> <p>Lastly, the actual instantiation using from_json isn't working either, it isn't populating any of the fields on my ActiveRecord. It gives me nil parameters, so I think the parse syntax isn't right.</p> <p>So, that makes three questions: </p> <ol> <li>Is there a way to determine which object it is that I'm just missing, that is much cleaner?</li> <li>If there isn't and I need to use a regexp, is there a syntax to get both bits of the line parsed in a single go, rather than my two lines with the same regexp?</li> <li>The from_json syntax appears unhappy. Is there a syntax I'm missing here? (no longer a question - the code above is fixed, I was using as_json when it should have been to_json, although the documentation is rather unclear on that....)</li> </ol> <p>(Note: edits over time to clarify my question, and because I've now got a regexp that works (didn't before), but still not sure it's very elegant.)</p> <p>Further information - one of the problems here, as I dig into it further, is that the as_json isn't actually giving me json - what I have in the file is a hash, not json at all. Further, the values for created_at and lastupdated_at in the hash aren't quoted - so basically that's what's causing the parse on the way back in to fail. I've worked out that I should use to_json instead of as_json, although the documentation suggests that as_json should work.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload