1- namespace JsonFeedNet ;
1+ using System . Text . Json ;
2+
3+ namespace JsonFeedNet ;
24
35using System . Text ;
4- using Newtonsoft . Json ;
6+ using System . Text . Json . Serialization ;
57
68// ReSharper disable MemberCanBePrivate.Global
79// ReSharper disable UnusedMember.Global
810public class JsonFeed
911{
10- private static readonly JsonSerializerSettings s_serializerSettings = new ( )
11- {
12- Formatting = Formatting . Indented ,
13- NullValueHandling = NullValueHandling . Ignore ,
14- DateFormatHandling = DateFormatHandling . IsoDateFormat
15- } ;
16-
1712 /// <summary>
1813 /// The URL of the version of the format the feed uses.
1914 /// </summary>
20- [ JsonProperty ( "version" ) ]
15+ [ JsonPropertyName ( "version" ) ]
2116 public string Version { get ; set ; } = "https://jsonfeed.org/version/1.1" ; //required
2217
2318 /// <summary>
2419 /// The name of the feed.
2520 /// This will often correspond to the name of the website(blog, for instance).
2621 /// </summary>
27- [ JsonProperty ( "title" ) ]
22+ [ JsonPropertyName ( "title" ) ]
2823 public string Title { get ; set ; } //required
2924
3025 /// <summary>
3126 /// The URL of the resource that the feed describes.
3227 /// This resource may or may not actually be a “home” page, but it should be an HTML page.
3328 /// </summary>
34- [ JsonProperty ( "home_page_url" ) ]
29+ [ JsonPropertyName ( "home_page_url" ) ]
3530 public string HomePageUrl { get ; set ; } //optional
3631
3732 /// <summary>
3833 /// The URL of the feed.
3934 /// Serves as the unique identifier for the feed.
4035 /// </summary>
41- [ JsonProperty ( "feed_url" ) ]
36+ [ JsonPropertyName ( "feed_url" ) ]
4237 public string FeedUrl { get ; set ; } //optional
4338
4439 /// <summary>
4540 /// More detail, beyond the title, on what the feed is about.
4641 /// A feed reader may display this text.
4742 /// </summary>
48- [ JsonProperty ( "description" ) ]
43+ [ JsonPropertyName ( "description" ) ]
4944 public string Description { get ; set ; } //optional
5045
5146 /// <summary>
5247 /// Description of the purpose of the feed.
5348 /// This is for the use of people looking at the raw JSON, and should be ignored by feed readers.
5449 /// </summary>
55- [ JsonProperty ( "user_comment" ) ]
50+ [ JsonPropertyName ( "user_comment" ) ]
5651 public string UserComment { get ; set ; } //optional
5752
5853 /// <summary>
@@ -61,64 +56,64 @@ public class JsonFeed
6156 /// won’t use it very often.
6257 /// Must not be the same as FeedUrl, and it must not be the same as a previous NextUrl (to avoid infinite loops).
6358 /// </summary>
64- [ JsonProperty ( "next_url" ) ]
59+ [ JsonPropertyName ( "next_url" ) ]
6560 public string NextUrl { get ; set ; } //optional
6661
6762 /// <summary>
6863 /// The URL of an image for the feed suitable to be used in a timeline, much the way an avatar might be used.
6964 /// It should be square and relatively large — such as 512 x 512 — so that it can be scaled-down.
7065 /// It should use transparency where appropriate, since it may be rendered on a non-white background.
7166 /// </summary>
72- [ JsonProperty ( "icon" ) ]
67+ [ JsonPropertyName ( "icon" ) ]
7368 public string Icon { get ; set ; } //optional
7469
7570 /// <summary>
7671 /// The URL of an image for the feed suitable to be used in a source list.
7772 /// It should be square and relatively small, but not smaller than 64 x 64.
7873 /// It should use transparency where appropriate, since it may be rendered on a non-white background.
7974 /// </summary>
80- [ JsonProperty ( "favicon" ) ]
75+ [ JsonPropertyName ( "favicon" ) ]
8176 public string FavIcon { get ; set ; } //optional
8277
8378 /// <summary>
8479 /// The feed author.
8580 /// </summary>
86- [ JsonProperty ( "author" ) ]
81+ [ JsonPropertyName ( "author" ) ]
8782 [ Obsolete ( "obsolete by specification version 1.1. Use `Authors`" ) ]
8883 public JsonFeedAuthor Author { get ; set ; } //optional
8984
9085 /// <summary>
9186 /// Specifies one or more feed authors.
9287 /// </summary>
93- [ JsonProperty ( "authors" ) ]
88+ [ JsonPropertyName ( "authors" ) ]
9489 public List < JsonFeedAuthor > Authors { get ; set ; } //optional
9590
9691 /// <summary>
9792 /// Primary language for the feed in the format specified in RFC 5646.
9893 /// The value is usually a 2-letter language tag from ISO 639-1, optionally followed by a region tag.
9994 /// (Examples: en or en-US.)
10095 /// </summary>
101- [ JsonProperty ( "language" ) ]
96+ [ JsonPropertyName ( "language" ) ]
10297 public string Language { get ; set ; } //optional
10398
10499 /// <summary>
105100 /// Indicates whether or not the feed is finished — that is, whether or not it will ever update again.
106101 /// If the value is true, then it’s expired. Any other value, or the absence of expired, means the feed may continue to
107102 /// update.
108103 /// </summary>
109- [ JsonProperty ( "expired" ) ]
104+ [ JsonPropertyName ( "expired" ) ]
110105 public bool ? Expired { get ; set ; } //optional
111106
112107 /// <summary>
113108 /// Endpoints that can be used to subscribe to real-time notifications of changes to this feed.
114109 /// </summary>
115- [ JsonProperty ( "hubs" ) ]
110+ [ JsonPropertyName ( "hubs" ) ]
116111 public List < JsonFeedHub > Hubs { get ; set ; } //optional
117112
118113 /// <summary>
119114 /// The individual items in the feed.
120115 /// </summary>
121- [ JsonProperty ( "items" ) ]
116+ [ JsonPropertyName ( "items" ) ]
122117 public List < JsonFeedItem > Items { get ; set ; } //required
123118
124119 /// <summary>
@@ -128,7 +123,7 @@ public class JsonFeed
128123 /// <returns>A JsonFeed object representing the parsed feed.</returns>
129124 public static JsonFeed Parse ( string jsonFeedString )
130125 {
131- return JsonConvert . DeserializeObject < JsonFeed > ( jsonFeedString ) ;
126+ return JsonSerializer . Deserialize < JsonFeed > ( jsonFeedString , SourceGenerationContext . Default . JsonFeed ) ;
132127 }
133128
134129 /// <summary>
@@ -142,7 +137,14 @@ public static async Task<JsonFeed> ParseFromUriAsync(Uri jsonFeedUri, HttpMessag
142137 HttpClient client = new ( httpMessageHandler ?? new HttpClientHandler ( ) ) ;
143138 string jsonDocument = await client . GetStringAsync ( jsonFeedUri ) ;
144139
145- return Parse ( jsonDocument ) ;
140+ try
141+ {
142+ return Parse ( jsonDocument ) ;
143+ }
144+ finally
145+ {
146+ client . Dispose ( ) ;
147+ }
146148 }
147149
148150 /// <summary>
@@ -172,6 +174,6 @@ public void Write(Stream stream)
172174 /// <returns>A string containing the generated feed JSON.</returns>
173175 public override string ToString ( )
174176 {
175- return JsonConvert . SerializeObject ( this , s_serializerSettings ) ;
177+ return JsonSerializer . Serialize ( this , SourceGenerationContext . Default . JsonFeed ) ;
176178 }
177179}
0 commit comments