Mega Code Archive

 
Categories / C# / Network
 

Removes Json null objects from the serialized string and return a new string(Extention Method)

// -------------------------------- // <copyright file="SharePageUtils.cs" company="Samaj Shekhar"> //     Copyright (c) 2010-2011 Samaj Shekhar // </copyright> // <author>Samaj Shekhar (samaj.shekhar@hotmail.com)</author> // <license>Released under the terms of the Microsoft Public License (Ms-PL)</license> // <website>http://thesharepage.codeplex.com</website> // --------------------------------- using System; using System.Web.Script.Serialization; using System.Text.RegularExpressions; namespace SharePage.CoreLib.Utils.ExtentionMethods {     /// <summary>     /// <para>Encapsulates the custom extention methods for Types</para>>     /// </summary>     /// <remarks>     /// Copyright: Copyright (c) 2010-2011 Samaj Shekhar     /// Last Update: 4-Jan-2011|4:46am     /// </remarks>     public static class Extentions     {         /// <summary>         /// Check wheather the string is empty or null (Extention Method)         /// </summary>         /// <param name="str">The string to be checked</param>         /// <returns>Returns true if string is NULL or String.Empty, otherwise false</returns>         public static bool IsEmptyOrNull(this string str)         {             if (str == null || str == string.Empty)             {                 return true;             }             return false;         }         /// <summary>         /// Removes Json null objects from the serialized string and return a new string(Extention Method)         /// </summary>         /// <param name="str">The String to be checked</param>         /// <returns>Returns the new processed string or NULL if null or empty string passed</returns>         public static string RemoveJsonNulls(this string str)         {             if (!str.IsEmptyOrNull())             {                 Regex regex = new Regex(UtilityRegExp.JsonNullRegEx);                 string data = regex.Replace(str, string.Empty);                 regex = new Regex(UtilityRegExp.JsonNullArrayRegEx);                 return regex.Replace(data, "[]");             }             return null;         }         /// <summary>         /// Serializes an objct to a Json string using Javascript Serializer and removes Json null objects if opted (Extention Method)         /// </summary>         /// <param name="arg">Object to serialize</param>         /// <param name="checknull">Set to true if you want remove null Json objects from serialized string, otherwise false(default)</param>         /// <returns>The serialized Json as string, if check null was set true then null Json strings are removed</returns>                 public static string SerializeToJson(this object arg, bool checknull = false)         {             JavaScriptSerializer sr = new JavaScriptSerializer();             if (checknull)             {                 return sr.Serialize(arg).RemoveJsonNulls();             }             return sr.Serialize(arg);                     }              }     /// <summary>     /// <para>Contains the Regular Expression for use in Application</para>>     /// </summary>     /// <remarks>     /// Copyright: Copyright (c) 2010-2011 Samaj Shekhar     /// Last Update: 30-Dec-2010|04:22pm     /// </remarks>     public class UtilityRegExp     {         /// <summary>         /// <para>A Regular Expression to Match any null Json object in a string</para>         /// <para>like "Samaj_Shekhar":null,</para>         /// <para>Useful in removing nulls from serialized Json string</para>         /// </summary>         public static string JsonNullRegEx = "[\"][a-zA-Z0-9_]*[\"]:null[ ]*[,]?";         /// <summary>         /// <para>A Regular Expression to Match an array of null Json object in a string</para>         /// <para>like [null, null]</para>         /// <para>Useful in removing null array from serialized Json string</para>         /// </summary>         public static string JsonNullArrayRegEx = "\\[( *null *,? *)*]";         /// <summary>         /// <para>A Regular Expression to Match an Email Address</para>         /// <para>Useful in validating email addresses</para>         /// </summary>         public static string ValidEmailRegEx = @"^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$";     } }