Revert "Simplify code in AS json encoder"

This reverts commit 046e27a7338f2961c10e9d133e0e2229b51c2ba8.
Check https://github.com/rails/rails/pull/8815#issuecomment-14026122 for
details.