My app imports some csv data, and I build an array, serialize it, and save it in a database field. I save it in serialize form because the data imported each time has a different structure … sometimes 3 columns, sometimes 10 columns, etc.
[[“fruit”, “price”, “weight”],
[“Apple”, 1.23, 4.5],
[“Orange”, 5.2, 3.3]]
The imported data has quotes around strings, and no quotes around numbers, so during the import I am able to preserve the type of each element.
But when I serialize it (to save in a database field) with ActiveSupport::JSON.encode(csv_data)
each element gets saved as a string
[[“fruit”, “price”, “weight”],
[“Apple”, “1.23”, “4.5”],
[“Orange”, “5.2”, “3.3”]]
so when I de-serialize it, the numeric values have been converted to strings, but I need them as numeric for the post-processing.
The problem is, each data import is different… sometimes it;s all numeric, sometimes the first column is string, sometimes 2 or 3 columns might be strings.
I don;t care if i use JSON or not — I just need a way to encode and decode the data so I can stuff it into a database field, without losing the ‘type’ durign the decode.
Should I be using some sort of xml method instead of JSON?
Marshal to the rescue!