TestExcludeIfNull.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Topten.JsonKit;
  6. using System.IO;
  7. using System.Reflection;
  8. using Xunit;
  9. namespace TestCases
  10. {
  11. [Obfuscation(Exclude = true, ApplyToMembers = true)]
  12. public class TestExcludeIfNull
  13. {
  14. class Thing
  15. {
  16. [Json("field", ExcludeIfNull = true)]
  17. public string Field;
  18. [Json("property", ExcludeIfNull = true)]
  19. public string Property { get; set; }
  20. [Json("nfield", ExcludeIfNull = true)]
  21. public int? NField;
  22. [Json("nproperty", ExcludeIfNull = true)]
  23. public int? NProperty { get; set; }
  24. }
  25. [Fact]
  26. public void TestDoesntWriteNull()
  27. {
  28. var thing = new Thing();
  29. // Save it
  30. var json = Json.Format(thing);
  31. // Check the object kinds were written out
  32. Assert.DoesNotContain("\"field\":", json);
  33. Assert.DoesNotContain("\"property\":", json);
  34. Assert.DoesNotContain("\"nfield\":", json);
  35. Assert.DoesNotContain("\"nproperty\":", json);
  36. }
  37. [Fact]
  38. public void TestDoesWriteNonNull()
  39. {
  40. var thing = new Thing()
  41. {
  42. Field = "blah",
  43. Property = "deblah",
  44. NField = 23,
  45. NProperty = 24,
  46. };
  47. // Save it
  48. var json = Json.Format(thing);
  49. // Check the object kinds were written out
  50. Assert.Contains("\"field\":", json);
  51. Assert.Contains("\"property\":", json);
  52. Assert.Contains("\"nfield\":", json);
  53. Assert.Contains("\"nproperty\":", json);
  54. Assert.Contains("\"blah\"", json);
  55. Assert.Contains("\"deblah\"", json);
  56. Assert.Contains("23", json);
  57. Assert.Contains("24", json);
  58. }
  59. }
  60. }