Closed airhorns closed 3 years ago
hi, unfortunately ATM the only way to convert hash into json is JSON.parse(hash.to_json)
as there is to #as_json
. We can extend Jennnifer::Mode::JSONConverter.from_hash
to recursively convert hash into json (this sounds achievable).
P.S. Product.create!(name: "example", config: "{\"foo\": \"bar\"}")
works because Jennnifer::Mode::JSONConverter.from_hash
automatically converts string into json
Some time before I had an idea to add support of embedded objects (similar to serialize
from rails) to allow map json from the database to the object with defined json mapping. The greatest obstacle is that model attributes must to be a database supported values which means nothing from Jennifer::DBAny
is supported ATM
This is resolved by #352
If I have a model with a
jsonb
column, it's a little tricky right now to populate that column on record create. With a model like:The thing that I tried first doesn't work:
because the
Hash(String, String)
isn't aJSON::Any
.Sadly, you can't really wrap hash literals in
JSON::Any
s in order to pass them in:both don't work as
JSON::Any
is really designed to be constructed by the JSON parser as opposed to manually.The only way I have figured out to actually get data into a jsonb column is by passing in JSON as a string:
which works, and notably saves the value as an actual JSONB value, and not a string inside a JSONB column.
It'd be nice (in this developers opinion) to be able to manipulate JSONB columns with something a bit higher level like a Hash, or even if we couldn't do that, an easy way to take a
JSON::Any
and turn it into something mutable and support serializing that without copying it back into the tricky-to-produce-manuallyJSON::Any
type. For Rails at least, it just returns JSONB columns as a Hash and then.to_json
s them on the way back to the database.