private static bool TryRemoveIdentityProperty(BlittableJsonReaderObject document, Type entityType, DocumentConventions conventions, bool isDynamicObject) { var identityProperty = conventions.GetIdentityProperty(entityType); if (identityProperty == null) { if (conventions.AddIdFieldToDynamicObjects && isDynamicObject) { if (document.Modifications == null) { document.Modifications = new DynamicJsonValue(document); } document.Modifications.Remove("Id"); return(true); } return(false); } if (document.Modifications == null) { document.Modifications = new DynamicJsonValue(document); } document.Modifications.Remove(identityProperty.Name); return(true); }
protected static BlittableJsonReaderObject ToBlittableInternal( object entity, DocumentConventions conventions, JsonOperationContext context, IJsonSerializer serializer, IJsonWriter writer, bool removeIdentityProperty = true) { var usesDefaultContractResolver = ((JsonSerializer)serializer).ContractResolver.GetType() == typeof(DefaultRavenContractResolver); var type = entity.GetType(); var isDynamicObject = entity is IDynamicMetaObjectProvider; var willUseDefaultContractResolver = usesDefaultContractResolver && isDynamicObject == false; var hasIdentityProperty = conventions.GetIdentityProperty(type) != null; try { if (willUseDefaultContractResolver) { DefaultRavenContractResolver.RootEntity = removeIdentityProperty && hasIdentityProperty ? entity : null; DefaultRavenContractResolver.RemovedIdentityProperty = false; } serializer.Serialize(writer, entity); } finally { if (willUseDefaultContractResolver) { DefaultRavenContractResolver.RootEntity = null; } } writer.FinalizeDocument(); var reader = writer.CreateReader(); if (willUseDefaultContractResolver == false || hasIdentityProperty && DefaultRavenContractResolver.RemovedIdentityProperty == false) { //This is to handle the case when user defined it's own contract resolver //or we are serializing dynamic object var changes = removeIdentityProperty && TryRemoveIdentityProperty(reader, type, conventions, isDynamicObject); changes |= TrySimplifyJson(reader, type); if (changes) { using (var old = reader) { reader = context.ReadObject(reader, "convert/entityToBlittable"); } } } return(reader); }
private MemberInfo GetIdentityProperty(Type entityType) { return(_conventions.GetIdentityProperty(entityType)); }