fix json encoding, and use less casts