1 #Region "Microsoft.VisualBasic::1811564255de9ee59499da9b7739ed07, Microsoft.VisualBasic.Core\ApplicationServices\IOHandler.vb"
2
3     ' Author:
4     
5     '       asuka (amethyst.asuka@gcmodeller.org)
6     '       xie (genetics@smrucc.org)
7     '       xieguigang (xie.guigang@live.com)
8     
9     ' Copyright (c) 2018 GPL3 Licensed
10     
11     
12     ' GNU GENERAL PUBLIC LICENSE (GPL3)
13     
14     
15     ' This program is free software: you can redistribute it and/or modify
16     ' it under the terms of the GNU General Public License as published by
17     ' the Free Software Foundation, either version 3 of the License, or
18     ' (at your option) any later version.
19     
20     ' This program is distributed in the hope that it will be useful,
21     ' but WITHOUT ANY WARRANTY; without even the implied warranty of
22     ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23     ' GNU General Public License for more details.
24     
25     ' You should have received a copy of the GNU General Public License
26     ' along with this program. If not, see <http://www.gnu.org/licenses/>.
27
28
29
30     ' /********************************************************************************/
31
32     ' Summaries:
33
34     '     Module IOHandler
35     
36     
37     '         Delegate Function
38     
39     
40     '         Delegate Function
41     
42     '             Properties: DefaultHandle, DefaultLoadHandle, DefaultSaveDescription
43     
44     '             Function: ReadJSON, SaveJSON, SaveXml
45     
46     '             SubSetHandle
47     
48     
49     
50     
51     
52     ' /********************************************************************************/
53
54 #End Region
55
56 Imports System.Runtime.CompilerServices
57 Imports System.Text
58 Imports Microsoft.VisualBasic.Serialization.JSON
59
60 Namespace ApplicationServices
61
62     ''' <summary>
63     ''' Collection IO extensions
64     ''' </summary>
65     Public Module IOHandler
66
67         Public Delegate Function ISave(obj As IEnumerable, path As String, encoding As Encoding) As Boolean
68         Public Delegate Function IRead(type As Type, path As String, encoding As Encoding) As IEnumerable
69
70         Public ReadOnly Property DefaultHandle As ISave = AddressOf SaveJSON
71         Public ReadOnly Property DefaultLoadHandle As IRead = AddressOf ReadJSON
72
73         Public ReadOnly Property DefaultSaveDescription As String
74             <MethodImpl(MethodImplOptions.AggressiveInlining)>
75             Get
76                 Return DefaultHandle _
77                     .Method _
78                     .DeclaringType _
79                     .Module _
80                     .Assembly _
81                     .Location _
82                     .FileName & "!" & DefaultHandle.Method.FullName(False)
83             End Get
84         End Property
85
86         Public Sub SetHandle(handle As ISave)
87             _DefaultHandle = handle
88         End Sub
89
90         Public Function ReadJSON(type As Type, path As String, encoding As Encoding) As IEnumerable
91             Dim text As String = path.ReadAllText(encoding)
92             type = type.MakeArrayType
93             Return DirectCast(JsonContract.LoadObject(text, type), IEnumerable)
94         End Function
95
96         Public Function SaveJSON(obj As IEnumerable, path As String, encoding As Encoding) As Boolean
97             Return GetObjectJson(obj, obj.GetType).SaveTo(path, encoding)
98         End Function
99
100         Public Function SaveXml(obj As IEnumerable, path As String, encoding As Encoding) As Boolean
101             Return GetXml(obj, obj.GetType).SaveTo(path, encoding)
102         End Function
103     End Module
104 End Namespace