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); } }