private static string EncodeKeyValuePair(KeyValuePair <string, object> keyValuePair) { string key = keyValuePair.Key; object value = keyValuePair.Value; if (string.IsNullOrEmpty(key)) { throw new ArgumentNullException("keyValuePair", "Key cannot be null/empty"); } if (!SpecialCharacters.IsValidKey(key)) { throw new ArgumentException("Invalid key in KeyValuePair", key); } string arg; string arg2; if (value is DateTime) { arg = "Dt"; arg2 = ((DateTime)value).ToString("yyyy-MM-ddTHH\\:mm\\:ss.fffZ", DateTimeFormatInfo.InvariantInfo); } else if (value is int) { arg = "I32"; arg2 = ((int)value).ToString(NumberFormatInfo.InvariantInfo); } else if (value is string) { arg = "S"; arg2 = (string)value; } else if (value is Guid) { arg = "S"; arg2 = ((Guid)value).ToString(); } else if (value is float) { arg = "F"; arg2 = ((float)value).ToString(NumberFormatInfo.InvariantInfo); } else if (value is double) { arg = "Dbl"; arg2 = ((double)value).ToString(NumberFormatInfo.InvariantInfo); } else { arg = "S"; arg2 = "[LogRowFormatter:EncodeKeyValuePair Invalid Data Type for value]"; } return(string.Format("{0}:{1}={2}", arg, keyValuePair.Key, arg2)); }
public static object DecodeKeyValuePair(byte[] src, int offset, int count) { string nextToken = CsvDecoder.GetNextToken(SpecialCharacters.ColonByte, src, ref offset, ref count); if (string.IsNullOrEmpty(nextToken)) { return(null); } string nextToken2 = CsvDecoder.GetNextToken(SpecialCharacters.EqualsByte, src, ref offset, ref count); if (string.IsNullOrEmpty(nextToken2) || !SpecialCharacters.IsValidKey(nextToken2)) { return(null); } object obj; if (nextToken.Equals("Dt", StringComparison.OrdinalIgnoreCase)) { obj = CsvDecoder.DecodeDateTime(src, offset, count); } else if (nextToken.Equals("I32", StringComparison.OrdinalIgnoreCase)) { obj = CsvDecoder.DecodeInt32(src, offset, count); } else if (nextToken.Equals("S", StringComparison.OrdinalIgnoreCase)) { obj = CsvDecoder.DecodeString(src, offset, count); } else if (nextToken.Equals("F", StringComparison.OrdinalIgnoreCase)) { obj = CsvDecoder.DecodeFloat(src, offset, count); } else { if (!nextToken.Equals("Dbl", StringComparison.OrdinalIgnoreCase)) { return(null); } obj = CsvDecoder.DecodeDouble(src, offset, count); } if (obj == null) { return(null); } return(new KeyValuePair <string, object>(nextToken2, obj)); }