コード例 #1
0
        private void EnsureSerializer(OutputFormatterCanWriteContext context)
        {
            if (context.HttpContext.Items[Constants.ContextKeys.JsonMultiCase] is ITextTransform transform)
            {
                if (!_resolvers.TryGetValue(transform, out var resolver))
                {
                    resolver = new JsonContractResolver(transform, JsonProcessingDirection.Output);
                    _resolvers.Add(transform, resolver);
                }

                _serializer.ContractResolver = resolver;
            }

            if (context.HttpContext.Items[Constants.ContextKeys.JsonPrettyPrint] is bool prettyPrint && !prettyPrint)
            {
                _serializer.Apply(s => s.Formatting = Formatting.None);
            }

            if (context.HttpContext.Items[Constants.ContextKeys.JsonTrim] is bool trim && trim)
            {
                _serializer.Apply(s =>
                {
                    s.NullValueHandling    = NullValueHandling.Ignore;
                    s.DefaultValueHandling = DefaultValueHandling.Ignore;
                });
            }
        }