Skip to content

Latest commit

 

History

History
40 lines (35 loc) · 1.4 KB

SerializeUnindentedJson.md

File metadata and controls

40 lines (35 loc) · 1.4 KB

Serialize Unindented JSON

This sample serializes an object to JSON without any formatting or indentation whitespace.

public class Account
{
    public string Email { get; set; }
    public bool Active { get; set; }
    public DateTime CreatedDate { get; set; }
    public IList<string> Roles { get; set; }
}

snippet source | anchor

var account = new Account
{
    Email = "[email protected]",
    Active = true,
    CreatedDate = new(2013, 1, 20, 0, 0, 0, DateTimeKind.Utc),
    Roles =
    [
        "User",
        "Admin"
    ]
};

var json = JsonConvert.SerializeObject(account);
// {"Email":"[email protected]","Active":true,"CreatedDate":"2013-01-20T00:00:00Z","Roles":["User","Admin"]}

Console.WriteLine(json);

snippet source | anchor