Created
September 25, 2013 04:20
-
-
Save PaulGreenwell/6695131 to your computer and use it in GitHub Desktop.
Sample C# extension to serialise a class to JSON
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.IO; | |
using Newtonsoft.Json; | |
using Newtonsoft.Json.Converters; | |
public static class JsonExtensions | |
{ | |
public static string ToJson(this object entity) | |
{ | |
var serializer = new JsonSerializer(); | |
serializer.Converters.Add(new StringEnumConverter()); | |
using (var writer = new StringWriter()) | |
{ | |
serializer.Serialize(writer, entity); | |
return writer.ToString(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment