Looks like workaround here
http://sequel.jeremyevans.net/rdoc-plugins/classes/Sequel/Plugins/JsonSerializer.html
didn't work anymore. Here is a gist https://pastebin.com/1xdxRB6q
[1] pry(main)> session_1 = Session.create(start_time: Time.now)
=> #<Session @values={:id=>1, :start_time=>2018-05-22 12:04:47 +0400,
:crawlers=>nil}>
[2] pry(main)> session_2 = Session.create(start_time: Time.now)
=> #<Session @values={:id=>2, :start_time=>2018-05-22 12:04:49 +0400,
:crawlers=>nil}>
[3] pry(main)> Session.to_json(include: [:running_time])
=>
"[{\"json\":\"{\\\"id\\\":1,\\\"start_time\\\":\\\"2018-05-22T12:04:47.358+04:00\\\",\\\"crawlers\\\":null,\\\"running_time\\\":4.384960448}\"},{\"json\":\"{\\\"id\\\":2,\\\"start_time\\\":\\\"2018-05-22T12:04:49.099+04:00\\\",\\\"crawlers\\\":null,\\\"running_time\\\":2.647039953}\"}]"
Is there still any way to make it work with active support?
On Thursday, August 16, 2012 at 8:45:20 PM UTC+4, Jeremy Evans wrote:
>
> On Thursday, August 16, 2012 9:12:43 AM UTC-7, Alex Cavalli wrote:
>>
>> Hi there,
>>
>> I'm using Sequel 3.38.0 and ActiveSupport 3.2.6.
>>
>> I'm trying to use the JSON Serializer plugin to generate JSON for some
>> nested model structures. The project that I'm working on also makes use of
>> some ActiveSupport features. Unfortunately, this appears to create a
>> conflict when ActiveSupport overrides the to_json methods on Hash and
>> Array. I've found a workaround in monkeypatching the monkeypatched Hash and
>> Array classes, but it's pretty ugly. Is there a better way?
>>
>> Here's a sample gist that shows the conflict:
>>
>> <script src="https://gist.github.com/3371268.js"> </script>
>>
>> Running this script will produce the following:
>>
>> Good JSON
>>
>> [{"name":"Bob","id":1,"albums":[{"label":"Summer","tracks":[{"id":1,"length":10,"json_class":"Track"},{"id":2,"length":20,"json_class":"Track"}],"id":1,"json_class":"Album"},{"label":"Winter","tracks":[{"id":3,"length":30,"json_class":"Track"},{"id":4,"length":40,"json_class":"Track"}],"id":2,"json_class":"Album"}],"json_class":"Artist"}]
>>
>> Bad JSON
>>
>> [{"json":"{\"name\":\"Bob\",\"id\":1,\"albums\":[{\"json\":\"{\\\"label\\\":\\\"Summer\\\",\\\"tracks\\\":[{\\\"id\\\":1,\\\"length\\\":10},{\\\"id\\\":2,\\\"length\\\":20}],\\\"id\\\":1,\\\"json_class\\\":\\\"Album\\\"}\"},{\"json\":\"{\\\"label\\\":\\\"Winter\\\",\\\"tracks\\\":[{\\\"id\\\":3,\\\"length\\\":30},{\\\"id\\\":4,\\\"length\\\":40}],\\\"id\\\":2,\\\"json_class\\\":\\\"Album\\\"}\"}],\"json_class\":\"Artist\"}"}]
>>
>> Good JSON, ugly workaround
>>
>> [{"name":"Bob","id":1,"albums":[{"label":"Summer","tracks":[{"id":1,"length":10,"json_class":"Track"},{"id":2,"length":20,"json_class":"Track"}],"id":1,"json_class":"Album"},{"label":"Winter","tracks":[{"id":3,"length":30,"json_class":"Track"},{"id":4,"length":40,"json_class":"Track"}],"id":2,"json_class":"Album"}],"json_class":"Artist"}]
>>
>>
>> Is there a better workaround or some configuration step I'm missing?
>>
>
> Don't use ActiveSupport::JSON? Unfortunately, there really isn't a better
> way. ActiveSupport::JSON basically completely takes over the JSON
> serialization process in a way incompatible with the standard JSON
> library. I consider that a bug in ActiveSupport, but it's unlikely they
> will change it. Even getting obvious bugs fixed in ActiveSupport has been
> a chore in my experience. Your workaround basically undoes what
> ActiveSupport does, using the standard JSON library for serializing hashes
> and arrays, which is why it works.
>
> I'm not sure if there is a way to use the ActiveSupport features you want
> without requiring ActiveSupport::JSON. ActiveSupport 3 is theoretically
> supposed to be modular in that you can only require certain parts without
> the whole ball of wax. However, in my experience, requiring one feature
> often loads other unrelated and undesired features. For example, loading
> ActiveSupport's Duration class requires loading ActiveSupport's inflection
> support, even though if you just want to use Duration for time
> calculations, there is no need for inflections.
>
> 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 post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.