Skip to content

Latest commit

 

History

History
65 lines (60 loc) · 1.93 KB

SerializeWithLinq.md

File metadata and controls

65 lines (60 loc) · 1.93 KB

Serializing to JSON with LINQ

This sample uses LINQ to JSON to manually convert a .NET type to JSON.

public class BlogPost
{
    public string Title { get; set; }
    public string AuthorName { get; set; }
    public string AuthorTwitter { get; set; }
    public string Body { get; set; }
    public DateTime PostedDate { get; set; }
}

snippet source | anchor

var blogPosts = new List<BlogPost>
{
    new()
    {
        Title = "Json.NET is awesome!",
        AuthorName = "James Newton-King",
        AuthorTwitter = "JamesNK",
        PostedDate = new(2013, 1, 23, 19, 30, 0),
        Body = "<h3>Title!</h3><p>Content!</p>"
    }
};

var blogPostsArray = new JArray(
    blogPosts.Select(p => new JObject
    {
        {"Title", p.Title},
        {
            "Author", new JObject
            {
                {"Name", p.AuthorName},
                {"Twitter", p.AuthorTwitter}
            }
        },
        {"Date", p.PostedDate},
        {"BodyHtml", HttpUtility.HtmlEncode(p.Body)}
    })
);

Console.WriteLine(blogPostsArray.ToString());
// [
//   {
//     "Title": "Json.NET is awesome!",
//     "Author": {
//       "Name": "James Newton-King",
//       "Twitter": "JamesNK"
//     },
//     "Date": "2013-01-23T19:30:00",
//     "BodyHtml": "&lt;h3&gt;Title!&lt;/h3&gt;&lt;p&gt;Content!&lt;/p&gt;"
//   }
// ]

snippet source | anchor