Skip to content

Commit

Permalink
Add support for custom objects and fields
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
DanRigby committed Oct 16, 2024
1 parent a2e33dd commit 61b2691
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 18 deletions.
15 changes: 15 additions & 0 deletions JsonFeedNet.Tests/JsonFeedTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
44 changes: 26 additions & 18 deletions JsonFeedNet.Tests/Resources/Simple.json
Original file line number Diff line number Diff line change
@@ -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": "<p>Hello, world!</p>"
}
]
}
{
"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": "<p>Hello, world!</p>"
}
],
"_custom_feed_object": {
"custom_feed_key1": "custom_feed_value1",
"custom_feed_key2": "custom_feed_value2"
}
}
6 changes: 6 additions & 0 deletions JsonFeedNet/JsonFeed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ public class JsonFeed
[JsonPropertyName("items")]
public List<JsonFeedItem> Items { get; set; } //required

/// <summary>
/// Custom objects in the feed.
/// </summary>
[JsonExtensionData]
public Dictionary<string, JsonElement> CustomObjects { get; set; } = new(); //optional

/// <summary>
/// Parses a JsonFeed in an input string into a JsonFeed object for use by code.
/// </summary>
Expand Down
7 changes: 7 additions & 0 deletions JsonFeedNet/JsonFeedItem.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace JsonFeedNet;

using System.Text.Json;
using System.Text.Json.Serialization;

/// <summary>
Expand Down Expand Up @@ -124,4 +125,10 @@ public class JsonFeedItem
/// </summary>
[JsonPropertyName("attachments")]
public List<JsonFeedAttachment> Attachments { get; set; } //optional

/// <summary>
/// Custom objects in the feed item.
/// </summary>
[JsonExtensionData]
public Dictionary<string, JsonElement> CustomObjects { get; set; } = new(); //optional
}

0 comments on commit 61b2691

Please sign in to comment.