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

            content.ShouldNotBeNull();
            value.ShouldBe("This is the content.");
        }
Example #2
0
        public static T GetAsJson <T>(this IContentSet contentSet, string name,
                                      JsonSerializerOptions?serializerOptions = null)
        {
            if (contentSet is null)
            {
                throw new ArgumentNullException(nameof(contentSet));
            }

            string json = contentSet.GetAsString(name);

            return(JsonSerializer.Deserialize <T>(json, serializerOptions ?? JsonOptions.SerializerOptions));
        }
Example #3
0
        private static async Task <T?> GetJsonAsCustomListEntryInternal <T>(this IContentSet contentSet,
                                                                            string name,
                                                                            params object[] args)
            where T : class
        {
            string json = await contentSet.GetAsString(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 #4
0
        public async Task Can_inject_content_manager_and_retrieve_content()
        {
            var manager = _fixture.ServiceProvider.GetService <IContentManager>();

            manager.ShouldNotBeNull();

            IContentSet contentSet = manager.GetContentSet("Text");

            contentSet.ShouldNotBeNull();

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

            value.ShouldBe("This is the content.");
        }
Example #5
0
        private static async Task <T> GetAsJsonInternal <T>(this IContentSet contentSet,
                                                            string name,
                                                            JsonSerializerOptions?serializerOptions = null)
        {
            string json = await contentSet.GetAsString(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;

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

            return(result);
        }
Example #6
0
 public Task <string> GetAsString(string name)
 {
     return(_contentSet.GetAsString(name));
 }