public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            if (reader.TokenType != JsonToken.Null)
            {
                JObject jObject = JObject.Load(reader);
                object  result;
                if (objectType == typeof(Message))
                {
                    JToken token       = jObject[nameof(MessageType)];
                    var    messageType = (MessageType)Enum.Parse(typeof(MessageType), token.ToString());
                    switch (messageType)
                    {
                    case MessageType.Progress:
                        result = new ProgressMessage();
                        break;

                    case MessageType.Error:
                        result = new ErrorMessage();
                        break;

                    case MessageType.Result:
                        result = new StatisticsMessage();
                        break;

                    default:
                        throw new NotImplementedException($"{token} message type is not supported");
                    }
                }
                else if (objectType == typeof(LanguageStatistics))
                {
                    JToken   token    = jObject[nameof(Language)];
                    Language language = LanguageUtils.ParseLanguages(token.ToString()).FirstOrDefault();
                    if (language == Language.CSharp)
                    {
                        result = new CSharpStatistics();
                    }
                    else if (language == Language.Java)
                    {
                        result = new JavaStatistics();
                    }
                    else if (language == Language.Php)
                    {
                        result = new PhpStatistics();
                    }
                    else
                    {
                        throw new NotImplementedException($"{token} language is not supported");
                    }
                }
                else
                {
                    throw new FormatException("Invalid JSON");
                }

                serializer.Populate(jObject.CreateReader(), result);
                return(result);
            }

            return(null);
        }
예제 #2
0
        public StatisticsMessage CollectStatistics(string directoryPath, int startInd = 0, int length = 0)
        {
            IEnumerable <string> fileNames = Enumerable.Empty <string>();

            if (Directory.Exists(directoryPath))
            {
                fileNames = Directory.GetFiles(directoryPath, "*.*", SearchOption.AllDirectories);
                if (startInd != 0)
                {
                    fileNames = fileNames.Skip(startInd);
                }
                if (length != 0)
                {
                    fileNames = fileNames.Take(length);
                }
            }
            else
            {
                fileNames = new string[] { directoryPath };
            }
            int totalFilesCount = fileNames.Count();

            var phpStatistics    = new PhpStatistics();
            var javaStatistics   = new JavaStatistics();
            var csharpStatistics = new CSharpStatistics();

            int processedCount = 0;

            if (!Multithreading)
            {
                foreach (var filePath in fileNames)
                {
                    CollectStatistics(phpStatistics, javaStatistics, csharpStatistics, filePath, totalFilesCount, ref processedCount);
                }
            }
            else
            {
                var options = new ParallelOptions {
                    MaxDegreeOfParallelism = Environment.ProcessorCount
                };
                Parallel.ForEach(fileNames, options, filePath =>
                {
                    Thread.CurrentThread.CurrentCulture   = CultureInfo.InvariantCulture;
                    Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;
                    CollectStatistics(phpStatistics, javaStatistics, csharpStatistics, filePath, totalFilesCount, ref processedCount);
                });
            }

            var result = new StatisticsMessage
            {
                Id                 = Guid.NewGuid().ToString(),
                ErrorCount         = Logger?.ErrorCount ?? 0,
                LanguageStatistics = new List <LanguageStatistics>()
                {
                    phpStatistics,
                    javaStatistics,
                    csharpStatistics
                }
            };

            return(result);
        }