Merge pull request #12792 from chancancode/move_json_requires

Move the JSON extension require statements to the right place.
only ask for these ivars if the target responds to them
Added some failing tests for as_json not being called recursively

These are currently failing:

   class Custom
     def initialize(serialized)
       @serialized = serialized
     end

     def as_json(options)
       @serialized
     end
   end

   Custom.new(:a).to_json
   Custom.new([ :foo, "bar" ])
   Custom.new(Custom.new("hello"))

They are failing with undefined method `encode_json' because the
encoder is not calling `as_json` recursively.