Backport string field encode fixes from 2.8.x

See #120.