private static void SetOption(Serialization.TextSerializationOptions options, string key, string value)
        {
            if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(value))
            {
                return;
            }

            switch (key.ToLowerInvariant())
            {
            case "ignores":
                options.Ignores(value);
                break;

            case "casing":
                if (string.Equals(value, "camel", StringComparison.OrdinalIgnoreCase))
                {
                    options.NamingConvention = Serialization.SerializationNamingConvention.Camel;
                }
                else if (string.Equals(value, "pascal", StringComparison.OrdinalIgnoreCase))
                {
                    options.NamingConvention = Serialization.SerializationNamingConvention.Pascal;
                }
                else
                {
                    options.NamingConvention = Serialization.SerializationNamingConvention.None;
                }

                break;
            }
        }
        private static Serialization.TextSerializationOptions GetSerializationOptions(string behaviors)
        {
            if (string.IsNullOrEmpty(behaviors))
            {
                return(null);
            }

            var parts   = Common.StringExtension.Slice(behaviors, ';');
            var options = new Serialization.TextSerializationOptions()
            {
                Indented = parts.Contains("Indented", StringComparer.OrdinalIgnoreCase),
            };

            foreach (var part in parts)
            {
                var index = part.IndexOf(':');

                if (index > 0 && index < part.Length - 1)
                {
                    SetOption(options, part.Substring(0, index).Trim(), part.Substring(index + 1).Trim());
                }
            }

            return(options);
        }
Example #3
0
        public JsonInputFormatter(Serialization.TextSerializationOptions options = null)
        {
            _options = options ?? Serialization.Serializer.Json.Options;

            SupportedEncodings.Add(UTF8EncodingWithoutBOM);
            SupportedEncodings.Add(UTF16EncodingLittleEndian);
            SupportedMediaTypes.Add("application/json");
            SupportedMediaTypes.Add("text/jsona");
            SupportedMediaTypes.Add("application/*+json");
        }
Example #4
0
        public JsonOutputFormatter(Serialization.TextSerializationOptions options = null)
        {
            _options = options ?? Serialization.Serializer.Json.Options;

            SupportedEncodings.Add(Encoding.UTF8);
            SupportedEncodings.Add(Encoding.Unicode);
            SupportedMediaTypes.Add("application/json");
            SupportedMediaTypes.Add("text/json");
            SupportedMediaTypes.Add("application/*+json");
        }
        private Stream GetWriteStream(HttpContext context, Encoding encoding, out Serialization.TextSerializationOptions options)
        {
            options = null;

            if (context.Request.Headers.TryGetValue("x-json-behaviors", out var behaviors))
            {
                options = GetSerializationOptions(behaviors);
            }

            if (encoding.CodePage == Encoding.UTF8.CodePage)
            {
                return(context.Response.Body);
            }

            return(new TranscodingWriteStream(context.Response.Body, encoding));
        }