| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Xml; |
| | 6 | | using System.Xml.Linq; |
| | 7 | | using Azure.Messaging.ServiceBus.Diagnostics; |
| | 8 | |
|
| | 9 | | namespace Azure.Messaging.ServiceBus.Management |
| | 10 | | { |
| | 11 | | internal class XmlObjectConvertor |
| | 12 | | { |
| | 13 | | internal static object ParseValueObject(XElement element) |
| | 14 | | { |
| 112 | 15 | | var serializedPrefix = element.GetPrefixOfNamespace(XNamespace.Get(ManagementClientConstants.SerializationNa |
| 112 | 16 | | var type = element.Attribute(XName.Get("type", ManagementClientConstants.XmlSchemaInstanceNamespace)).Value; |
| | 17 | |
|
| 112 | 18 | | if (!string.IsNullOrEmpty(serializedPrefix)) |
| | 19 | | { |
| 12 | 20 | | if (type.Substring(serializedPrefix.Length + 1) == "duration") |
| | 21 | | { |
| 12 | 22 | | return XmlConvert.ToTimeSpan(element.Value); |
| | 23 | | } |
| | 24 | | } |
| | 25 | |
|
| 100 | 26 | | var prefix = element.GetPrefixOfNamespace(XNamespace.Get(ManagementClientConstants.XmlSchemaNamespace)); |
| 100 | 27 | | if (string.IsNullOrWhiteSpace(prefix)) |
| | 28 | | { |
| 0 | 29 | | return element.Value; |
| | 30 | | } |
| | 31 | |
|
| 100 | 32 | | switch (type.Substring(prefix.Length + 1)) |
| | 33 | | { |
| | 34 | | case "string": |
| 40 | 35 | | return element.Value; |
| | 36 | | case "int": |
| 20 | 37 | | return XmlConvert.ToInt32(element.Value); |
| | 38 | | case "long": |
| 16 | 39 | | return XmlConvert.ToInt64(element.Value); |
| | 40 | | case "boolean": |
| 4 | 41 | | return XmlConvert.ToBoolean(element.Value); |
| | 42 | | case "double": |
| 4 | 43 | | return XmlConvert.ToDouble(element.Value); |
| | 44 | | case "dateTime": |
| 16 | 45 | | return XmlConvert.ToDateTime(element.Value, XmlDateTimeSerializationMode.Utc); |
| | 46 | | case "duration": |
| 0 | 47 | | return XmlConvert.ToTimeSpan(element.Value); |
| | 48 | | default: |
| 0 | 49 | | ServiceBusEventSource.Log.ManagementSerializationException( |
| 0 | 50 | | $"{nameof(XmlObjectConvertor)}_{nameof(ParseValueObject)}", |
| 0 | 51 | | element.ToString()); |
| 0 | 52 | | return element.Value; |
| | 53 | | } |
| | 54 | | } |
| | 55 | |
|
| | 56 | | internal static XElement SerializeObject(object value) |
| | 57 | | { |
| 64 | 58 | | var prefix = "l28"; |
| 64 | 59 | | string type = prefix + ':'; |
| 64 | 60 | | if (value is string) |
| | 61 | | { |
| 24 | 62 | | type += "string"; |
| | 63 | | } |
| 40 | 64 | | else if (value is int) |
| | 65 | | { |
| 12 | 66 | | type += "int"; |
| | 67 | | } |
| 28 | 68 | | else if (value is long) |
| | 69 | | { |
| 8 | 70 | | type += "long"; |
| | 71 | | } |
| 20 | 72 | | else if (value is bool) |
| | 73 | | { |
| 4 | 74 | | type += "boolean"; |
| | 75 | | } |
| 16 | 76 | | else if (value is double) |
| | 77 | | { |
| 4 | 78 | | type += "double"; |
| | 79 | | } |
| 12 | 80 | | else if (value is DateTime) |
| | 81 | | { |
| 8 | 82 | | type += "dateTime"; |
| | 83 | | } |
| 4 | 84 | | else if (value is TimeSpan) |
| | 85 | | { |
| 4 | 86 | | type += "duration"; |
| | 87 | | } |
| | 88 | | else |
| | 89 | | { |
| 0 | 90 | | var unknownType = value.GetType().Name; |
| 0 | 91 | | ServiceBusEventSource.Log.ManagementSerializationException( |
| 0 | 92 | | $"{nameof(XmlObjectConvertor)}_{nameof(SerializeObject)}", |
| 0 | 93 | | unknownType); |
| | 94 | |
|
| 0 | 95 | | throw new ServiceBusException(false, "Object is not of supported type: " + unknownType + ". " + |
| 0 | 96 | | "Only following types are supported through HTTP: string,int,long,bool,double,DateTime"); |
| | 97 | | } |
| | 98 | |
|
| 64 | 99 | | var element = new XElement(XName.Get("Value", ManagementClientConstants.ServiceBusNamespace), |
| 64 | 100 | | new XAttribute(XName.Get("type", ManagementClientConstants.XmlSchemaInstanceNamespace), type), |
| 64 | 101 | | new XAttribute(XNamespace.Xmlns + prefix, ManagementClientConstants.XmlSchemaNamespace), |
| 64 | 102 | | value); |
| | 103 | |
|
| 64 | 104 | | return element; |
| | 105 | | } |
| | 106 | | } |
| | 107 | | } |