On Friday, April 10, 2020 at 4:44:48 PM UTC-7, John Devine wrote:
>
> The docs suggest overriding ActiveSupport's implementation of Hash#to_json
> and Array#to_json.
>
> I've been trying to figure out why the suggested method of overriding the
> methods doesn't work for me:
>
> class Array
> def to_json(options = {})
> JSON.generate(self)
> end
> end
>
>
> class Hash
> def to_json(options = {})
> JSON.generate(self)
> end
> end
>
>
>
> Turns out ActiveRecord is using Class#prepend to define those methods
> instead of just overriding them, so even if you override the methods with
> your own definitions in an initializer, they won't be called:
>
> module ActiveSupport
> module ToJsonWithActiveSupportEncoder # :nodoc:
> def to_json(options = nil)
> if options.is_a?(::JSON::State)
> # Called from JSON.{generate,dump}, forward it to JSON gem's
> to_json
> super(options)
> else
> # to_json is being invoked directly, use ActiveSupport's encoder
> ActiveSupport::JSON.encode(self, options)
> end
> end
> end
> end
>
> [Object, Array, FalseClass, Float, Hash, Integer, NilClass, String,
> TrueClass, Enumerable].reverse_each do |klass|
> klass.prepend(ActiveSupport::ToJsonWithActiveSupportEncoder)
> end
>
> So I've countered with my own prepend:
>
> module FixJson
> def to_json(options = {})
> JSON.generate(self)
> end
> end
>
> Array.prepend(FixJson)
> Hash.prepend(FixJson)
>
> And that seems to work.
>
> Before, for a dataset .to_json I was getting:
>
>
> "[{\"json\":\"{\\\"id\\\":1,\\\"name\\\":\\\"sdfasdf\\\",\\\"template\\\":null,\\\"cumulative\\\":true,\\\"required_weekly\\\":true,\\\"in_house\\\":false,\\\"created_at\\\":\\\"2020-03-20T19:46:54.933-04:00\\\",\\\"updated_at\\\":\\\"2020-03-20T19:46:54.933-04:00\\\"}\
> "},{\
> "json\":\"{\\\"id\\\":2,\\\"name\\\":\\\"sdafsd\\\",\\\"template\\\":null,\\\"cumulative\\\":true,\\\"required_weekly\\\":true,\\\"in_house\\\":false,\\\"created_at\\\":\\\"2020-03-20T19:46:54.933-04:00\\\",\\\"updated_at\\\":\\\"2020-03-20T19:46:54.933-04:00\\\"}\"},{\
> "json\
> ":\"{\\\"id\\\":5,\\\"name\\\":\\\"sadfsaedf\\\",\\\"template\\\":null,\\\"cumulative\\\":true,\\\"required_weekly\\\":true,\\\"in_house\\\":true,\\\"created_at\\\":\\\"2020-03-20T19:46:54.933-04:00\\\",\\\"updated_at\\\":\\\"2020-03-20T19:46:54.933-04:00\\\"}\
> "},{\
> "json\":\"{\\\"id\\\":6,\\\"name\\\":\\\"sdfsad\\\",\\\"template\\\":null,\\\"cumulative\\\":true,\\\"required_weekly\\\":true,\\\"in_house\\\":true,\\\"created_at\\\":\\\"2020-03-20T19:46:54.933-04:00\\\",\\\"updated_at\\\":\\\"2020-03-20T19:46:54.933-04:00\\\"}\"}
>
> ,{\"json\":\"{\\\"id\\\":4,\\\"name\\\":\\\"sdafasd\\\",\\\"template\\\":null,\\\"cumulative\\\":true,\\\"required_weekly\\\":true,\\\"in_house\\\":true,\\\"created_at\\\":\\\"2020-03-20T19:46:54.933-04:00\\\",\\\"updated_at\\\":\\\"2020-03-20T19:46:54.933-04:00\\\"}\"},{\
> "json\
> ":\"{\\\"id\\\":3,\\\"name\\\":\\\"sadfsd\\\",\\\"template\\\":null,\\\"cumulative\\\":true,\\\"required_weekly\\\":true,\\\"in_house\\\":false,\\\"created_at\\\":\\\"2020-03-20T19:46:54.933-04:00\\\",\\\"updated_at\\\":\\\"2020-03-20T19:46:54.933-04:00\\\"}\"},{\"json
>
> \":\"{\\\"id\\\":7,\\\"name\\\":\\\"sadfsda\\\",\\\"template\\\":null,\\\"cumulative\\\":false,\\\"required_weekly\\\":true,\\\"in_house\\\":false,\\\"created_at\\\":\\\"2020-03-20T19:46:54.933-04:00\\\",\\\"updated_at\\\":\\\"2020-03-20T19:46:54.933-04:00\\\"}\"}]"
>
>
> Now, I get:
> "[{\"id\":1,\"name\":\"sadfasd\",\"template\":null,\"cumulative\":true,\"required_weekly\":true,\"in_house\":false,\"created_at\":\"2020-03-20
>
> 19:46:54-0400\",\"updated_at\":\"2020-03-20 19:46:54
> -0400\"},{\"id\":2,\"name\":\"asdfdas\",\"template\":null,\"cumulative\":true,\"required_weekly\":true,\"in_house\":false,\"created_at\":\"2020-03-20
>
> 19:46:54 -0400\",\"updated_at\":\"2020-03-20 19:46:54
> -0400\"},{\"id\":5,\"name\":\"sadfasdf\",\"template\":null,\"cumulative\":true,\"required_weekly\":true,\"in_house\":true,\"created_at\":\"2020-03-20
>
> 19:46:54 -0400\",\"updated_at\":\"2020-03-20 19:46:54
> -0400\"},{\"id\":6,\"name\":\"sadfds\",\"template\":null,\"cumulative\":true,\"required_weekly\":true,\"in_house\":true,\"created_at\":\"2020-03-20
>
> 19:46:54 -0400\",\"updated_at\":\"2020-03-20 19:46:54
> -0400\"},{\"id\":4,\"name\":\"sadfsdf\",\"template\":null,\"cumulative\":true,\"required_weekly\":true,\"in_house\":true,\"created_at\":\"2020-03-20
>
> 19:46:54 -0400\",\"updated_at\":\"2020-03-20 19:46:54
> -0400\"},{\"id\":3,\"name\":\"asdfsd\",\"template\":null,\"cumulative\":true,\"required_weekly\":true,\"in_house\":false,\"created_at\":\"2020-03-20
>
> 19:46:54 -0400\",\"updated_at\":\"2020-03-20 19:46:54
> -0400\"},{\"id\":7,\"name\":\"asdfads\",\"template\":null,\"cumulative\":false,\"required_weekly\":true,\"in_house\":false,\"created_at\":\"2020-03-20
>
> 19:46:54 -0400\",\"updated_at\":\"2020-03-20 19:46:54 -0400\"}]"
>
> (with obfuscated names :)
>
> Took me some time to figure out. Should the docs be updated?
>
I suppose since they give a code example that doesn't work anymore, they
should. I'll hold my nose and push a commit for that.
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/e6ef57ad-5683-40d1-a4f0-e01d2aad6dd4%40googlegroups.com.