private static string EncodeBallotChoices(object choices, int logId) { // offices separated by $ // candidates separated by | // writeIn indicated by * string result = null; if (choices is Dictionary <string, object> officeDictionary) { var offices = new List <string> { logId.ToString(CultureInfo.InvariantCulture) }; foreach (var officeKey in officeDictionary.Keys) { if (!(officeDictionary[officeKey] is Dictionary <string, object> candidateDictionary)) // it's a ballot measure { var key = Referendums.GetIdByReferendumKey(officeKey); var val = officeDictionary[officeKey] as string; // > indicates ballot measure offices.Add($"{key}|>{val}"); } else { var keys = new List <string> { Offices.GetId(officeKey).ToString() }; foreach (var politicianKey in candidateDictionary.Keys) { if (candidateDictionary[politicianKey] is int) { // politician key keys.Add(Politicians.GetId(politicianKey).ToString()); } else { if (candidateDictionary[politicianKey] is string writeIn) { writeIn = Uri.EscapeUriString(Regex.Replace(writeIn, "[$|]", "-")); keys.Add($"*{writeIn}"); } } } offices.Add(Join("|", keys)); } }