Speed up FunctionAsJSON.

Instead of creating many temporary strings, the code now formats the complete
object into a StringBuilder.