Here is my Json file:
{
"blogPosts": [
{
"id": 1,
"date": "2019-11-11T18:11:22.511Z",
"title": "title1",
"image": "https://www.imageExample.com/static/ae8188adb9e0f13c40fce50bd773bc51/a6b7d/Content-considerations.jpg",
"htmlContent": "htmlExample",
"comments": [
{
"name": "Joe Bloggs",
"date": "2019-11-11T20:44:01.000Z",
"emailAddress": "[email protected]",
"message": "Sed vel odio consequat, elementum massa quis, lobortis est. Nulla egestas congue dolor, sit amet fermentum massa dignissim sit amet. In vestibulum iaculis egestas."
},
{
"name": "John Smith",
"date": "2019-11-13T09:00:23.533Z",
"emailAddress": "[email protected]",
"message": "Nam vel aliquet nulla, ac tempor ex. Suspendisse sit amet sollicitudin ex, vel placerat ipsum. Duis vitae fermentum eros. In maximus maximus purus, et volutpat eros rutrum et. Nulla fringilla at massa vel varius. In tristique egestas nisl, vitae elementum orci fringilla quis. Ut rutrum mauris erat, a rhoncus orci posuere non. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus."
},
{
"name": "Jack Black",
"date": "2019-11-11T19:22:22.511Z",
"emailAddress": "[email protected]",
"message": "Integer volutpat, sapien eu dapibus sodales, ipsum arcu dapibus elit, ac faucibus mi ligula suscipit mauris."
}
]
},
{
"id": 2,
"date": "2019-11-01T01:21:39.123Z",
"title": "title2",
"image": "https://www.imageExample.com/static/251940c537d045417ef75286eb970948/f9a6c/Ben.jpg",
"htmlContent": "htmlExample"
},
{
"id": 3,
"date": "2019-10-28T14:53:09.511Z",
"title": "title3",
"image": "https://www.imageExample.com/static/026bfc5011b0f64f2b912fd1d0ef87ae/f9a6c/brno.jpg",
"htmlContent": "htmlExample"
}
]
}
I am trying to deserialize the file into these classes:
public class BlogRoot
{
[JsonProperty("blogPosts")]
public List<BlogPost> BlogPosts {get;set;}
}
public class Comment
{
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("date")]
public DateTime Date { get; set; }
[JsonProperty("emailAddress")]
public string EmailAddress { get; set; }
[JsonProperty("message")]
public string Message { get; set; }
}
public class BlogPost
{
[JsonProperty("id")]
public int Id { get; set; }
[JsonProperty("date")]
public DateTime Date { get; set; }
[JsonProperty("title")]
public string Title { get; set; }
[JsonProperty("image")]
public Uri Image { get; set; }
[JsonProperty("htmlContent")]
public string HtmlContent { get; set; }
[JsonProperty("comments", NullValueHandling = NullValueHandling.Ignore)]
public List<Comment> Comments { get; set; }
public BlogPost(int id)
{
LoadBlogPost(id);
}
private List<BlogPost> LoadBlogPost(int id)
{
string path = HostingEnvironment.MapPath(@"~\App_Data\Blog-Posts - Copy.json");
string jsonFromFile;
using (StreamReader reader = File.OpenText(path))
{
jsonFromFile = reader.ReadToEnd();
var root = JsonConvert.DeserializeObject<BlogRoot>(jsonFromFile);
return root.BlogPosts;
}
}
}
When I try to deserialize the Json file I get a StackOverflowException, and I have no idea why. I can't find any solutions online and I've been tearing my hair out trying to figure out what the cause is, probably something really obvious that I've completely overlooked. Funnily enough, I can deserialize the Json to a dynamic array, but I just want to be able to deserialize to these classes. Any ideas?
Call Stack:
> Web.dll!Web.Business.BlogPost.LoadBlogPost(int id) Line 39 C#
Web.dll!Web.Business.BlogPost.BlogPost(int id) Line 34 C#
[External Code]
Web.dll!Web.Business.BlogPost.LoadBlogPost(int id) Line 47 C#
Web.dll!Web.Business.BlogPost.BlogPost(int id) Line 34 C#
[External Code]
Web.dll!Web.Business.BlogPost.LoadBlogPost(int id) Line 47 C#
Web.dll!Web.Business.BlogPost.BlogPost(int id) Line 34 C#
[External Code]
Web.dll!Web.Business.BlogPost.LoadBlogPost(int id) Line 47 C#
Web.dll!Web.Business.BlogPost.BlogPost(int id) Line 34 C#
[External Code]
Web.dll!Web.Business.BlogPost.LoadBlogPost(int id) Line 47 C#
It's longer than this but it just repeats these lines.
Error message is literally just System.StackOverflowException, no extra details.
DeserializeObjectand then into the JSON.NET lib?LoadBlogPostand see what is going on. You'll be surprised.