예제 #1
0
        public void It_does_not_JSON_encode_JSON_types(JToken jtoken)
        {
            var formatter = JsonFormatter.Create(jtoken.GetType());

            var writer = new StringWriter();

            formatter.Format(jtoken, writer);

            writer
            .ToString()
            .Should()
            .Be(jtoken.ToString(Newtonsoft.Json.Formatting.None));
        }
 public override void OnException(HttpActionExecutedContext context)
 {
     if (context.Exception is EntityErrorsException)
     {
         var e     = (EntityErrorsException)context.Exception;
         var error = new SaveError(e.Message, e.EntityErrors);
         var resp  = new HttpResponseMessage(e.StatusCode)
         {
             Content = new ObjectContent(typeof(SaveError), error, JsonFormatter.Create()),
         };
         context.Response = resp;
     }
 }
예제 #3
0
        protected override MediaTypeFormatter GetJsonFormatter()
        {
            JsonMediaTypeFormatter formatter = JsonFormatter.Create();

            if (formatter.SerializerSettings.ContractResolver != null)
            {
                throw new Exception("BreezeGuard does not support custom JSON ContractResolvers.");
            }

            // TODO: Is formatter.SerializerSettings and object that is shared across all Breeze formatters? If so,
            // we may have to clone it here.

            formatter.SerializerSettings.ContractResolver =
                new BreezeGuardContractResolver(this.ContextProviderType);

            return(formatter);
        }
        /// <summary>
        /// Return the Breeze-specific <see cref="MediaTypeFormatter"/> that formats
        /// content to JSON. This formatter must be tailored to work with Breeze clients. 
        /// </summary>
        /// <remarks>
        /// By default returns the Breeze <see cref="JsonFormatter"/>.
        /// Override it to substitute a custom JSON formatter.
        /// </remarks>
        protected virtual JsonMediaTypeFormatter GetJsonFormatter(HttpConfiguration configuration)
        {
            var formatter = JsonFormatter.Create();
            var jsonSerializer = formatter.SerializerSettings;
            if (!formatter.SerializerSettings.Converters.Any(o => o is NHibernateProxyJsonConverter))
                jsonSerializer.Converters.Add(new NHibernateProxyJsonConverter());
            jsonSerializer.ContractResolver = (IContractResolver)configuration.DependencyResolver.GetService(typeof(NHibernateContractResolver));
            // Setup save serializer
            var saveJsonSerializer = BreezeConfig.Instance.GetJsonSerializerSettingsForSave();
            saveJsonSerializer.ContractResolver = jsonSerializer.ContractResolver;

            /* Error handling is not needed anymore. NHibernateContractResolver will take care of non initialized properties*/
            //FIX: Still errors occurs
            jsonSerializer.Error = (sender, args) =>
            {
                // When the NHibernate session is closed, NH proxies throw LazyInitializationException when
                // the serializer tries to access them.  We want to ignore those exceptions.
                var error = args.ErrorContext.Error;
                if (error is LazyInitializationException || error is ObjectDisposedException)
                    args.ErrorContext.Handled = true;
            };
            return formatter;
        }