Ejemplo n.º 1
0
        /// <summary>
        /// Writes the given API response to the given HTTP context's response.
        /// </summary>
        /// <param name="context">The HTTP context whose response should be written to.</param>
        /// <param name="request">The de-serialized API request that generated the current response. May be null.</param>
        /// <param name="response">The API response to write.</param>
        /// <param name="knownTypes">A collection of known types that may exist in the response object graph.</param>
        public virtual void WriteResponse(HttpContextBase context, ApiRequest request, ApiResponse response, IEnumerable<Type> knownTypes)
        {
            string json = response.ToJson(knownTypes);
            json = Regex.Replace(json, "({)?(,)?\"__type\":\".*?\"(})?(,)?", new MatchEvaluator(TypeIdentifiersEvaluator));

            if (context.AcceptsGZip() && request != null && request.GetType().GetCustomAttributes(typeof(GZipAttribute), true).Length > 0)
            {
                context.Response.Filter = new GZipStream(context.Response.Filter, CompressionMode.Compress);
                context.Response.AddHeader("content-encoding", "gzip");
            }

            context.Response.ContentType = "application/json";
            context.Response.Write(json);
        }
 /// <summary>
 /// Writes the given API response to the given HTTP context's response.
 /// </summary>
 /// <param name="context">The HTTP context whose response should be written to.</param>
 /// <param name="request">The de-serialized API request that generated the current response. May be null.</param>
 /// <param name="response">The API response to write.</param>
 /// <param name="knownTypes">A collection of known types that may exist in the response object graph.</param>
 public virtual void WriteResponse(HttpContextBase context, ApiRequest request, ApiResponse response, IEnumerable<Type> knownTypes)
 {
     context.Response.ContentType = "application/x-bplist";
     new DataContractBinaryPlistSerializer(response.GetType()).WriteObject(context.Response.OutputStream, response);
 }