Factor _serialize_key_value