Esempio n. 1
0
        private void WriteToStream(Type type, object value, Stream writeStream, HttpContent content)
        {
            Encoding effectiveEncoding = SelectCharacterEncoding(content == null ? null : content.Headers);

            using (JsonTextWriter jsonTextWriter = new JsonTextWriter(new StreamWriter(writeStream, effectiveEncoding))
            {
                CloseOutput = false
            })
            {
                if (Indent)
                {
                    jsonTextWriter.Formatting = Newtonsoft.Json.Formatting.Indented;
                }
                JsonSerializer jsonSerializer = JsonSerializer.Create(_jsonSerializerSettings);

                if (!ShouldBypassPartialResponse(_request))
                {
                    _fields = GetPartialResponseFields(_request);
                }

                if (_fields == null)
                {
                    jsonSerializer.Serialize(jsonTextWriter, value);
                }
                else
                {
                    PartialJsonMediaTypeFormatterUtilities.RemovePropertiesAndArrayElements(value, jsonTextWriter, jsonSerializer, ShouldSerialize);
                }

                jsonTextWriter.Flush();
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Returns a value that indicates whether the field should be serialized.
        /// </summary>
        /// <param name="field">The field.</param>
        /// <param name="tokenType">The type.</param>
        /// <returns>True if the value should be serialized, otherwise false.</returns>
        protected virtual bool ShouldSerialize(string field)
        {
            if (field == null)
            {
                throw new ArgumentNullException("field");
            }

            var pattern      = PartialJsonMediaTypeFormatterUtilities.GetRegexPatternForField(field);
            var regexOptions = RegexOptions.None;

            if (IgnoreCase)
            {
                regexOptions = RegexOptions.IgnoreCase;
            }

            return(_fields.Any(f => Regex.IsMatch(f, pattern, regexOptions)));
        }