Exemple #1
0
        public T Deserialize <T>(RavenJObject result)
        {
            var metadata = result.Value <RavenJObject>("@metadata");

            if ((projectionFields == null || projectionFields.Length <= 0) &&
                (metadata != null && string.IsNullOrEmpty(metadata.Value <string>("@id")) == false))
            {
                return(sessionOperations.TrackEntity <T>(metadata.Value <string>("@id"),
                                                         result,
                                                         metadata, disableEntitiesTracking));
            }

            if (typeof(T) == typeof(RavenJObject))
            {
                return((T)(object)result);
            }

            if (typeof(T) == typeof(object) && string.IsNullOrEmpty(result.Value <string>("$type")))
            {
                return((T)(object)new DynamicJsonObject(result));
            }

            var documentId = result.Value <string>(Constants.DocumentIdFieldName);  //check if the result contain the reserved name

            if (!string.IsNullOrEmpty(documentId) && typeof(T) == typeof(string) && // __document_id is present, and result type is a string
                                                                                    // We are projecting one field only (although that could be derived from the
                                                                                    // previous check, one could never be too careful
                projectionFields != null && projectionFields.Length == 1 &&
                HasSingleValidProperty(result, metadata)                            // there are no more props in the result object
                )
            {
                return((T)(object)documentId);
            }

            sessionOperations.HandleInternalMetadata(result);

            var deserializedResult = DeserializedResult <T>(result);

            if (string.IsNullOrEmpty(documentId) == false)
            {
                // we need to make an additional check, since it is possible that a value was explicitly stated
                // for the identity property, in which case we don't want to override it.
                var identityProperty = sessionOperations.Conventions.GetIdentityProperty(typeof(T));
                if (identityProperty != null &&
                    (result[identityProperty.Name] == null ||
                     result[identityProperty.Name].Type == JTokenType.Null))
                {
                    sessionOperations.GenerateEntityIdOnTheClient.TrySetIdentity(deserializedResult, documentId);
                }
            }

            return(deserializedResult);
        }