Skip to content

Latest commit

 

History

History
35 lines (30 loc) · 1.31 KB

SerializeWithJsonSerializerToFile.md

File metadata and controls

35 lines (30 loc) · 1.31 KB

Serialize JSON to a file

This sample serializes JSON to a file.

public class Movie
{
    public string Name { get; set; }
    public int Year { get; set; }
}

snippet source | anchor

var movie = new Movie
{
    Name = "Bad Boys",
    Year = 1995
};

// serialize JSON to a string and then write string to a file
File.WriteAllText(@"c:\movie.json", JsonConvert.SerializeObject(movie));

// serialize JSON directly to a file
using var file = File.CreateText(@"c:\movie.json");
var serializer = new JsonSerializer();
serializer.Serialize(file, movie);

snippet source | anchor