From 61b2691e94b188fc6a35a5b4cee3259d9633fbd4 Mon Sep 17 00:00:00 2001 From: Dan Rigby <114417+DanRigby@users.noreply.github.com> Date: Wed, 16 Oct 2024 11:52:16 -0400 Subject: [PATCH] Add support for custom objects and fields Fixes #1 Add support for custom objects and fields in JSON feed. * Add `CustomObjects` property to `JsonFeed` and `JsonFeedItem` classes to store custom objects. * Update `Simple.json` test resource to include examples of custom objects with names starting with an underscore and alphanumeric member keys. * Add test in `JsonFeedTests.cs` to verify the handling of custom objects in the feed. --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/DanRigby/JsonFeed.NET/issues/1?shareId=XXXX-XXXX-XXXX-XXXX). --- JsonFeedNet.Tests/JsonFeedTests.cs | 15 +++++++++ JsonFeedNet.Tests/Resources/Simple.json | 44 +++++++++++++++---------- JsonFeedNet/JsonFeed.cs | 6 ++++ JsonFeedNet/JsonFeedItem.cs | 7 ++++ 4 files changed, 54 insertions(+), 18 deletions(-) diff --git a/JsonFeedNet.Tests/JsonFeedTests.cs b/JsonFeedNet.Tests/JsonFeedTests.cs index 7792e34..fb28e4d 100644 --- a/JsonFeedNet.Tests/JsonFeedTests.cs +++ b/JsonFeedNet.Tests/JsonFeedTests.cs @@ -174,4 +174,19 @@ public void WriteFeedToStream() var jsonFeed2 = JsonFeed.Parse(outputJsonFeed); Assert.Equivalent(jsonFeed, jsonFeed2); } + + [Fact] + public void CustomObjectsHandling() + { + string inputJsonFeed = TestExtensions.GetResourceAsString("Simple.json").NormalizeEndings(); + JsonFeed jsonFeed = JsonFeed.Parse(inputJsonFeed); + + Assert.True(jsonFeed.CustomObjects.ContainsKey("_custom_feed_object")); + Assert.Equal("custom_feed_value1", jsonFeed.CustomObjects["_custom_feed_object"].GetProperty("custom_feed_key1").GetString()); + Assert.Equal("custom_feed_value2", jsonFeed.CustomObjects["_custom_feed_object"].GetProperty("custom_feed_key2").GetString()); + + Assert.True(jsonFeed.Items[0].CustomObjects.ContainsKey("_custom_object")); + Assert.Equal("custom_value1", jsonFeed.Items[0].CustomObjects["_custom_object"].GetProperty("custom_key1").GetString()); + Assert.Equal("custom_value2", jsonFeed.Items[0].CustomObjects["_custom_object"].GetProperty("custom_key2").GetString()); + } } diff --git a/JsonFeedNet.Tests/Resources/Simple.json b/JsonFeedNet.Tests/Resources/Simple.json index 30ca371..4ea7f7b 100644 --- a/JsonFeedNet.Tests/Resources/Simple.json +++ b/JsonFeedNet.Tests/Resources/Simple.json @@ -1,18 +1,26 @@ -{ - "version": "https://jsonfeed.org/version/1", - "title": "My Example Feed", - "home_page_url": "https://example.org/", - "feed_url": "https://example.org/feed.json", - "items": [ - { - "id": "2", - "url": "https://example.org/second-item", - "content_text": "This is a second item." - }, - { - "id": "1", - "url": "https://example.org/initial-post", - "content_html": "

Hello, world!

" - } - ] -} \ No newline at end of file +{ + "version": "https://jsonfeed.org/version/1", + "title": "My Example Feed", + "home_page_url": "https://example.org/", + "feed_url": "https://example.org/feed.json", + "items": [ + { + "id": "2", + "url": "https://example.org/second-item", + "content_text": "This is a second item.", + "_custom_object": { + "custom_key1": "custom_value1", + "custom_key2": "custom_value2" + } + }, + { + "id": "1", + "url": "https://example.org/initial-post", + "content_html": "

Hello, world!

" + } + ], + "_custom_feed_object": { + "custom_feed_key1": "custom_feed_value1", + "custom_feed_key2": "custom_feed_value2" + } +} diff --git a/JsonFeedNet/JsonFeed.cs b/JsonFeedNet/JsonFeed.cs index 27fb133..fa5e4e5 100644 --- a/JsonFeedNet/JsonFeed.cs +++ b/JsonFeedNet/JsonFeed.cs @@ -119,6 +119,12 @@ public class JsonFeed [JsonPropertyName("items")] public List Items { get; set; } //required + /// + /// Custom objects in the feed. + /// + [JsonExtensionData] + public Dictionary CustomObjects { get; set; } = new(); //optional + /// /// Parses a JsonFeed in an input string into a JsonFeed object for use by code. /// diff --git a/JsonFeedNet/JsonFeedItem.cs b/JsonFeedNet/JsonFeedItem.cs index 64c0dc1..53152d3 100644 --- a/JsonFeedNet/JsonFeedItem.cs +++ b/JsonFeedNet/JsonFeedItem.cs @@ -1,5 +1,6 @@ namespace JsonFeedNet; +using System.Text.Json; using System.Text.Json.Serialization; /// @@ -124,4 +125,10 @@ public class JsonFeedItem /// [JsonPropertyName("attachments")] public List Attachments { get; set; } //optional + + /// + /// Custom objects in the feed item. + /// + [JsonExtensionData] + public Dictionary CustomObjects { get; set; } = new(); //optional }