Detect, then decode and encode JSON values only to avoid bugs