public object FromBlittable(Type type, BlittableJsonReaderObject json, string id) { try { var defaultValue = InMemoryDocumentSessionOperations.GetDefaultValue(type); var entity = defaultValue; var documentTypeAsString = Conventions.Conventions.GetClrType(id, json); if (documentTypeAsString != null) { var documentType = Type.GetType(documentTypeAsString); if (documentType != null && type.IsAssignableFrom(documentType)) { entity = Conventions.DeserializeEntityFromBlittable(documentType, json); } } if (Equals(entity, defaultValue)) { entity = Conventions.DeserializeEntityFromBlittable(type, json); } return(entity); } catch (Exception ex) { throw new InvalidOperationException($"Could not convert document {id} to entity of type {type}", ex); } }
/// <summary> /// Converts a BlittableJsonReaderObject to an entity without a session. /// </summary> /// <param name="entityType"></param> /// <param name="id">The id.</param> /// <param name="document">The document found.</param> /// <param name="documentConvention">The documentConvention.</param> /// <returns>The converted entity</returns> public static object ConvertToEntity(Type entityType, string id, BlittableJsonReaderObject document, DocumentConvention documentConvention) { try { var defaultValue = InMemoryDocumentSessionOperations.GetDefaultValue(entityType); var entity = defaultValue; var documentType = documentConvention.GetClrType(id, document); if (documentType != null) { var type = Type.GetType(documentType); if (type != null) { entity = documentConvention.DeserializeEntityFromBlittable(type, document); } } if (Equals(entity, defaultValue)) { entity = documentConvention.DeserializeEntityFromBlittable(entityType, document); } return(entity); } catch (Exception ex) { throw new InvalidOperationException($"Could not convert document {id} to entity of type {entityType}", ex); } }
/// <summary> /// Converts a BlittableJsonReaderObject to an entity. /// </summary> /// <param name="entityType"></param> /// <param name="id">The id.</param> /// <param name="document">The document found.</param> /// <returns>The converted entity</returns> public object ConvertToEntity(Type entityType, string id, BlittableJsonReaderObject document) { //TODO - Add RegisterMissingProperties ??? try { var defaultValue = InMemoryDocumentSessionOperations.GetDefaultValue(entityType); var entity = defaultValue; var documentType = _session.Conventions.GetClrType(id, document); if (documentType != null) { var type = Type.GetType(documentType); if (type != null) { entity = _session.Conventions.DeserializeEntityFromBlittable(type, document); } } if (Equals(entity, defaultValue)) { entity = _session.Conventions.DeserializeEntityFromBlittable(entityType, document); } _session.GenerateEntityIdOnTheClient.TrySetIdentity(entity, id); return(entity); } catch (Exception ex) { throw new InvalidOperationException($"Could not convert document {id} to entity of type {entityType}", ex); } }
public object FromBlittable(Type type, ref BlittableJsonReaderObject json, string id, bool trackEntity) { try { if (type == typeof(BlittableJsonReaderObject)) { return(json); } _session.OnBeforeConversionToEntityInvoke(id, type, ref json); var defaultValue = InMemoryDocumentSessionOperations.GetDefaultValue(type); var entity = defaultValue; ExtensionDataSetter dataSetter = null; if (trackEntity) { dataSetter = RegisterMissingProperties; } using (DefaultRavenContractResolver.RegisterExtensionDataSetter(dataSetter)) { var documentTypeAsString = _session.Conventions.GetClrType(id, json); if (documentTypeAsString != null) { var documentType = Type.GetType(documentTypeAsString); if (documentType != null && type.IsAssignableFrom(documentType)) { entity = _session.Conventions.Serialization.DeserializeEntityFromBlittable(documentType, json); } } if (Equals(entity, defaultValue)) { entity = _session.Conventions.Serialization.DeserializeEntityFromBlittable(type, json); } } if (id != null) { _session.GenerateEntityIdOnTheClient.TrySetIdentity(entity, id); } _session.OnAfterConversionToEntityInvoke(id, json, entity); return(entity); } catch (Exception ex) { throw new InvalidOperationException($"Could not convert document {id} to entity of type {type}", ex); } }
public object FromBlittable(Type type, ref BlittableJsonReaderObject json, string id, bool trackEntity) { try { if (type == typeof(BlittableJsonReaderObject)) { return(json); } _session.OnBeforeConversionToEntityInvoke(id, type, ref json); var defaultValue = InMemoryDocumentSessionOperations.GetDefaultValue(type); var entity = defaultValue; ExtensionDataSetter dataSetter = null; if (trackEntity) { dataSetter = RegisterMissingProperties; } using (DefaultRavenContractResolver.RegisterExtensionDataSetter(dataSetter)) { var documentTypeAsString = _session.Conventions.GetClrType(id, json); if (documentTypeAsString != null) { var documentType = Type.GetType(documentTypeAsString); if (documentType != null && type.IsAssignableFrom(documentType)) { entity = _session.Conventions.Serialization.DeserializeEntityFromBlittable(documentType, json); } } if (Equals(entity, defaultValue)) { entity = _session.Conventions.Serialization.DeserializeEntityFromBlittable(type, json); } } if (id != null) { _session.GenerateEntityIdOnTheClient.TrySetIdentity(entity, id); } _session.OnAfterConversionToEntityInvoke(id, json, entity); return(entity); } catch (Exception ex) { string jsonAsString = TryReadBlittableAsString(json); throw new InvalidOperationException($"Could not convert document {id} to entity of type {type}. Json: {jsonAsString}", ex); } string TryReadBlittableAsString(BlittableJsonReaderObject jsonToRead) { var jsString = string.Empty; using (var memoryStream = new MemoryStream()) { try { jsonToRead.WriteJsonToAsync(memoryStream).AsTask().ConfigureAwait(false).GetAwaiter().GetResult(); } catch { // ignore } memoryStream.Position = 0; try { using (var sr = new StreamReader(memoryStream)) { jsString = sr.ReadToEnd(); } } catch { // ignore } } return(jsString); } }