TestsEvents.cs 1.7 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.Reflection;
  7. using Xunit;
  8. namespace TestCases
  9. {
  10. [Json]
  11. struct StructEvents : IJsonLoaded, IJsonLoading, IJsonLoadField, IJsonWriting, IJsonWritten
  12. {
  13. public int IntField;
  14. [JsonExclude] public bool loading;
  15. [JsonExclude] public bool loaded;
  16. [JsonExclude] public bool fieldLoaded;
  17. void IJsonLoaded.OnJsonLoaded(IJsonReader r)
  18. {
  19. loaded = true;
  20. }
  21. void IJsonLoading.OnJsonLoading(IJsonReader r)
  22. {
  23. loading = true;
  24. }
  25. bool IJsonLoadField.OnJsonField(IJsonReader r, string key)
  26. {
  27. fieldLoaded = true;
  28. return false;
  29. }
  30. void IJsonWriting.OnJsonWriting(IJsonWriter w)
  31. {
  32. w.WriteRaw("/* OnJsonWriting */");
  33. }
  34. void IJsonWritten.OnJsonWritten(IJsonWriter w)
  35. {
  36. w.WriteRaw("/* OnJsonWritten */");
  37. }
  38. }
  39. [Obfuscation(Exclude = true, ApplyToMembers = true)]
  40. public class TestsEvents
  41. {
  42. [Fact]
  43. public void TestStructLoadEvents()
  44. {
  45. var o2 = Json.Parse<StructEvents>("{\"IntField\":23}");
  46. Assert.True(o2.loading);
  47. Assert.True(o2.loaded);
  48. Assert.True(o2.fieldLoaded);
  49. }
  50. [Fact]
  51. public void TestStructWriteEvents()
  52. {
  53. var o = new StructEvents();
  54. o.IntField = 23;
  55. var json = Json.Format(o);
  56. Assert.Contains("OnJsonWriting", json);
  57. Assert.Contains("OnJsonWritten", json);
  58. }
  59. }
  60. }