Example #1
0
        public async Task Able_to_load_embedded_resources()
        {
            IContentSet content = _contentManager.GetContentSet("Text");
            string      value   = await content.GetAsStringAsync("Content")
                                  .ConfigureAwait(false);

            content.ShouldNotBeNull();
            value.ShouldBe("This is the content.");
        }
Example #2
0
        private static async Task <T?> GetJsonAsCustomListEntryInternal <T>(this IContentSet contentSet,
                                                                            string name,
                                                                            params object[] args)
            where T : class
        {
            string json = await contentSet.GetAsStringAsync(name).ConfigureAwait(false);

            JsonDocument doc = JsonDocument.Parse(json, new JsonDocumentOptions
            {
                AllowTrailingCommas = true,
                CommentHandling     = JsonCommentHandling.Skip,
            });

            foreach (JsonElement item in doc.RootElement.EnumerateArray())
            {
                if (!item.TryGetProperty("Inputs", out JsonElement inputsProperty))
                {
                    throw new JsonException($"Cannot find Inputs property in the JSON item {item}.");
                }
                if (inputsProperty.ValueKind != JsonValueKind.Array)
                {
                    throw new JsonException($"The Inputs property must be an array in the JSON item {item}.");
                }
                if (inputsProperty.GetArrayLength() != args.Length)
                {
                    throw new JsonException($"Invalid number of arguments in the Inputs property for the JSON item {item}.");
                }

                bool inputsMatch             = true;
                List <JsonElement> inputArgs = inputsProperty.EnumerateArray().ToList();
                for (var i = 0; i < inputArgs.Count; i++)
                {
                    if (!inputArgs[i].ToString().Equals(args[i].ToString(), StringComparison.Ordinal))
                    {
                        inputsMatch = false;
                        break;
                    }
                }

                if (inputsMatch)
                {
                    if (!item.TryGetProperty("Data", out JsonElement dataProperty))
                    {
                        throw new JsonException($"Cannot find Data property in the JSON item {item}.");
                    }
                    T value = JsonSerializer.Deserialize <T>(dataProperty.GetRawText(), JsonOptions.SerializerOptions);
                    return(value);
                }
            }

            return(default);
Example #3
0
        public async Task Can_inject_content_manager_and_retrieve_content()
        {
            var manager = _fixture.ServiceProvider.GetService <IContentManager>();

            manager.ShouldNotBeNull();

            IContentSet contentSet = manager.GetContentSet <TextContentSet>();

            contentSet.ShouldNotBeNull();

            string value = await contentSet.GetAsStringAsync("Content").ConfigureAwait(false);

            value.ShouldBe("This is the content.");
        }
Example #4
0
        private static async Task <T> GetAsJsonAsyncInternal <T>(this IContentSet contentSet,
                                                                 string name,
                                                                 JsonSerializerOptions?serializerOptions = null)
        {
            string json = await contentSet.GetAsStringAsync(name).ConfigureAwait(false);

            using var ms = new MemoryStream();
#pragma warning disable CA2000 // Dispose objects before losing scope
            var writer = new StreamWriter(ms, Encoding.UTF8);
#pragma warning restore CA2000 // Dispose objects before losing scope
            writer.Write(json);
            writer.Flush();
            ms.Position = 0;

            return(await JsonSerializer.DeserializeAsync <T>(ms, serializerOptions ?? JsonOptions.SerializerOptions)
                   .ConfigureAwait(false));
        }