Ejemplo n.º 1
0
    public void WriteSerializedValue(object value, params string[] path)
    {
        if (path == null)
        {
            throw new ArgumentNullException(nameof(path));
        }

        var filename = Path.Combine(DataPath, Path.Combine(path));

        if (!filename.EndsWith(JsonExtension, StringComparison.Ordinal))
        {
            filename += JsonExtension;
        }

        var directory = Path.GetDirectoryName(filename);

        Directory.CreateDirectory(directory);

        if (value == null)
        {
            File.WriteAllBytes(filename, Array.Empty <byte>());
            return;
        }

        var json = _jsonSerializerService.Serialize(value);

        File.WriteAllText(filename, json, Encoding.UTF8);
    }
    public async Task Run()
    {
        var data = await PostcodeScraper.Run();

        File.Delete(DataLocations.LocalitiesPath);
        JsonSerializerService.Serialize(data, DataLocations.LocalitiesPath);
        await Verify(data.Take(10));
    }
Ejemplo n.º 3
0
        public async void SerializationTest1()
        {
            var logServiceMoq        = new Mock <ILogService>();
            var serializationService = new JsonSerializerService(logServiceMoq.Object);

            var fakeCvService = new FakeCvService();
            var cvs           = await fakeCvService.GetAllCvs();

            var cv = cvs.ElementAt(0);

            var json = await serializationService.Serialize(cv);

            Console.WriteLine(json);
        }
Ejemplo n.º 4
0
    public void UnwrapContext(IDictionary <object, object> dictionary, HttpContext context)
    {
        if (context is null)
        {
            throw new ArgumentNullException(nameof(context));
        }

        var statusCode = Convert.ToInt32(dictionary.GetValueOrDefault("status_code", (int)HttpStatusCode.NotFound), CultureInfo.InvariantCulture);

        context.Response.StatusCode = statusCode;

        if (dictionary.GetValueOrDefault("headers") is IDictionary <object, object> headers)
        {
            foreach (var header in headers)
            {
                var headerKey   = Convert.ToString(header.Key, CultureInfo.InvariantCulture);
                var headerValue = new StringValues(Convert.ToString(header.Value, CultureInfo.InvariantCulture));
                context.Response.Headers.TryAdd(headerKey, headerValue);
            }
        }

        if (dictionary.GetValueOrDefault("content") is IDictionary <object, object> content)
        {
            var type = Convert.ToString(content.GetValueOrDefault("type"), CultureInfo.InvariantCulture);

            byte[] contentBuffer = null;
            if (content.GetValueOrDefault("data") is IDictionary jsonData)
            {
                var serializedData = _jsonSerializerService.Serialize(jsonData);
                contentBuffer = Encoding.UTF8.GetBytes(serializedData);
                type          = "application/json";
            }
            else if (content.GetValueOrDefault("data") is IEnumerable list)
            {
                contentBuffer = list.Select(Convert.ToByte).ToArray();
            }

            if (!string.IsNullOrEmpty(type))
            {
                context.Response.ContentType = type;
            }

            if (contentBuffer?.Length > 0)
            {
                context.Response.Body.Write(contentBuffer, 0, contentBuffer.Length);
            }
        }
    }
Ejemplo n.º 5
0
        public void UnwrapContext(WirehomeDictionary dictionary, HttpContext context)
        {
            var statusCode = Convert.ToInt32(DictionaryExtensions.GetValueOrDefault(dictionary, "status_code", 200), CultureInfo.InvariantCulture);

            context.Response.StatusCode = statusCode;

            if (dictionary.GetValueOrDefault(dictionary, "headers") is WirehomeDictionary headers)
            {
                foreach (var header in headers)
                {
                    var headerValue = new StringValues(Convert.ToString(header.Value, CultureInfo.InvariantCulture));
                    context.Response.Headers.TryAdd(header.Key, headerValue);
                }
            }

            if (dictionary.GetValueOrDefault("content") is WirehomeDictionary content)
            {
                var type = Convert.ToString(content.GetValueOrDefault("type"), CultureInfo.InvariantCulture);

                byte[] contentBuffer = null;
                if (content.GetValueOrDefault("data") is IDictionary jsonData)
                {
                    var serializedData = _jsonSerializerService.Serialize(jsonData);
                    contentBuffer = Encoding.UTF8.GetBytes(serializedData);
                    type          = "application/json";
                }
                else if (content.GetValueOrDefault("data") is IEnumerable list)
                {
                    contentBuffer = list.Select(Convert.ToByte).ToArray();
                }

                if (!string.IsNullOrEmpty(type))
                {
                    context.Response.ContentType = type;
                }

                if (contentBuffer?.Length > 0)
                {
                    context.Response.Body.Write(contentBuffer, 0, contentBuffer.Length);
                }
            }
        }
Ejemplo n.º 6
0
    public static async Task Run()
    {
        var codes = await PartyCodeScraper.Run();

        var htmlPath          = Path.Combine(DataLocations.TempPath, "partycodes.html");
        var partyRegisterPath = Path.Combine(DataLocations.TempPath, "partyRegister.json");

        File.Delete(htmlPath);
        File.Delete(partyRegisterPath);
        var url = "https://www.aec.gov.au/parties_and_representatives/party_registration/Registered_parties/";

        try
        {
            await Downloader.DownloadFile(htmlPath, url);

            var jsonUrl = (await File.ReadAllLinesAsync(htmlPath))
                          .Single(x => x.Contains("/Parties_and_Representatives/Party_Registration/Registered_parties/files/register"))
                          .Split('"')[1];
            await Downloader.DownloadFile(partyRegisterPath, $"https://www.aec.gov.au{jsonUrl}");

            var          aecParties = JsonSerializerService.Deserialize <PartyData>(partyRegisterPath);
            List <Party> parties    = new();
            foreach (var detail in aecParties.Details)
            {
                var party = DetailToParty(detail, codes);
                parties.Add(party);
            }

            File.Delete(DataLocations.PartiesJsonPath);
            JsonSerializerService.Serialize(parties, DataLocations.PartiesJsonPath);
        }
        catch (Exception exception)
        {
            throw new($"Failed to parse {htmlPath} {htmlPath}", exception);
        }
    }