public void Read(string response, IServerResponseReaderObserver observer)
        {
            using (var jsonReader = new JsonReader(response))
            {
                IDictionary<string, object> jsonObject;
                try
                {
                    jsonObject = jsonReader.ReadValue() as IDictionary<string, object>;
                }
                catch (FormatException fe)
                {
                    observer.OnInvalidJsonResponse(fe);
                    return;
                }

                if (jsonObject.ContainsKey(ApiOperation.ApiError))
                {
                    var errorObj = jsonObject[ApiOperation.ApiError] as IDictionary<string, object>;
                    var code = errorObj[ApiOperation.ApiErrorCode] as string;
                    var message = errorObj[ApiOperation.ApiErrorMessage] as string;
                    observer.OnErrorResponse(code, message);
                    return;
                }

                observer.OnSuccessResponse(jsonObject, response);
            }
        }
        private static LiveLoginResult ReadResponse(HttpWebResponse response)
        {
            LiveConnectSession newSession = null;
            LiveConnectSessionStatus status = LiveConnectSessionStatus.Unknown;
            IDictionary<string, object> jsonObj = null;
            try
            {
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    JsonReader jsReader = new JsonReader(reader.ReadToEnd());
                    jsonObj = jsReader.ReadValue() as IDictionary<string, object>;
                }
            }
            catch (FormatException fe)
            {
                return new LiveLoginResult(
                    new LiveAuthException(AuthErrorCodes.ServerError, fe.Message));
            }

            if (jsonObj != null)
            {
                if (jsonObj.ContainsKey(AuthConstants.Error))
                {
                    string errorCode = jsonObj[AuthConstants.Error] as string;
                    string errorDescription = string.Empty;
                    if (jsonObj.ContainsKey(AuthConstants.ErrorDescription))
                    {
                        errorDescription = jsonObj[AuthConstants.ErrorDescription] as string;
                    }

                    return new LiveLoginResult(new LiveAuthException(errorCode, errorDescription));
                }
                else
                {
                    status = LiveConnectSessionStatus.Connected;
                    newSession = CreateSession(jsonObj);
                    return new LiveLoginResult(status, newSession);
                }
            }

            return new LiveLoginResult(
                    new LiveAuthException(AuthErrorCodes.ServerError, ErrorText.ServerError));
        }
        /// <summary>
        /// Called when a BackgroundTransferRequet's TransferStatus is set to Completed.
        /// This method will remove the request from the BackgroundTransferService and call
        /// the LiveOperationEventArgs event handler.
        /// </summary>
        /// <param name="request">request with a TransferStatus that is set to Completed</param>
        private void OnTransferStatusComplete(BackgroundTransferRequest request)
        {
            Debug.Assert(request.TransferStatus == TransferStatus.Completed);
            Debug.Assert(BackgroundTransferHelper.IsDownloadRequest(request));

            this.OnBackgroundTransferRequestCompleted(request);

            // Remove the transfer request in order to make room in the queue for more transfers.
            // Transfers are not automatically removed by the system.
            // Cancelled requests have already been removed from the system and cannot be removed twice.
            if (!BackgroundTransferHelper.IsCanceledRequest(request))
            {
                try
                {
                    this.backgroundTransferService.Remove(request);
                }
                catch (Exception exception)
                {
                    this.tcs.TrySetException(new LiveConnectException(
                        ApiOperation.ApiClientErrorCode,
                        ResourceHelper.GetString("BackgroundTransferServiceRemoveError"),
                        exception));
                    return;
                }
            }

            if (request.TransferError != null)
            {
                var exception = new LiveConnectException(ApiOperation.ApiServerErrorCode,
                                                         ResourceHelper.GetString("ServerError"),
                                                         request.TransferError);

                this.tcs.TrySetException(exception);
            }
            else if (!BackgroundTransferHelper.IsSuccessfulStatusCode(request.StatusCode))
            {
                var exception = new LiveConnectException(ApiOperation.ApiServerErrorCode,
                                                         ResourceHelper.GetString("ServerError"));
                this.tcs.TrySetException(exception);
            }
            else
            {
                string jsonResponse = string.Format(JsonResponse, request.DownloadLocation.OriginalString.Replace("\\", "\\\\"));
                var jsonReader = new JsonReader(jsonResponse);
                var jsonObject = jsonReader.ReadValue() as IDictionary<string, object>;
                var result = new LiveOperationResult(jsonObject, jsonResponse);

                this.tcs.TrySetResult(result);
            }
        }
        private LiveLoginResult GenerateLoginResultFrom(Stream responseStream)
        {
            IDictionary<string, object> jsonObj;
            try
            {
                using (var reader = new StreamReader(responseStream))
                {
                    var jsReader = new JsonReader(reader.ReadToEnd());
                    jsonObj = jsReader.ReadValue() as IDictionary<string, object>;
                }
            }
            catch (FormatException fe)
            {
                return new LiveLoginResult(
                    new LiveAuthException(AuthErrorCodes.ServerError, fe.Message));
            }

            if (jsonObj == null)
            {
                return new LiveLoginResult(
                        new LiveAuthException(AuthErrorCodes.ServerError, ResourceHelper.GetString("ServerError")));
            }

            if (jsonObj.ContainsKey(AuthConstants.Error))
            {
                var errorCode = jsonObj[AuthConstants.Error] as string;
                if (errorCode.Equals(AuthErrorCodes.InvalidGrant, StringComparison.Ordinal))
                {
                    return new LiveLoginResult(LiveConnectSessionStatus.NotConnected, null);
                }

                string errorDescription = string.Empty;
                if (jsonObj.ContainsKey(AuthConstants.ErrorDescription))
                {
                    errorDescription = jsonObj[AuthConstants.ErrorDescription] as string;
                }

                return new LiveLoginResult(new LiveAuthException(errorCode, errorDescription));
            }

            LiveConnectSession newSession = LiveAuthClient.CreateSession(this.AuthClient, jsonObj);
            return new LiveLoginResult(LiveConnectSessionStatus.Connected, newSession);
        }