Skip to content

Instantly share code, notes, and snippets.

@brainded
Last active September 16, 2018 17:18

Revisions

  1. brainded revised this gist Dec 1, 2014. 1 changed file with 7 additions and 5 deletions.
    12 changes: 7 additions & 5 deletions JsonExtensions.cs
    Original file line number Diff line number Diff line change
    @@ -4,6 +4,11 @@ public static class ObjectExtensions
    /// The string representation of null.
    /// </summary>
    private static readonly string Null = "null";

    /// <summary>
    /// The string representation of exception.
    /// </summary>
    private static readonly string Exception = "Exception";

    /// <summary>
    /// To json.
    @@ -12,10 +17,7 @@ public static class ObjectExtensions
    /// <returns>The Json of any object.</returns>
    public static string ToJson(this object value)
    {
    if (value == null)
    {
    return Null;
    }
    if (value == null) return Null;

    try
    {
    @@ -25,7 +27,7 @@ public static string ToJson(this object value)
    catch (Exception exception)
    {
    //log exception but dont throw one
    return string.Empty;
    return Exception;
    }
    }
    }
  2. brainded renamed this gist Jan 31, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. brainded created this gist Jan 29, 2014.
    31 changes: 31 additions & 0 deletions ObjectExtensions.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    public static class ObjectExtensions
    {
    /// <summary>
    /// The string representation of null.
    /// </summary>
    private static readonly string Null = "null";

    /// <summary>
    /// To json.
    /// </summary>
    /// <param name="value">The value.</param>
    /// <returns>The Json of any object.</returns>
    public static string ToJson(this object value)
    {
    if (value == null)
    {
    return Null;
    }

    try
    {
    string json = JsonConvert.SerializeObject(value);
    return json;
    }
    catch (Exception exception)
    {
    //log exception but dont throw one
    return string.Empty;
    }
    }
    }