example_01.bmx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. SuperStrict
  2. Framework Text.PersistenceXml
  3. Import BRL.StandardIO
  4. Type TRect
  5. Field x:Int
  6. Field y:Int
  7. Field w:Int
  8. Field h:Int
  9. Field ignoreMe:String = "Hello" {nopersist}
  10. End Type
  11. Type TObj
  12. Field text:String
  13. Field numbersi:Int[]
  14. Field numbersf:Float[]
  15. Field numbersd:Double[]
  16. Field numbersl:Long[]
  17. Field multi:Int[,,]
  18. Field circularRef:TTest
  19. Field refNull:TTest
  20. Field emptyList:TList = New TList
  21. Field list:TList = New TList
  22. Field rect:TRect = New TRect
  23. Field map:TMap = New TMap
  24. Field map2:TMap = New TMap
  25. Function Set:TObj()
  26. Local this:TObj = New TObj
  27. this.text = "woot"
  28. this.numbersi = [ 1, 2, 3, 4, 5, 6 ]
  29. this.numbersf = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]
  30. this.numbersd = [ 1.0:Double, 2.0:Double, 3.0:Double, 4.0:Double, 5.0:Double, 6.0:Double ]
  31. this.numbersl = [ 1:Long, 2:Long, 3:Long, 4:Long, 5:Long, 6:Long ]
  32. this.multi = New Int[3,4,5]
  33. this.multi[0,0,0] = 22
  34. this.multi[1,2,2] = 33
  35. this.multi[2,3,4] = 44
  36. this.list.AddLast("Item 1")
  37. this.rect.x = 100
  38. this.rect.y = 200
  39. this.rect.w = 300
  40. this.rect.h = 400
  41. this.map.Insert("Key 1", "Value 1")
  42. Return this
  43. End Function
  44. End Type
  45. Type TTest
  46. Field one:String
  47. Field two:Int
  48. Field three:Float
  49. Field four:Double
  50. Field five:Long
  51. Field obj:TObj
  52. Field rects:TRect[]
  53. Function Set:TTest()
  54. Local this:TTest = New TTest
  55. this.one = "Hello World"
  56. this.two = 155
  57. this.three = 3.33
  58. this.four = 2.95
  59. this.five = 222
  60. this.obj = TObj.Set()
  61. this.obj.circularRef = this
  62. this.rects = New TRect[2]
  63. 'rects[0] = New TRect ' <- null!
  64. this.rects[1] = New TRect
  65. this.rects[1].y = 125
  66. Return this
  67. End Function
  68. End Type
  69. ' register as a default serializer
  70. TXMLPersistenceBuilder.RegisterDefault(New TRectXmlSerializer)
  71. Local test:TTest = TTest.Set()
  72. Local persist:TPersist = New TXMLPersistenceBuilder.Build()
  73. ' ++ Serialize to a String
  74. Local s:String = persist.SerializeToString(test)
  75. Print s
  76. persist.Free()
  77. ' ++ De-serialize the String
  78. Local obj:Object = persist.DeSerializeObject(s)
  79. persist.Free()
  80. ' ++ Create a Stream and Serialize the current object to it.
  81. Local stream:TStream = WriteStream("example.bmo")
  82. persist.SerializeToStream(obj, stream)
  83. stream.Close()
  84. persist.Free()
  85. ' ++ De-serialize from a Stream.
  86. stream = ReadStream("example.bmo")
  87. obj = persist.DeSerializeFromStream(stream)
  88. persist.Free()
  89. ' ++ Serialize and output the latest object... all should be well :-)
  90. TPersist.format = True
  91. Print persist.SerializeToString(obj)
  92. Type TRectXMLSerializer Extends TXMLSerializer
  93. Global nil:TNode = New TMap._root
  94. Method TypeName:String()
  95. Return "TRect"
  96. End Method
  97. Method Serialize(tid:TTypeId, obj:Object, node:TxmlNode)
  98. Local rect:TRect = TRect(obj)
  99. Local sb:TStringBuilder = New TStringBuilder
  100. sb.AppendInt(rect.x)
  101. sb.Append(",").AppendInt(rect.y)
  102. sb.Append(",").AppendInt(rect.w)
  103. sb.Append(",").AppendInt(rect.h)
  104. node.SetContent(sb.ToString())
  105. End Method
  106. Method Deserialize:Object(objType:TTypeId, node:TxmlNode)
  107. Local rect:TRect = TRect(CreateObjectInstance(objType, node))
  108. Local parts:String[] = node.GetContent().Split(",")
  109. If parts.length = 4 Then
  110. rect.x = Int(parts[0])
  111. rect.y = Int(parts[1])
  112. rect.w = Int(parts[2])
  113. rect.h = Int(parts[3])
  114. End If
  115. Return rect
  116. End Method
  117. Method Clone:TXMLSerializer()
  118. Return New TRectXMLSerializer
  119. End Method
  120. End Type