Exemplo n.º 1
0
        public static async Task <bool> GetResultProcess(string processUrl, int id)
        {
            using (var client = new HttpClient()) {
                var request = new HttpRequestMessage {
                    Method     = HttpMethod.Get,
                    RequestUri = new Uri(processUrl),
                    Headers    =
                    {
                        { "x-token", "4950fc7e-55be-44ea-8832-58ebbd0cd436" }
                    },
                };

                using (var massage = await client.SendAsync(request)) {
                    string response = null;
                    var    result   = await massage.Content.ReadAsStringAsync();

                    var list = JsonSerializer.Deserialize <dynamic>(result);
                    foreach (var item in list)
                    {
                        if (item["id"] == id)
                        {
                            response = item["progress_status"];
                        }
                    }

                    if (response == "success")
                    {
                        return(true);
                    }
                    return(false);
                }
            }
        }
Exemplo n.º 2
0
        public static async Task <List <dynamic> > GetAdAnalytics(string[] analysticsUrl, int id)
        {
            using (var client = new HttpClient()) {
                List <dynamic> results = new List <dynamic>();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                var postData = "?input_parameters=" +
                               Encoding.UTF8.GetString(JsonSerializer.Serialize(new { ad_campaign_id = id }));

                for (int i = 0; i < analysticsUrl.Length; i++)
                {
                    var request = new HttpRequestMessage {
                        Method     = HttpMethod.Get,
                        RequestUri = new Uri(analysticsUrl[i] + postData),
                        Headers    =
                        {
                            { "x-token", "4950fc7e-55be-44ea-8832-58ebbd0cd436" }
                        },
                    };

                    using (var massage = await client.SendAsync(request)) {
                        results.Add(JsonSerializer.Deserialize <dynamic>(await massage.Content.ReadAsStringAsync()));
                    }
                }
                return(results);
            }
        }
Exemplo n.º 3
0
        public void Utf8Json()
        {
            var json = Utf8JsonSerializer.ToJsonString(PolymorphismScenario.Instance2, JsonSettings.Utf8JsonResolver);

            _output.WriteLine(json);

            var deserializedObject = Utf8JsonSerializer.Deserialize <BaseClass>(json, JsonSettings.Utf8JsonResolver);

            deserializedObject.Should().BeOfType <Subclass2>();
        }
        public void Utf8Json()
        {
            var json = Utf8JsonSerializer.ToJsonString(PreserveReferencesScenario.ObjectGraphRoot, JsonSettings.Utf8JsonResolver);

            _output.WriteLine(json);

            var deserializedGraph = Utf8JsonSerializer.Deserialize <A>(json, JsonSettings.Utf8JsonResolver);

            deserializedGraph.C.Should().BeSameAs(deserializedGraph.B.C);
        }
        public void JsonSerializer_Dictionary_DateTimeOffset()
        {
            var dict = new Dictionary <string, object>();

            dict["Timestamp"] = DateTimeOffset.UtcNow;
            var bts    = Utf8JsonSerializer.Serialize(dict, Utf8Json.Resolvers.StandardResolver.AllowPrivateExcludeNullSnakeCase);
            var newdt  = Utf8JsonSerializer.Deserialize <Dictionary <string, object> >(bts, Utf8Json.Resolvers.StandardResolver.AllowPrivateExcludeNullSnakeCase);
            var newdt2 = JsonObjectTypeDeserializer.Deserialize <DateTimeOffset>(newdt, "Timestamp");

            Assert.Equal(dict["Timestamp"], newdt2);
        }
        public void JsonSerializer_DateTimeOffset()
        {
            var timestamp = DateTimeOffset.UtcNow;
            var bts       = Utf8JsonSerializer.Serialize(timestamp, Utf8Json.Resolvers.StandardResolver.AllowPrivateExcludeNullSnakeCase);
            var newdt     = Utf8JsonSerializer.Deserialize <DateTimeOffset>(bts, Utf8Json.Resolvers.StandardResolver.AllowPrivateExcludeNullSnakeCase);

            Assert.Equal(timestamp, newdt);
            var newdt1 = JsonConvert.DeserializeObject <DateTimeOffset>(System.Text.Encoding.UTF8.GetString(bts));

            Assert.Equal(timestamp, newdt1);
            var newdt2 = JsonObjectTypeDeserializer.Deserialize <DateTimeOffset>(System.Text.Encoding.UTF8.GetString(bts));

            Assert.Equal(timestamp, newdt2);
        }
