Exemplo n.º 1
0
 public static string SerializeObjectToJsonString <T>(T obj, bool isPrettyFormat = false, bool isBestEffort = true)
 {
     try
     {
         DataContractJsonSerializer dataContractJsonSerializer = new DataContractJsonSerializer(typeof(T));
         using (MemoryStream memoryStream = new MemoryStream())
         {
             dataContractJsonSerializer.WriteObject(memoryStream, obj);
             string text = Encoding.Default.GetString(memoryStream.ToArray());
             if (isPrettyFormat)
             {
                 text = Utils.FormatJson(text, 3);
             }
             return(text);
         }
     }
     catch
     {
         if (!isBestEffort)
         {
             throw;
         }
     }
     return(null);
 }