I hope it's not bad form to dig up an old conversation but I'm having a 
similar issue under Sequel 5.41

The block from to_json yields Sequel::Plugins::JsonSerializer::Literal 
objects for nested associations but I'd like associations and nested 
associations to be plain old Ruby Hashes.

None of the approaches below (taken and modified 
from https://groups.google.com/g/sequel-talk/c/T5eCp3U_EZo/m/qZ2FqxyVAgAJ) 
seem to achieve this:

require "sequel"


DB = Sequel.connect("sqlite:/")


DB.create_table(:a1s){primary_key :id}
DB.create_table(:a2s){primary_key :id; foreign_key :a1_id, :a1s}
DB.create_table(:a3s){primary_key :id; foreign_key :a2_id, :a2s}
DB.create_table(:a4s){primary_key :id; foreign_key :a3_id, :a3s}


Sequel::Model.plugin :json_serializer
class A1 < Sequel::Model; end
class A2 < Sequel::Model; end
class A3 < Sequel::Model; end
class A4 < Sequel::Model; end
A1.one_to_many :a2s
A2.one_to_many :a3s
A3.one_to_many :a4s


A4.create(:a3_id=>A3.create(:a2_id=>A2.create(:a1_id=>A1.create.id).id).id)
puts "A1#to_json"
A1.each do |a1|
p a1.to_json(:include=>{:a2s=>{:include=>{:a3s=>{:include=>:a4s}}}})
end
# 
"{\"id\":1,\"a2s\":[{\"id\":1,\"a1_id\":1,\"a3s\":[{\"id\":1,\"a2_id\":1,\"a4s\":[{\"id\":1,\"a3_id\":1}]}]}]}"
# JSON string, no thanks!


puts "A1#to_json yield to block"
A1.each do |a1|
a1.to_json(:include=>{:a2s=>{:include=>{:a3s=>{:include=>:a4s}}}}) do |obj|
puts obj
end
end
# {"id"=>1, 
"a2s"=>[#<Sequel::Plugins::JsonSerializer::Literal:0x000055bdf144c168 
@json="{\"id\":1,\"a1_id\":1,\"a3s\":[{\"id\":1,\"a2_id\":1,\"a4s\":[{\"id\":1,\"a3_id\":1}]}]}">]}
# Hash, but nested association is a Literal.  This is the closest I seem to 
be able to get, but it's not what I need


puts "A1.json_serializer_opts.to_json"
p 
A1.eager(:a2s=>{:a3s=>:a4s}).json_serializer_opts(:include=>{:a2s=>{:include=>{:a3s=>{:include=>:a4s}}}}).to_json
# 
"[{\"id\":1,\"a2s\":[{\"id\":1,\"a1_id\":1,\"a3s\":[{\"id\":1,\"a2_id\":1,\"a4s\":[{\"id\":1,\"a3_id\":1}]}]}]}]"
# JSON string, no thanks!


puts "A1.json_serializer_opts.to_json yield to block"
A1.eager(:a2s=>{:a3s=>:a4s}).json_serializer_opts(:include=>{:a2s=>{:include=>{:a3s=>{:include=>:a4s}}}}).to_json
 
{ |o| p o }
# [#<Sequel::Plugins::JsonSerializer::Literal:0x000055bdf13d1f58 
@json="{\"id\":1,\"a2s\":[{\"id\":1,\"a1_id\":1,\"a3s\":[{\"id\":1,\"a2_id\":1,\"a4s\":[{\"id\":1,\"a3_id\":1}]}]}]}">]
# Whole thing is a Literal.  :-(


puts "A1.to_json"
p 
A1.eager(:a2s=>{:a3s=>:a4s}).to_json(:include=>{:a2s=>{:include=>{:a3s=>{:include=>:a4s}}}})
# 
"[{\"id\":1,\"a2s\":[{\"id\":1,\"a1_id\":1,\"a3s\":[{\"id\":1,\"a2_id\":1,\"a4s\":[{\"id\":1,\"a3_id\":1}]}]}]}]"
# JSON string, no thanks!

puts "A1.to_json yield to block"
A1.eager(:a2s=>{:a3s=>:a4s}).to_json(:include=>{:a2s=>{:include=>{:a3s=>{:include=>:a4s}}}})
 
{ |o| p o }
# [#<Sequel::Plugins::JsonSerializer::Literal:0x000055bdf13a5278 
@json="{\"id\":1,\"a2s\":[{\"id\":1,\"a1_id\":1,\"a3s\":[{\"id\":1,\"a2_id\":1,\"a4s\":[{\"id\":1,\"a3_id\":1}]}]}]}">]
# Whole thing is a Literal.  :-(

Thanks for your help on this,
Ryan

On Tuesday, August 22, 2017 at 5:31:31 PM UTC-7 Jeremy Evans wrote:

> On Tuesday, August 22, 2017 at 5:15:48 PM UTC-7, Tom Wardrop wrote:
>>
>> I'm basically after something like #to_json, but which obviously returns 
>> a standard Ruby hash instead of a JSON string. #to_json allows you to 
>> easily :include associations and other fields. Is there anything like that 
>> for Ruby hashes?
>>
>
> to_json now takes a block yielding the object that would be converted to 
> JSON. You can now do something like:
>
> def hash_with_foo
>   to_json(:include=>:foo){|hash| return hash}
> end
>
> The graph_each extension works on a dataset, not on a model object, and it 
> uses separate subhashes for each table (not just for associated tables), so 
> it won't work for what you want.
>
> Thanks,
> Jeremy
>

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sequel-talk/a64fb17b-d9bb-43e0-93d5-d1e1edeafd16n%40googlegroups.com.

Reply via email to