Exemplo n.º 7
0
        private static async Task <GameCharacter?> GetCharacterAsync(IHttpClientFactory factory,
                                                                     IConfiguration configuration, int id)
        {
            var baseUri  = configuration.GetValue <string>("API");
            var request  = new HttpRequestMessage(HttpMethod.Get, $"{baseUri}/character/{id}");
            var client   = factory.CreateClient();
            var response = await client.SendAsync(request);

            if (!response.IsSuccessStatusCode)
            {
                return(null);
            }

            var content = await response.Content.ReadAsStringAsync();

            return(JsonSerializer.Deserialize <GameCharacter>(content));
        }
Exemplo n.º 8
0
        public void Setup()
        {
            Model.Initialize();
            T deserializedModel;

            // Newtonsoft
            using (MemoryStream stream = new MemoryStream())
            {
                using StreamWriter textWriter   = new StreamWriter(stream);
                using JsonTextWriter jsonWriter = new JsonTextWriter(textWriter);

                var serializer = new Newtonsoft.Json.JsonSerializer();
                serializer.Serialize(jsonWriter, Model);
                jsonWriter.Flush();

                NewtonsoftJsonData = stream.GetBuffer();

                stream.Seek(0, SeekOrigin.Begin);
                using StreamReader textReader   = new StreamReader(stream);
                using JsonTextReader jsonReader = new JsonTextReader(textReader);
                deserializedModel = serializer.Deserialize <T>(jsonReader);

                if (!Model.Equals(deserializedModel))
                {
                    throw new InvalidOperationException("Failed comparison with Newtonsoft.Json");
                }
            }

            // Binary formatter
            using (MemoryStream stream = new MemoryStream())
            {
                var formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                formatter.Serialize(stream, Model);

                BinaryFormatterData = stream.GetBuffer();

                stream.Seek(0, SeekOrigin.Begin);
                deserializedModel = (T)formatter.Deserialize(stream);

                if (!Model.Equals(deserializedModel))
                {
                    throw new InvalidOperationException("Failed comparison with BinaryFormatter");
                }
            }

            // .NETCore JSON
            using (MemoryStream stream = new MemoryStream())
            {
                using Utf8JsonWriter jsonWriter = new Utf8JsonWriter(stream);

                System.Text.Json.JsonSerializer.Serialize(jsonWriter, Model);

                DotNetCoreJsonData = stream.GetBuffer();

                stream.Seek(0, SeekOrigin.Begin);
                deserializedModel = System.Text.Json.JsonSerializer.DeserializeAsync <T>(stream).Result;

                if (!Model.Equals(deserializedModel))
                {
                    throw new InvalidOperationException("Failed comparison with System.Text.Json");
                }
            }

            // DataContractJson
            using (MemoryStream stream = new MemoryStream())
            {
                var serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(T));
                serializer.WriteObject(stream, Model);

                DataContractJsonData = stream.GetBuffer();

                stream.Seek(0, SeekOrigin.Begin);
                deserializedModel = (T)serializer.ReadObject(stream);

                if (!Model.Equals(deserializedModel))
                {
                    throw new InvalidOperationException("Failed comparison with DataContractJson");
                }
            }

            // XML serializer
            using (MemoryStream stream = new MemoryStream())
            {
                var serializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
                serializer.Serialize(stream, Model);

                XmlSerializerData = stream.GetBuffer();

                stream.Seek(0, SeekOrigin.Begin);
                deserializedModel = (T)serializer.Deserialize(stream);

                if (!Model.Equals(deserializedModel))
                {
                    throw new InvalidOperationException("Failed comparison with XmlSerializer");
                }
            }

            // Portable Xaml
            using (MemoryStream stream = new MemoryStream())
            {
                Portable.Xaml.XamlServices.Save(stream, Model);

                PortableXamlData = stream.GetBuffer();

                stream.Seek(0, SeekOrigin.Begin);
                _ = Portable.Xaml.XamlServices.Load(stream);
                if (!Model.Equals(deserializedModel))
                {
                    throw new InvalidOperationException("Failed comparison with Portable.Xaml");
                }
            }

            // Utf8Json
            using (MemoryStream stream = new MemoryStream())
            {
                Utf8JsonSerializer.Serialize(stream, Model);

                Utf8JsonData = stream.GetBuffer();

                stream.Seek(0, SeekOrigin.Begin);
                deserializedModel = Utf8JsonSerializer.Deserialize <T>(stream);

                if (!Model.Equals(deserializedModel))
                {
                    throw new InvalidOperationException("Failed comparison with Utf8Json");
                }
            }

            // MessagePack
            using (MemoryStream stream = new MemoryStream())
            {
                MessagePack.MessagePackSerializer.Serialize(stream, Model, MessagePack.Resolvers.ContractlessStandardResolver.Instance);

                MessagePackData = stream.GetBuffer();

                stream.Seek(0, SeekOrigin.Begin);
                deserializedModel = MessagePack.MessagePackSerializer.Deserialize <T>(stream, MessagePack.Resolvers.ContractlessStandardResolver.Instance);

                if (!Model.Equals(deserializedModel))
                {
                    throw new InvalidOperationException("Failed comparison with MessagePack");
                }
            }

            // BinaryPack
            using (MemoryStream stream = new MemoryStream())
            {
                BinaryConverter.Serialize(Model, stream);

                BinaryPackData = stream.GetBuffer();

                stream.Seek(0, SeekOrigin.Begin);
                deserializedModel = BinaryConverter.Deserialize <T>(stream);

                if (!Model.Equals(deserializedModel))
                {
                    throw new InvalidOperationException("Failed comparison with BinaryPack");
                }
            }
        }
