I store somewhat large JSON strings as text fields in my model but would like to have the data accessible as an OpenStruct variable when the instance of the model is initialized.
In other words:
Model: CrawlCache
Field: results, type: text #Storing a JSON String
When I run crawl = CrawlCache.find(x)
I'd like crawl.results not to be the string but rather the result of JSON.parse(crawl.result, object_class: OpenStruct)
My code so far is this:
after_initialize :set_results
def set_results
self.results = JSON.parse(self.results, object_class: OpenStruct)
end
However when I run the aforementioned crawl = CrawlCache.find(x)
, crawl.results.class
is still a String.
The reason I'd like to overwrite the orignial is for memory reasons, the strings are fairly large and I wouldn't want to have the string in memory AND the parsed object. That is why am not going the attr_accessor
route and naming it something else.