bean.sbn 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System.Collections.Generic;
  2. using SimpleJSON;
  3. using Luban;
  4. {{namespace_with_grace_begin __namespace_with_top_module}}
  5. {{~if __this.comment != '' ~}}
  6. /// <summary>
  7. /// {{escape_comment __this.comment}}
  8. /// </summary>
  9. {{~end~}}
  10. public {{class_modifier __bean}} class {{__name}} : {{if __parent_def_type}} {{__this.parent}} {{else}} Luban.EditorBeanBase {{end}}
  11. {
  12. public {{__name}}()
  13. {
  14. {{~ for field in __fields ~}}
  15. {{~if (need_init field.ctype) && !field.ctype.is_nullable ~}}
  16. {{format_field_name __code_style field.name}} = {{init_value field.ctype}};
  17. {{~end~}}
  18. {{~end~}}
  19. }
  20. {{~if !__this.is_abstract_type~}}
  21. public override void LoadJson(SimpleJSON.JSONObject _json)
  22. {
  23. {{~ for field in __hierarchy_fields ~}}
  24. {
  25. var _fieldJson = _json["{{field.name}}"];
  26. if (_fieldJson != null)
  27. {
  28. {{deserialize '_fieldJson' (format_field_name __code_style field.name) field.ctype}}
  29. }
  30. }
  31. {{~end~}}
  32. }
  33. public override void SaveJson(SimpleJSON.JSONObject _json)
  34. {
  35. {{~if parent~}}
  36. _json["$type"] = "{{__this.full_name}}";
  37. {{~end~}}
  38. {{~ for field in __hierarchy_fields ~}}
  39. {{~if field.ctype.is_nullable}}
  40. if ({{format_field_name __code_style field.name}} != null)
  41. {
  42. {{serialize '_json' field.name (format_field_name __code_style field.name) field.ctype}}
  43. }
  44. {{~else~}}
  45. {
  46. {{~if (is_raw_nullable field.ctype)}}
  47. if ({{format_field_name __code_style field.name}} == null) { throw new System.ArgumentNullException(); }
  48. {{~end~}}
  49. {{serialize '_json' field.name (format_field_name __code_style field.name) field.ctype}}
  50. }
  51. {{~end~}}
  52. {{~end~}}
  53. }
  54. {{~end~}}
  55. public static {{__name}} LoadJson{{__name}}(SimpleJSON.JSONNode _json)
  56. {
  57. {{~if __this.is_abstract_type~}}
  58. string type = _json["$type"];
  59. {{__name}} obj;
  60. switch (type)
  61. {
  62. {{~for child in __this.hierarchy_not_abstract_children~}}
  63. {{~if child.namespace == __this.namespace && __this.namespace != '' ~}}
  64. case "{{child.full_name}}":
  65. {{~end~}}
  66. case "{{impl_data_type child __this}}":obj = new {{child.full_name}}(); break;
  67. {{~end~}}
  68. default: throw new SerializationException();
  69. }
  70. {{~else~}}
  71. {{__name}} obj = new {{__this.full_name}}();
  72. {{~end~}}
  73. obj.LoadJson((SimpleJSON.JSONObject)_json);
  74. return obj;
  75. }
  76. public static void SaveJson{{__name}}({{__name}} _obj, SimpleJSON.JSONNode _json)
  77. {
  78. {{~if __this.is_abstract_type~}}
  79. _json["$type"] = _obj.GetType().Name;
  80. {{~end~}}
  81. _obj.SaveJson((SimpleJSON.JSONObject)_json);
  82. }
  83. {{~ for field in __fields ~}}
  84. {{~if field.comment != '' ~}}
  85. /// <summary>
  86. /// {{escape_comment field.comment}}
  87. /// </summary>
  88. {{~end~}}
  89. public {{declaring_type_name field.ctype}} {{format_field_name __code_style field.name}};
  90. {{~end~}}
  91. }
  92. {{namespace_with_grace_end __namespace_with_top_module}}