Exemplo n.º 9
0
 public T Deserialize <T>()
 {
     return(JsonSerializer.Deserialize <T>(_result));
 }
Exemplo n.º 10
0
        public void Utf8Json2()
        {
            using Stream stream = new MemoryStream(Utf8JsonData);

            _ = Utf8JsonSerializer.Deserialize <T>(stream);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Import movies to database
        /// </summary>
        /// <param name="rawImports">Documents to import</param>
        /// <param name="pbar"><see cref="IProgressBar"/></param>
        /// <returns><see cref="Task"/></returns>
        public async Task Import(IEnumerable <string> rawImports, IProgressBar pbar)
        {
            await TmdbClient.GetConfigAsync();

            var imports        = rawImports.ToList();
            var workBarOptions = new ProgressBarOptions
            {
                ForegroundColor   = ConsoleColor.Yellow,
                ProgressCharacter = '─',
                BackgroundColor   = ConsoleColor.DarkGray,
            };

            using (var childProgress = pbar?.Spawn(imports.Count, "step import progress", workBarOptions))
            {
                using (var context = new PopcornContextFactory().CreateDbContext(new string[0]))
                {
                    foreach (var import in imports)
                    {
                        try
                        {
                            // Deserialize a document to a movie
                            var movieJson =
                                JsonSerializer.Deserialize <MovieJson>(import);

                            if (movieJson.Torrents == null || movieJson.Cast == null)
                            {
                                continue;
                            }

                            var movie = new Movie
                            {
                                ImdbCode = movieJson.ImdbCode,
                                Url      = movieJson.Url,
                                Torrents = movieJson.Torrents.Select(torrent => new TorrentMovie
                                {
                                    Url              = torrent.Url,
                                    DateUploaded     = torrent.DateUploaded,
                                    DateUploadedUnix = torrent.DateUploadedUnix,
                                    Quality          = torrent.Quality,
                                    Hash             = torrent.Hash,
                                    Peers            = torrent.Peers,
                                    Seeds            = torrent.Seeds,
                                    Size             = torrent.Size,
                                    SizeBytes        = torrent.SizeBytes
                                }).ToList(),
                                DateUploaded     = movieJson.DateUploaded,
                                DateUploadedUnix = movieJson.DateUploadedUnix,
                                DownloadCount    = movieJson.DownloadCount,
                                MpaRating        = movieJson.MpaRating,
                                Runtime          = movieJson.Runtime,
                                YtTrailerCode    = movieJson.YtTrailerCode,
                                DescriptionIntro = movieJson.DescriptionIntro,
                                TitleLong        = movieJson.TitleLong,
                                Rating           = movieJson.Rating,
                                Year             = movieJson.Year,
                                LikeCount        = movieJson.LikeCount,
                                DescriptionFull  = movieJson.DescriptionFull,
                                Cast             = movieJson.Cast?.Select(cast => new Database.Cast
                                {
                                    ImdbCode      = cast?.ImdbCode,
                                    SmallImage    = cast?.SmallImage,
                                    CharacterName = cast?.CharacterName,
                                    Name          = cast?.Name
                                }).ToList(),
                                Genres = movieJson.Genres?.Select(genre => new Genre
                                {
                                    Name = genre
                                }).ToList() ?? new List <Genre>(),
                                GenreNames      = string.Join(", ", movieJson.Genres?.Select(FirstCharToUpper) ?? new List <string>()),
                                Language        = movieJson.Language,
                                Slug            = movieJson.Slug,
                                Title           = movieJson.Title,
                                BackgroundImage = movieJson.BackgroundImage,
                                PosterImage     = movieJson.PosterImage
                            };

                            if (!context.MovieSet.Any(a => a.ImdbCode == movie.ImdbCode))
                            {
                                await RetrieveAssets(movie);

                                context.MovieSet.Add(movie);
                                await context.SaveChangesAsync();
                            }
                            else
                            {
                                var existingEntity =
                                    await context.MovieSet.Include(a => a.Torrents)
                                    .FirstOrDefaultAsync(a => a.ImdbCode == movie.ImdbCode);

                                existingEntity.DownloadCount = movie.DownloadCount;
                                existingEntity.LikeCount     = movie.LikeCount;
                                existingEntity.Rating        = movie.Rating;
                                foreach (var torrent in existingEntity.Torrents)
                                {
                                    var updatedTorrent =
                                        movie.Torrents.FirstOrDefault(a => a.Quality == torrent.Quality);
                                    if (updatedTorrent == null)
                                    {
                                        continue;
                                    }
                                    torrent.Peers = updatedTorrent.Peers;
                                    torrent.Seeds = updatedTorrent.Seeds;
                                }

                                await context.SaveChangesAsync();
                            }

                            childProgress?.Tick();
                        }
                        catch (Exception ex)
                        {
                            _loggingService.Telemetry.TrackException(ex);
                        }
                    }
                }

                // Finish
                pbar?.Tick();
            }
        }
Exemplo n.º 12
0
        static void Main(string[] args)
        {
            var file  = File.ReadAllText(@"D:\Dataset\up_ds3.json");
            var items = JsonSerializer.Deserialize <DataSet>(file);


            //items.Items = items.Items.Take(5000).ToArray();

            var uniqueProductIds = items.Items.Select(x => x.ProductId).Distinct().ToList();
            var usersCount       = items.Items.Select(x => x.UserId).Distinct().ToList();

            var productUserGroup = items.Items.GroupBy(x => x.ProductId, x => x.UserId).ToDictionary(x => x.Key, x => x.Distinct());

            var listProductScore = new ConcurrentBag <ProductScore>();

            var productsCounter = 0;

            var path = $"D:\\Dataset\\ds-calc-result-{DateTime.Now:yy-MM-dd-HH-mm}.txt";
            var fs   = File.Create(path);

            fs.Close();

            //var sw = File.CreateText($"D:\\Dataset\\ds -calc-result-{DateTime.Now:yy-MM-dd-HH-mm}.txt");

            foreach (var uniqueProductId in uniqueProductIds)
            {
                //var boughtUsers = items.Items.Where(x => x.ProductId == uniqueProductId).Select(x=>x.UserId).Distinct().ToList();
                productUserGroup.TryGetValue(uniqueProductId, out var boughtUsers);
                if (boughtUsers == null)
                {
                    boughtUsers = new List <string>();
                }

                Console.WriteLine($"Process productsCounter - {productsCounter++} of {uniqueProductIds.Count}");

                Parallel.ForEach(uniqueProductIds, new ParallelOptions {
                    MaxDegreeOfParallelism = 5
                }, (productId) =>
                {
                    if (productId == uniqueProductId)
                    {
                        return;
                    }

                    //var boughtUsers2 = items.Items.Where(x => x.ProductId == productId).Select(x => x.UserId).Distinct().ToList();
                    var commonUsers = productUserGroup.TryGetValue(productId, out var boughtUsers2) ? boughtUsers.Intersect(boughtUsers2).Count() : 0;
                    var scrore      = commonUsers / (double)usersCount.Count;

                    listProductScore.Add(new ProductScore
                    {
                        Product1 = uniqueProductId,
                        Product2 = productId,
                        Score    = scrore
                    });
                });

                if (listProductScore.Count > 500)
                {
                    FlushProducts(listProductScore, path);
                }
            }

            FlushProducts(listProductScore, path);

            //using (var fs = File.OpenWrite($"ds-calc-result-{DateTime.Now:yy-MM-dd-HH-mm}.txt"))
            //{
            //    JsonSerializer.Serialize(fs, listProductScore.OrderByDescending(x => x.Score).ToList());
            //}

            //JsonSerializer.Serialize(stream, p2);

            //File.WriteAllText($"ds-calc-result-{DateTime.Now:yy-MM-dd-HH-mm}.txt", JsonConvert.SerializeObject());

            Console.WriteLine("Finish");
        }
        public static void Utf8Json()
        {
            var deserializedObject = Utf8JsonSerializer.Deserialize <ImmutableSignUpDto>(SignUpScenario.Json, JsonSettings.Utf8JsonResolver);

            deserializedObject.Should().BeEquivalentTo(SignUpScenario.MutableObject);
        }
 public SignUpDto Utf8Json() => Utf8JsonSerializer.Deserialize <SignUpDto>(SignUpScenario.Json, JsonSettings.Utf8JsonResolver);