I should add that if you do this and w is < 0 when you encode (which it shouldn't be, but you never know), you need to negate x, y, and z.  Negating every element of a quaternion produces the same rotation [(x, y, z, w) is the same rotation as (-x, -y, -z, -w)].