source: trunk/Include/Classes/System/Xml/Serialization/XmlSerializer.ab@ 472

Last change on this file since 472 was 452, checked in by dai, 17 years ago

System/Xml/Serialization/XmlSerializer.abを追加。
まずはシリアライズ処理を動くようにした。
※逆シリアライズは未実装なので、ActiveBasic.Xml.Parserクラスを実装してから対応すること。

File size: 4.0 KB
Line 
1Namespace System
2Namespace Xml
3Namespace Serialization
4
5
6Class XmlSerializer
7 m_typeInfo As System.TypeInfo
8
9 Static Sub SerializeForBasicType( typeInfo As TypeInfo, memoryOffset As LONG_PTR, o As Object, doc As XmlDocument, parentNode As XmlNode )
10 Dim valueStr = Nothing As String
11 If typeInfo.FullName = "Byte" Then
12 valueStr = Str$( GetByte( ObjPtr( o ) + memoryOffset ) As Byte )
13 ElseIf typeInfo.FullName = "SByte" Then
14 valueStr = Str$( GetByte( ObjPtr( o ) + memoryOffset ) As SByte )
15 ElseIf typeInfo.FullName = "Word" Then
16 valueStr = Str$( GetWord( ObjPtr( o ) + memoryOffset ) As Word )
17 ElseIf typeInfo.FullName = "Integer" Then
18 valueStr = Str$( GetWord( ObjPtr( o ) + memoryOffset ) As Integer )
19 ElseIf typeInfo.FullName = "DWord" Then
20 valueStr = Str$( GetDWord( ObjPtr( o ) + memoryOffset ) As DWord )
21 ElseIf typeInfo.FullName = "Long" Then
22 valueStr = Str$( GetDWord( ObjPtr( o ) + memoryOffset ) As Long )
23 ElseIf typeInfo.FullName = "QWord" Then
24 valueStr = Str$( GetQWord( ObjPtr( o ) + memoryOffset ) As QWord )
25 ElseIf typeInfo.FullName = "Int64" Then
26 valueStr = Str$( GetQWord( ObjPtr( o ) + memoryOffset ) As Int64 )
27 ElseIf typeInfo.FullName = "Boolean" Then
28 If GetByte( ObjPtr( o ) + memoryOffset ) As Boolean Then
29 valueStr = "True"
30 Else
31 valueStr = "False"
32 End If
33 ElseIf typeInfo.FullName = "Single" Then
34 valueStr = Str$( GetSingle( ObjPtr( o ) + memoryOffset ) As Single )
35 ElseIf typeInfo.FullName = "Double" Then
36 valueStr = Str$( GetDouble( ObjPtr( o ) + memoryOffset ) As Double )
37 ElseIf typeInfo.FullName = "VoidPtr" Then
38 valueStr = Str$( Get_LONG_PTR( ObjPtr( o ) + memoryOffset ) )
39 Else
40 Throw
41 End If
42
43 Dim childNode = doc.CreateElement( typeInfo.FullName )
44 parentNode.AppendChild( childNode )
45 childNode.AppendChild( doc.CreateTextNode( valueStr ) )
46
47 End Sub
48
49 Static Sub SerializeForClass( typeInfo As TypeInfo, o As Object, doc As XmlDocument, parentNode = Nothing As XmlNode )
50 If typeInfo.IsClass() and typeInfo.FullName = "System.Object" Then
51 ' Object型はシリアライズしない
52 Return
53 End If
54
55 If ActiveBasic.IsNothing( parentNode ) Then
56 parentNode = doc
57 End If
58
59 Dim memberInfos = typeInfo.GetMembers()
60
61 ' 子ノードを生成
62 Dim childNode = doc.CreateElement( typeInfo.Name )
63 Dim namespaceAttr = doc.CreateAttribute( "namespace" )
64 namespaceAttr.Value = typeInfo.Namespace
65 childNode.Attributes.Add( namespaceAttr )
66
67 ' 親ノードへ追加
68 parentNode.AppendChild( childNode )
69
70 If Not ActiveBasic.IsNothing( typeInfo.BaseType ) Then
71 ' 基底クラスが存在するとき
72
73 ' 基底クラスをシリアライズ
74 SerializeForClass( typeInfo.BaseType, o, doc, childNode )
75 End If
76
77 ' メンバをシリアライズ
78 Foreach memberInfo In memberInfos
79 If memberInfo.MemberType.IsPointer() Then
80 Throw
81 ElseIf memberInfo.MemberType.IsClass() Then
82 SerializeForClass( memberInfo.MemberType, o, doc, childNode )
83 ElseIf memberInfo.MemberType.IsValueType() Then
84 SerializeForBasicType( memberInfo.MemberType, memberInfo._System_Offset, o, doc, childNode )
85 End If
86 Next
87 End Sub
88
89 Function Deserialize( doc As XmlDocument ) As Object
90 End Function
91
92Public
93 Sub XmlSerializer( typeInfo As System.TypeInfo )
94 This.m_typeInfo = typeInfo
95 End Sub
96 Sub ~XmlSerializer()
97 End Sub
98
99
100 /*!
101 @brief XML文書をオブジェクトに逆シリアライズします。
102 @author Daisuke Yamamoto
103 */
104 Function Deserialize( stream As System.IO.Stream ) As Object
105 Dim doc = New XmlDocument
106 doc.Load( stream )
107 Deserialize( doc )
108 End Function
109
110 /*!
111 @brief オブジェクトをXML文書にシリアライズします。
112 @author Daisuke Yamamoto
113 */
114 Sub Serialize( stream As System.IO.Stream, o As Object )
115 Dim doc = New XmlDocument
116 doc.AppendChild( doc.CreateXmlDeclaration( "1.0", "shift-jis", Nothing ) )
117
118 SerializeForClass( m_typeInfo, o, doc )
119
120 ' ストリームに保存
121 doc.Save( stream )
122 End Sub
123End Class
124
125
126End Namespace
127End Namespace
128End Namespace
Note: See TracBrowser for help on using the repository browser.