/// <summary> /// Gets the json object. /// </summary> /// <param name="dictionaryObject">The dictionary object.</param> /// <param name="service">The service.</param> /// <returns></returns> private JsonObject GetJsonObject(object dictionaryObject, ExchangeService service) { UserConfigurationDictionaryObjectType dictionaryObjectType = UserConfigurationDictionaryObjectType.String; string[] valueAsStringArray = null; if (dictionaryObject == null) { return(null); } if (dictionaryObject is string[]) { dictionaryObjectType = UserConfigurationDictionaryObjectType.StringArray; valueAsStringArray = dictionaryObject as string[]; } else if (dictionaryObject is byte[]) { dictionaryObjectType = UserConfigurationDictionaryObjectType.ByteArray; valueAsStringArray = new string[] { Convert.ToBase64String(dictionaryObject as byte[]) }; } else { valueAsStringArray = new string[1]; GetTypeCode(service, dictionaryObject, ref dictionaryObjectType, ref valueAsStringArray[0]); } JsonObject jsonDictionaryObject = new JsonObject(); jsonDictionaryObject.Add(XmlElementNames.Type, dictionaryObjectType); jsonDictionaryObject.Add(XmlElementNames.Value, valueAsStringArray); return(jsonDictionaryObject); }
/// <summary> /// Writes a dictionary Object's value to Xml. /// </summary> /// <param name="writer">The writer.</param> /// <param name="dictionaryObject">The dictionary object to write.</param> /* private */ void WriteObjectValueToXml(EwsServiceXmlWriter writer, object dictionaryObject) { EwsUtilities.Assert( writer != null, "UserConfigurationDictionary.WriteObjectValueToXml", "writer is null"); EwsUtilities.Assert( dictionaryObject != null, "UserConfigurationDictionary.WriteObjectValueToXml", "dictionaryObject is null"); // This logic is based on Microsoft.Exchange.Services.Core.GetUserConfiguration.ConstructDictionaryObject(). // // Object values are either: // . an array of strings // . a single value // // Single values can be: // . base64 String (from a byte array) // . datetime, boolean, byte, short, int, long, string, ushort, unint, ulong // // First check for a String array string[] dictionaryObjectAsStringArray = dictionaryObject as string[]; if (dictionaryObjectAsStringArray != null) { this.WriteEntryTypeToXml(writer, UserConfigurationDictionaryObjectType.StringArray); for (String arrayElement in dictionaryObjectAsStringArray) { this.WriteEntryValueToXml(writer, arrayElement); } } else { // if not a String array, all other object values are returned as a single element UserConfigurationDictionaryObjectType dictionaryObjectType = UserConfigurationDictionaryObjectType.String; String valueAsString = null; Uint8List dictionaryObjectAsByteArray = dictionaryObject as Uint8List; if (dictionaryObjectAsByteArray != null) { // Convert byte array to base64 string dictionaryObjectType = UserConfigurationDictionaryObjectType.ByteArray; valueAsString = Convert.ToBase64String(dictionaryObjectAsByteArray); } else { GetTypeCode(writer.Service, dictionaryObject, ref dictionaryObjectType, ref valueAsString); } this.WriteEntryTypeToXml(writer, dictionaryObjectType); this.WriteEntryValueToXml(writer, valueAsString); } }
/// <summary> /// Extracts a dictionary object (key or entry value) from the specified reader. /// </summary> /// <param name="reader">The reader.</param> /// <returns>Dictionary object.</returns> /* private */ object GetDictionaryObject(EwsServiceXmlReader reader) { EwsUtilities.Assert( reader != null, "UserConfigurationDictionary.LoadFromXml", "reader is null"); UserConfigurationDictionaryObjectType type = this.GetObjectType(reader); List <string> values = this.GetObjectValue(reader, type); return(this.ConstructObject(type, values, reader.Service)); }
/// <summary> /// Gets the dictionary object. /// </summary> /// <param name="jsonObject">The json object.</param> /// <param name="service">The service.</param> /// <returns></returns> private object GetDictionaryObject(JsonObject jsonObject, ExchangeService service) { if (jsonObject == null) { return(null); } UserConfigurationDictionaryObjectType type = GetObjectType(jsonObject.ReadAsString(XmlElementNames.Type)); List <string> values = this.GetObjectValue(jsonObject.ReadAsArray(XmlElementNames.Value)); return(this.ConstructObject(type, values, service)); }
/// <summary> /// Extracts a dictionary object (key or entry value) as a String list from the /// specified reader. /// </summary> /// <param name="reader">The reader.</param> /// <param name="type">The object type.</param> /// <returns>String list representing a dictionary object.</returns> /* private */ List <string> GetObjectValue(EwsServiceXmlReader reader, UserConfigurationDictionaryObjectType type) { EwsUtilities.Assert( reader != null, "UserConfigurationDictionary.LoadFromXml", "reader is null"); List <string> values = new List <string>(); reader.ReadStartElement(this.Namespace, XmlElementNames.Value); do { String value = null; if (reader.IsEmptyElement) { // Only String types can be represented with empty values. switch (type) { case UserConfigurationDictionaryObjectType.String: case UserConfigurationDictionaryObjectType.StringArray: value = ""; break; default: EwsUtilities.Assert( false, "UserConfigurationDictionary.GetObjectValue", "Empty element passed for type: " + type.ToString()); break; } } else { value = reader.ReadElementValue(); } values.Add(value); reader.Read(); // Position at next element or DictionaryKey/DictionaryValue end element }while (reader.IsStartElement(this.Namespace, XmlElementNames.Value)); return(values); }
/// <summary> /// Constructs a dictionary object (key or entry value) from the specified type and String list. /// </summary> /// <param name="type">Object type to construct.</param> /// <param name="value">Value of the dictionary object as a String list</param> /// <param name="service">The service.</param> /// <returns>Dictionary object.</returns> /* private */ object ConstructObject( UserConfigurationDictionaryObjectType type, List <string> value, ExchangeService service) { EwsUtilities.Assert( value != null, "UserConfigurationDictionary.ConstructObject", "value is null"); EwsUtilities.Assert( (value.Count == 1 || type == UserConfigurationDictionaryObjectType.StringArray), "UserConfigurationDictionary.ConstructObject", "value is array but type is not StringArray"); object dictionaryObject = null; switch (type) { case UserConfigurationDictionaryObjectType.Boolean: dictionaryObject = bool.Parse(value[0]); break; case UserConfigurationDictionaryObjectType.Byte: dictionaryObject = byte.Parse(value[0]); break; case UserConfigurationDictionaryObjectType.ByteArray: dictionaryObject = Convert.FromBase64String(value[0]); break; case UserConfigurationDictionaryObjectType.DateTime: DateTime?dateTime = service.ConvertUniversalDateTimeStringToLocalDateTime(value[0]); if (dateTime.HasValue) { dictionaryObject = dateTime.Value; } else { EwsUtilities.Assert( false, "UserConfigurationDictionary.ConstructObject", "DateTime is null"); } break; case UserConfigurationDictionaryObjectType.Integer32: dictionaryObject = int.Parse(value[0]); break; case UserConfigurationDictionaryObjectType.Integer64: dictionaryObject = long.Parse(value[0]); break; case UserConfigurationDictionaryObjectType.String: dictionaryObject = value[0]; break; case UserConfigurationDictionaryObjectType.StringArray: dictionaryObject = value.ToArray(); break; case UserConfigurationDictionaryObjectType.UnsignedInteger32: dictionaryObject = uint.Parse(value[0]); break; case UserConfigurationDictionaryObjectType.UnsignedInteger64: dictionaryObject = ulong.Parse(value[0]); break; default: EwsUtilities.Assert( false, "UserConfigurationDictionary.ConstructObject", "Type not recognized: " + type.ToString()); break; } return(dictionaryObject); }
/// <summary> /// Writes a dictionary entry type to Xml. /// </summary> /// <param name="writer">The writer.</param> /// <param name="dictionaryObjectType">Type to write.</param> /* private */ void WriteEntryTypeToXml(EwsServiceXmlWriter writer, UserConfigurationDictionaryObjectType dictionaryObjectType) { writer.WriteStartElement(XmlNamespace.Types, XmlElementNames.Type); writer.WriteValue(dictionaryObjectType.ToString(), XmlElementNames.Type); writer.WriteEndElement(); }
/// <summary> /// Gets the type code. /// </summary> /// <param name="service">The service.</param> /// <param name="dictionaryObject">The dictionary object.</param> /// <param name="dictionaryObjectType">Type of the dictionary object.</param> /// <param name="valueAsString">The value as string.</param> /* private */ static void GetTypeCode(ExchangeServiceBase service, object dictionaryObject, ref UserConfigurationDictionaryObjectType dictionaryObjectType, ref String valueAsString) { // Handle all other types by TypeCode switch (Type.GetTypeCode(dictionaryObject.GetType())) { case TypeCode.Boolean: dictionaryObjectType = UserConfigurationDictionaryObjectType.Boolean; valueAsString = EwsUtilities.BoolToXSBool((bool)dictionaryObject); break; case TypeCode.Byte: dictionaryObjectType = UserConfigurationDictionaryObjectType.Byte; valueAsString = ((byte)dictionaryObject).ToString(); break; case TypeCode.DateTime: dictionaryObjectType = UserConfigurationDictionaryObjectType.DateTime; valueAsString = service.ConvertDateTimeToUniversalDateTimeString((DateTime)dictionaryObject); break; case TypeCode.Int32: dictionaryObjectType = UserConfigurationDictionaryObjectType.Integer32; valueAsString = ((int)dictionaryObject).ToString(); break; case TypeCode.Int64: dictionaryObjectType = UserConfigurationDictionaryObjectType.Integer64; valueAsString = ((long)dictionaryObject).ToString(); break; case TypeCode.String: dictionaryObjectType = UserConfigurationDictionaryObjectType.String; valueAsString = (string)dictionaryObject; break; case TypeCode.UInt32: dictionaryObjectType = UserConfigurationDictionaryObjectType.UnsignedInteger32; valueAsString = ((uint)dictionaryObject).ToString(); break; case TypeCode.UInt64: dictionaryObjectType = UserConfigurationDictionaryObjectType.UnsignedInteger64; valueAsString = ((ulong)dictionaryObject).ToString(); break; default: EwsUtilities.Assert( false, "UserConfigurationDictionary.WriteObjectValueToXml", "Unsupported type: " + dictionaryObject.GetType().ToString()); break; } }
/// <summary> /// Constructs a dictionary object (key or entry value) from the specified type and string list. /// </summary> /// <param name="type">Object type to construct.</param> /// <param name="value">Value of the dictionary object as a string list</param> /// <param name="service">The service.</param> /// <returns>Dictionary object.</returns> private object ConstructObject( UserConfigurationDictionaryObjectType type, List<string> value, ExchangeService service) { EwsUtilities.Assert( value != null, "UserConfigurationDictionary.ConstructObject", "value is null"); EwsUtilities.Assert( (value.Count == 1 || type == UserConfigurationDictionaryObjectType.StringArray), "UserConfigurationDictionary.ConstructObject", "value is array but type is not StringArray"); object dictionaryObject = null; switch (type) { case UserConfigurationDictionaryObjectType.Boolean: dictionaryObject = bool.Parse(value[0]); break; case UserConfigurationDictionaryObjectType.Byte: dictionaryObject = byte.Parse(value[0]); break; case UserConfigurationDictionaryObjectType.ByteArray: dictionaryObject = Convert.FromBase64String(value[0]); break; case UserConfigurationDictionaryObjectType.DateTime: DateTime? dateTime = service.ConvertUniversalDateTimeStringToLocalDateTime(value[0]); if (dateTime.HasValue) { dictionaryObject = dateTime.Value; } else { EwsUtilities.Assert( false, "UserConfigurationDictionary.ConstructObject", "DateTime is null"); } break; case UserConfigurationDictionaryObjectType.Integer32: dictionaryObject = int.Parse(value[0]); break; case UserConfigurationDictionaryObjectType.Integer64: dictionaryObject = long.Parse(value[0]); break; case UserConfigurationDictionaryObjectType.String: dictionaryObject = value[0]; break; case UserConfigurationDictionaryObjectType.StringArray: dictionaryObject = value.ToArray(); break; case UserConfigurationDictionaryObjectType.UnsignedInteger32: dictionaryObject = uint.Parse(value[0]); break; case UserConfigurationDictionaryObjectType.UnsignedInteger64: dictionaryObject = ulong.Parse(value[0]); break; default: EwsUtilities.Assert( false, "UserConfigurationDictionary.ConstructObject", "Type not recognized: " + type.ToString()); break; } return dictionaryObject; }
/// <summary> /// Extracts a dictionary object (key or entry value) as a string list from the /// specified reader. /// </summary> /// <param name="reader">The reader.</param> /// <param name="type">The object type.</param> /// <returns>String list representing a dictionary object.</returns> private List<string> GetObjectValue(EwsServiceXmlReader reader, UserConfigurationDictionaryObjectType type) { EwsUtilities.Assert( reader != null, "UserConfigurationDictionary.LoadFromXml", "reader is null"); List<string> values = new List<string>(); reader.ReadStartElement(this.Namespace, XmlElementNames.Value); do { string value = null; if (reader.IsEmptyElement) { // Only string types can be represented with empty values. switch (type) { case UserConfigurationDictionaryObjectType.String: case UserConfigurationDictionaryObjectType.StringArray: value = string.Empty; break; default: EwsUtilities.Assert( false, "UserConfigurationDictionary.GetObjectValue", "Empty element passed for type: " + type.ToString()); break; } } else { value = reader.ReadElementValue(); } values.Add(value); reader.Read(); // Position at next element or DictionaryKey/DictionaryValue end element } while (reader.IsStartElement(this.Namespace, XmlElementNames.Value)); return values; }
/// <summary> /// Writes a dictionary entry type to Xml. /// </summary> /// <param name="writer">The writer.</param> /// <param name="dictionaryObjectType">Type to write.</param> private void WriteEntryTypeToXml(EwsServiceXmlWriter writer, UserConfigurationDictionaryObjectType dictionaryObjectType) { writer.WriteStartElement(XmlNamespace.Types, XmlElementNames.Type); writer.WriteValue(dictionaryObjectType.ToString(), XmlElementNames.Type); writer.WriteEndElement(); }
/// <summary> /// Gets the type code. /// </summary> /// <param name="service">The service.</param> /// <param name="dictionaryObject">The dictionary object.</param> /// <param name="dictionaryObjectType">Type of the dictionary object.</param> /// <param name="valueAsString">The value as string.</param> private static void GetTypeCode(ExchangeServiceBase service, object dictionaryObject, ref UserConfigurationDictionaryObjectType dictionaryObjectType, ref string valueAsString) { // Handle all other types by TypeCode switch (Type.GetTypeCode(dictionaryObject.GetType())) { case TypeCode.Boolean: dictionaryObjectType = UserConfigurationDictionaryObjectType.Boolean; valueAsString = EwsUtilities.BoolToXSBool((bool)dictionaryObject); break; case TypeCode.Byte: dictionaryObjectType = UserConfigurationDictionaryObjectType.Byte; valueAsString = ((byte)dictionaryObject).ToString(); break; case TypeCode.DateTime: dictionaryObjectType = UserConfigurationDictionaryObjectType.DateTime; valueAsString = service.ConvertDateTimeToUniversalDateTimeString((DateTime)dictionaryObject); break; case TypeCode.Int32: dictionaryObjectType = UserConfigurationDictionaryObjectType.Integer32; valueAsString = ((int)dictionaryObject).ToString(); break; case TypeCode.Int64: dictionaryObjectType = UserConfigurationDictionaryObjectType.Integer64; valueAsString = ((long)dictionaryObject).ToString(); break; case TypeCode.String: dictionaryObjectType = UserConfigurationDictionaryObjectType.String; valueAsString = (string)dictionaryObject; break; case TypeCode.UInt32: dictionaryObjectType = UserConfigurationDictionaryObjectType.UnsignedInteger32; valueAsString = ((uint)dictionaryObject).ToString(); break; case TypeCode.UInt64: dictionaryObjectType = UserConfigurationDictionaryObjectType.UnsignedInteger64; valueAsString = ((ulong)dictionaryObject).ToString(); break; default: EwsUtilities.Assert( false, "UserConfigurationDictionary.WriteObjectValueToXml", "Unsupported type: " + dictionaryObject.GetType().ToString()); break; } }
/// <summary> /// Gets the type code. /// </summary> /// <param name="service">The service.</param> /// <param name="dictionaryObject">The dictionary object.</param> /// <param name="dictionaryObjectType">Type of the dictionary object.</param> /// <param name="valueAsString">The value as string.</param> private static void GetTypeCode(ExchangeServiceBase service, object dictionaryObject, ref UserConfigurationDictionaryObjectType dictionaryObjectType, ref string valueAsString) { if (dictionaryObject is Boolean) { dictionaryObjectType = UserConfigurationDictionaryObjectType.Boolean; valueAsString = EwsUtilities.BoolToXSBool((bool)dictionaryObject); } else if (dictionaryObject is Byte) { dictionaryObjectType = UserConfigurationDictionaryObjectType.Byte; valueAsString = ((byte)dictionaryObject).ToString(); } else if (dictionaryObject is DateTime) { dictionaryObjectType = UserConfigurationDictionaryObjectType.DateTime; valueAsString = service.ConvertDateTimeToUniversalDateTimeString((DateTime)dictionaryObject); } else if (dictionaryObject is Int32) { dictionaryObjectType = UserConfigurationDictionaryObjectType.Integer32; valueAsString = ((int)dictionaryObject).ToString(); } else if (dictionaryObject is Int64) { dictionaryObjectType = UserConfigurationDictionaryObjectType.Integer64; valueAsString = ((long)dictionaryObject).ToString(); } else if (dictionaryObject is String) { dictionaryObjectType = UserConfigurationDictionaryObjectType.String; valueAsString = (string)dictionaryObject; } else if (dictionaryObject is UInt32) { dictionaryObjectType = UserConfigurationDictionaryObjectType.UnsignedInteger32; valueAsString = ((uint)dictionaryObject).ToString(); } else if (dictionaryObject is UInt64) { dictionaryObjectType = UserConfigurationDictionaryObjectType.UnsignedInteger64; valueAsString = ((ulong)dictionaryObject).ToString(); } else { EwsUtilities.Assert( false, "UserConfigurationDictionary.WriteObjectValueToXml", "Unsupported type: " + dictionaryObject.GetType().ToString()); } }