Пример #1
0
        private T Deserialize <T>(RavenJObject result)
        {
            var metadata = result.Value <RavenJObject>("@metadata");

            if (
                // we asked for a projection directly from the index
                projectionFields != null && projectionFields.Length > 0
                // we got a document without an @id
                // we aren't querying a document, we are probably querying a map reduce index result or a projection
                || metadata == null || string.IsNullOrEmpty(metadata.Value <string>("@id")))
            {
                if (typeof(T) == typeof(RavenJObject))
                {
                    return((T)(object)result);
                }

#if !NET_3_5
                if (typeof(T) == typeof(object))
                {
                    return((T)(object)new DynamicJsonObject(result));
                }
#endif

                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
                    projectionFields != null && projectionFields.Length == 1 &&                          // We are projecting one field only (although that could be derived from the
                    // previous check, one could never be too careful
                    ((metadata != null && result.Count == 2) || (metadata == null && result.Count == 1)) // there are no more props in the result object
                    )
                {
                    return((T)(object)documentId);
                }

                HandleInternalMetadata(result);

                var deserializedResult = DeserializedResult <T>(result);

                if (string.IsNullOrEmpty(documentId) == false)
                {
                    // we need to make an addtional 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.TrySetIdentity(deserializedResult, documentId);
                    }
                }

                return(deserializedResult);
            }
            return(sessionOperations.TrackEntity <T>(metadata.Value <string>("@id"),
                                                     result,
                                                     metadata));
        }