Fixed Object#as_json and Struct#as_json with options

These methods now takes the same options as Hash#as_json, for example:

    struct = Struct.new(:foo, :bar).new
    struct.foo = "hello"
    struct.bar = "world"
    json = struct.as_json(only: [:foo]) # => {foo: "hello"}

This is extracted from PR #11728 from @sergiocampama, see also the
discussion in #11460.

/cc @jeremy