Created
April 29, 2023 17:09
-
-
Save icodeintx/077809a8d6ac5c2b50eab771fbb49d81 to your computer and use it in GitHub Desktop.
I Can JSON Interface with Implementation
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 Newtonsoft.Json; | |
namespace myproject | |
{ | |
public interface ICanJson | |
{ | |
// marker | |
} | |
public static class JsonExtensions | |
{ | |
/// <summary> | |
/// ToJSON | |
/// </summary> | |
/// <param name="obj"></param> | |
/// <returns></returns> | |
public static string ToJson(this ICanJson obj) | |
{ | |
return JsonConvert.SerializeObject(obj); | |
} | |
/// <summary> | |
/// ToJSONFormatted | |
/// </summary> | |
/// <param name="obj"></param> | |
/// <returns></returns> | |
public static string ToJsonFormatted(this ICanJson obj) | |
{ | |
return JsonConvert.SerializeObject(obj, Formatting.Indented); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment