`as_json` correctly detecting circular references

The way `ActiveSupport::JSON::Encoding::Encoder` was setup didn't
account for certain cases for circular reference detection, besides
having incorrect arguments for certain methods (rails/rails#11460). The
way the encoder works has been rewritten to account for this cases and
fix the incorrect arguments.

To fix the issues described in rails/rails#11460, `as_json` had to be
standarized across Rails to return a JSON Ready object, which only
contains primitives that can be directly mapped to JSON primitives
(`Array`, `Hash`, `Numeric`, `String`, `TrueClass`,`FalseClass`,
`NilClass`). This meant rewriting some tests that failed to comply with
this requirement, among other minor changes in `ActiveModel` and
`ActionPack`.

Fixes rails/rails#11460