public JsonSerializerSettings?GetJsonSerializerSettings(IJsonSerializationOptions?options = null, bool caseSensitive = false)
        {
            if (caseSensitive)
            {
                throw new NotSupportedException("Newtonsoft does not support case-sensitive deserialization.");
            }

            if (options == null)
            {
                return(null);
            }

            if (options.CamelCaseKeys == true && options.CamelCaseProperties != true)
            {
                throw new NotSupportedException("Camel casing keys but not properties is not supported.");
            }

            var o = _settings.Clone();

            if (options.CamelCaseKeys == true)
            {
                o.ContractResolver = new DefaultContractResolver
                {
                    NamingStrategy = new CamelCaseNamingStrategy
                    {
                        ProcessDictionaryKeys = true
                    }
                };
            }
            else if (options.CamelCaseProperties == true)
            {
                o.ContractResolver = new CamelCasePropertyNamesContractResolver();
            }
            else if (options.CamelCaseProperties == false)
            {
                o.ContractResolver = new DefaultContractResolver();
            }

            if (options.OmitNull.HasValue)
            {
                o.NullValueHandling = options.OmitNull.Value ? NullValueHandling.Ignore : NullValueHandling.Include;
            }

            if (options.Indent.HasValue)
            {
                o.Formatting = options.Indent.Value ? Formatting.Indented : Formatting.None;
            }

            return(o);
        }
Ejemplo n.º 2
0
        public override Task WriteToStreamAsync(Type type, object value, Stream stream, HttpContent content, TransportContext transportContext)
        {
            var formatRequest = GetFormatType();
            var task          = Task.Factory.StartNew(() =>
            {
                var settings = Settings;

                if (formatRequest != null)
                {
                    settings = Settings.Clone();
                    settings.ContractResolver = formatRequest;
                }
                var json = JsonConvert.SerializeObject(value, Formatting, settings);
                var buf  = Encoding.UTF8.GetBytes(json);
                stream.Write(buf, 0, buf.Length);
                stream.Flush();
            });

            return(task);
        }
 public JsonSerializerFactory(JsonSerializerSettings?defaultOptions)
 {
     _settings = defaultOptions?.Clone() ?? DefaultOptions;
 }