<commit_before>﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Dynamic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Threading.Tasks;
using System.Web;
using Humanizer;

namespace EmberJS.WebAPI
{
    public class EmberJsonMediaTypeFormatter : JsonMediaTypeFormatter
    {
        private int _maxDepth = 256;
        private JsonSerializerSettings _jsonSerializerSettings;
        public EmberJsonMediaTypeFormatter()
        {
            _jsonSerializerSettings = CreateDefaultSerializerSettings();
        }
        public override System.Threading.Tasks.Task WriteToStreamAsync(System.Type type, object value, System.IO.Stream writeStream, System.Net.Http.HttpContent content, System.Net.TransportContext transportContext)
        {
            var root = GetRootFieldName(type, value);
            var obj = new ExpandoObject() as IDictionary<string, Object>;
            obj[root] = value;
            return base.WriteToStreamAsync(type, obj as object, writeStream, content, transportContext);
        }

        public override Task<object> ReadFromStreamAsync(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger)
        {
            return Task.FromResult(ReadFromStream(type, readStream, content, formatterLogger)); ;
        }

        private object ReadFromStream(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger)
        {
            var root = GetRootFieldName(type);
            var contentHeaders = content == null ? null : content.Headers;

            // If content length is 0 then return default value for this type
            if (contentHeaders != null && contentHeaders.ContentLength == 0)
                return GetDefaultValueForType(type);

            // Get the character encoding for the content
            var effectiveEncoding = SelectCharacterEncoding(contentHeaders);

            try
            {
                using (var reader = (new StreamReader(readStream, effectiveEncoding)))
                {
                    var json = reader.ReadToEnd();
                    var serializer = new EmberJsonSerializer();
                    var deserialized = serializer.Deserialize(json);
                    return deserialized.ToObject(type);
                }
            }
            catch (Exception e)
            {
                if (formatterLogger == null)
                {
                    throw;
                }
                formatterLogger.LogError(String.Empty, e);
                return GetDefaultValueForType(type);
            }
        }

        private string GetRootFieldName(Type type, dynamic value = null)
        {
            //get element type if array
            var isCollection = false;
            if (value != null && (value is IEnumerable || type.IsArray))
            {
                type = this.GetElementTypeFromCollection(value);
                isCollection = true;
            }

            var attrs = type.CustomAttributes.Where(x => x.AttributeType == typeof(Newtonsoft.Json.JsonObjectAttribute));

            if (attrs.Any())
            {
                var titles = attrs.First().NamedArguments.Where(arg => arg.MemberName == "Title")
                    .Select(arg => arg.TypedValue.Value.ToString()).ToList();

                if (titles.Any())
                {
                    if (isCollection)
                    {
                        return titles.First().Camelize().Pluralize();
                    }
                    return titles.First().Camelize();
                }
            }

            return isCollection ? type.Name.Camelize().Pluralize() : type.Name.Camelize();
        }

        private Type GetElementTypeFromCollection(dynamic collection)
        {
            var type = collection.GetType();
            // For arrays
            var elementType = type.GetElementType();
            if (elementType != null)
            {
                return elementType;
            }

            // For other collections
            var elementTypes = type.GetGenericArguments();
            if (elementTypes.Length > 0)
            {
                return elementTypes[0];
            }

            return null;
        }
    }
}<commit_msg>fixed GetElementTypeFromCollection to not return null<commit_after>﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Dynamic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Threading.Tasks;
using System.Web;
using Humanizer;

namespace EmberJS.WebAPI
{
    public class EmberJsonMediaTypeFormatter : JsonMediaTypeFormatter
    {
        private int _maxDepth = 256;
        private JsonSerializerSettings _jsonSerializerSettings;
        public EmberJsonMediaTypeFormatter()
        {
            _jsonSerializerSettings = CreateDefaultSerializerSettings();
        }
        public override System.Threading.Tasks.Task WriteToStreamAsync(System.Type type, object value, System.IO.Stream writeStream, System.Net.Http.HttpContent content, System.Net.TransportContext transportContext)
        {
            var root = GetRootFieldName(type, value);
            var obj = new ExpandoObject() as IDictionary<string, Object>;
            obj[root] = value;
            return base.WriteToStreamAsync(type, obj as object, writeStream, content, transportContext);
        }

        public override Task<object> ReadFromStreamAsync(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger)
        {
            return Task.FromResult(ReadFromStream(type, readStream, content, formatterLogger)); ;
        }

        private object ReadFromStream(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger)
        {
            var root = GetRootFieldName(type);
            var contentHeaders = content == null ? null : content.Headers;

            // If content length is 0 then return default value for this type
            if (contentHeaders != null && contentHeaders.ContentLength == 0)
                return GetDefaultValueForType(type);

            // Get the character encoding for the content
            var effectiveEncoding = SelectCharacterEncoding(contentHeaders);

            try
            {
                using (var reader = (new StreamReader(readStream, effectiveEncoding)))
                {
                    var json = reader.ReadToEnd();
                    var serializer = new EmberJsonSerializer();
                    var deserialized = serializer.Deserialize(json);
                    return deserialized.ToObject(type);
                }
            }
            catch (Exception e)
            {
                if (formatterLogger == null)
                {
                    throw;
                }
                formatterLogger.LogError(String.Empty, e);
                return GetDefaultValueForType(type);
            }
        }

        private string GetRootFieldName(Type type, dynamic value = null)
        {
            //get element type if array
            var isCollection = false;
            if (value != null && (value is IEnumerable || type.IsArray))
            {
                type = this.GetElementTypeFromCollection(value);
                isCollection = true;
            }

            var attrs = type.CustomAttributes.Where(x => x.AttributeType == typeof(Newtonsoft.Json.JsonObjectAttribute));

            if (attrs.Any())
            {
                var titles = attrs.First().NamedArguments.Where(arg => arg.MemberName == "Title")
                    .Select(arg => arg.TypedValue.Value.ToString()).ToList();

                if (titles.Any())
                {
                    if (isCollection)
                    {
                        return titles.First().Camelize().Pluralize();
                    }
                    return titles.First().Camelize();
                }
            }

            return isCollection ? type.Name.Camelize().Pluralize() : type.Name.Camelize();
        }

        private Type GetElementTypeFromCollection(dynamic collection)
        {
            var type = collection.GetType();
            // For arrays
            var elementType = type.GetElementType();
            if (elementType != null)
            {
                return elementType;
            }

            // For other collections
            var elementTypes = type.GetGenericArguments();
            if (elementTypes.Length > 0)
            {
                return elementTypes[0];
            }

            return type;
        }
    }
}
{'fraction_non_alphanumeric': 0.08250226654578423, 'fraction_numerical': 0.001586582048957389, 'mean_word_length': 1.7944901836605447, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '665325', 'n_tokens_mistral': 2106, 'n_tokens_neox': 2018, 'n_words': 511}