Eliminare campi nulli nella serializzazione Json

Classe Helper per gestire nella serializzazione di un oggetto in formato json l'eliminazione di eventuali campi nulli

   1:  public static class JsonHelper
   2:      {
   3:          public static bool IsEmptyOrNull(this string str)
   4:          {
   5:              if (str == null || str == string.Empty)
   6:              {
   7:                  return true;
   8:              }
   9:              return false;
  10:          }
  11:   
  12:          public static string RemoveJsonNulls(this string str)
  13:          {
  14:              if (!str.IsEmptyOrNull())
  15:              {
  16:                  Regex regex = new Regex(UtilityRegExp.JsonNullRegEx);
  17:                  string data = regex.Replace(str, string.Empty);
  18:                  regex = new Regex(UtilityRegExp.JsonNullArrayRegEx);
  19:                  return regex.Replace(data, "[]");
  20:              }
  21:              return null;
  22:          }
  23:   
  24:          public static string SerializeToJson(this object arg, bool checknull = false)
  25:          {
  26:              JavaScriptSerializer sr = new JavaScriptSerializer();
  27:              if (checknull)
  28:              {
  29:                  return sr.Serialize(arg).RemoveJsonNulls();
  30:              }
  31:              return sr.Serialize(arg);
  32:          }
  33:   
  34:      }
  35:   
  36:     
  37:      public class UtilityRegExp
  38:      {
  39:          public static string JsonNullRegEx = "[\"][a-zA-Z0-9_]*[\"]:null[ ]*[,]?";
  40:          public static string JsonNullArrayRegEx = "\\[( *null *,? *)*]";
  41:      }

posted @ venerdì 23 marzo 2012 13:02

Print

Comments on this entry:

# re: Eliminare campi nulli nella serializzazione Json

Left by iren at 27/06/2012 11:45
Gravatar
You should also UGG try doing something only for yourself, something totally selfish. This could be something you wanted for a long time: maybe a short trip, a coat, a ring or anything that could lift up your mood. This doesn't have to be extravagant nor costly. The point of all this is being good to yourself. Not only giving presents to yourself can lift up your mood. Buying things to others may be a perfect way to feel happy. UGG 新作 Think of your friends, family,
relatives or even local charities,choose one person and give him a gift. It doesn't have to be something expensive nor big. It may be just a little toy to your poor neighbor's child. Making another person feel happy will make you feel good in your heart and mind.
Meeting a positive person should be very useful. アグ Just try to keep your problems off the conversation. The idea here is to get out of your shell and have some interaction. Get involved with someone else and your mind will take a turn UGGブーツ as well

# re: Eliminare campi nulli nella serializzazione Json

Left by http://www.fcherstal.be at 27/08/2015 14:13
Gravatar
Classe Helper per gestire nella serializzazione di un oggetto in formato json l'eliminazione di eventuali campi nulli

# gamatire

Left by gamatire at 01/02/2017 10:49
Gravatar
Comments have been closed on this topic.
«dicembre»
domlunmarmergiovensab
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234