/// <summary> /// Serializes the specified entity in Json Format. /// </summary> /// <param name="entity">The entity.</param> /// <returns> /// Returns the serialize entity in string format. /// </returns> public string Serialize(object entity) { string data = string.Empty; JsonSerializerSettings settings = new JsonSerializerSettings(); settings.Converters.Add(new ObjectToEnumConverter()); settings.Converters.Add(new IntuitConverter()); settings.NullValueHandling = NullValueHandling.Ignore; settings.MissingMemberHandling = MissingMemberHandling.Error; settings.DateFormatHandling = DateFormatHandling.IsoDateFormat; settings.DateTimeZoneHandling = DateTimeZoneHandling.Utc; settings.DateFormatString = "yyyy-MM-ddTHH:mm:ssK"; try { data = JsonConvert.SerializeObject(entity, settings); } catch (Exception ex) { SerializationException serializationException = new SerializationException(ex.Message, ex); this.IDSLogger.Log(TraceLevel.Error, serializationException.ToString()); IdsExceptionManager.HandleException(serializationException); } data = data.Replace("T00:00:00Z", ""); data = data.Replace("T00:00:00", ""); return(data); }
/// <summary> /// DeSerializes the specified action entity type in Json Format. /// </summary> /// <typeparam name="T">The type to be serialize to</typeparam> /// <param name="message">The message.</param> /// <returns> /// Returns the de serialized object. /// </returns> public object Deserialize <T>(string message) { object deserializedObject = null; // Initialize serialize for object JsonSerializerSettings settings = new JsonSerializerSettings(); settings.Converters.Add(new ObjectToEnumConverter()); settings.Converters.Add(new IntuitConverter()); settings.NullValueHandling = NullValueHandling.Ignore; settings.MissingMemberHandling = MissingMemberHandling.Ignore; try { // de serialization of message. /* JObject o = JObject.Parse(message); * string key = o.Properties().Select(p => p.Name).Single(); * string entityString = o[key].ToString();*/ deserializedObject = JsonConvert.DeserializeObject <T>(message, settings); } catch (SystemException ex) { SerializationException serializationException = new SerializationException(ex.Message, ex); this.IDSLogger.Log(TraceLevel.Error, serializationException.ToString()); IdsExceptionManager.HandleException(serializationException); } return(deserializedObject); }
/// <summary> /// DeSerializes the specified action entity type. /// </summary> /// <typeparam name="T">The type to be serialize to</typeparam> /// <param name="message">The message.</param> /// <returns> /// Returns the de serialized object. /// </returns> public object Deserialize <T>(string message) { object deserializedObject = null; // Initialize serialize for object XmlSerializer serializer = new XmlSerializer(typeof(T)); try { using (TextReader reader = new StringReader(message)) { // de serialization of message. deserializedObject = serializer.Deserialize(reader); } } catch (SystemException ex) { SerializationException serializationException = new SerializationException(ex.Message, ex); this.IDSLogger.Log(TraceLevel.Error, serializationException.ToString()); IdsExceptionManager.HandleException(serializationException); } return(deserializedObject); }
/// <summary> /// Serializes the specified entity. /// </summary> /// <param name="entity">The entity.</param> /// <returns> /// Returns the serialize entity in string format. /// </returns> public string Serialize(object entity) { string data = string.Empty; try { UTF8Encoding encoder = new UTF8Encoding(); using (MemoryStream memoryStream = new MemoryStream()) { XmlSerializer xmlSerializer = new XmlSerializer(entity.GetType()); xmlSerializer.Serialize(memoryStream, entity); data = encoder.GetString(memoryStream.ToArray()); } } catch (SystemException ex) { SerializationException serializationException = new SerializationException(ex.Message, ex); this.IDSLogger.Log(TraceLevel.Error, serializationException.ToString()); IdsExceptionManager.HandleException(serializationException); } data = data.Replace("T00:00:00Z", ""); data = data.Replace("T00:00:00", ""); return(data); }
protected void TestNullCannotBeConvertedToValueType() { SearchServiceClient serviceClient = Data.GetSearchServiceClient(); var index = new Index() { Name = SearchTestUtilities.GenerateName(), Fields = FieldBuilder.BuildForType <ModelWithNullableValueTypes>() }; serviceClient.Indexes.Create(index); SearchIndexClient indexClient = Data.GetSearchIndexClient(index.Name); var batch = IndexBatch.Upload(new[] { new ModelWithNullableValueTypes() { Key = "123", IntValue = null, Bucket = new Bucket() { BucketName = "Z", Count = 1 } }, new ModelWithNullableValueTypes() { Key = "456", IntValue = 5, Bucket = null } }); indexClient.Documents.Index(batch); SearchTestUtilities.WaitForIndexing(); SerializationException e = Assert.Throws <SerializationException>(() => indexClient.Documents.Search <ModelWithValueTypes>("123")); Assert.Contains("Error converting value {null} to type 'System.Int32'. Path 'IntValue'.", e.ToString()); e = Assert.Throws <SerializationException>(() => indexClient.Documents.Search <ModelWithValueTypes>("456")); Assert.Contains("Error converting value {null} to type 'Microsoft.Azure.Search.Tests.SearchTests+Bucket'. Path 'Bucket'.", e.ToString()); }
protected void TestNullCannotBeConvertedToValueType() { SearchServiceClient serviceClient = Data.GetSearchServiceClient(); Index index = new Index() { Name = SearchTestUtilities.GenerateName(), Fields = new[] { new Field("Key", DataType.String) { IsKey = true }, new Field("IntValue", DataType.Int32) } }; serviceClient.Indexes.Create(index); SearchIndexClient indexClient = Data.GetSearchIndexClient(index.Name); var doc = new ModelWithNullableInt() { Key = "123", IntValue = null }; var batch = IndexBatch.Upload(new[] { doc }); indexClient.Documents.Index(batch); SearchTestUtilities.WaitForIndexing(); SerializationException e = Assert.Throws <SerializationException>(() => indexClient.Documents.Search <ModelWithInt>("*")); Assert.Contains("Error converting value {null} to type 'System.Int32'. Path 'IntValue'.", e.ToString()); }