1 | #Region "Microsoft.VisualBasic::78ede7fa8ccb25820af8457aa2804413, Microsoft.VisualBasic.Core\Serialization\JSON\AnonymousJSON.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 AnonymousJSONExtensions |
35 | ' |
36 | ' Function: AnonymousJSON, (+4 Overloads) GetJson |
37 | ' |
38 | ' |
39 | ' /********************************************************************************/ |
40 | |
41 | #End Region |
42 | |
43 | Imports System.Reflection |
44 | Imports System.Runtime.CompilerServices |
45 | Imports Microsoft.VisualBasic.ComponentModel.DataSourceModel |
46 | |
47 | Namespace Serialization.JSON |
48 | |
49 | ''' <summary> |
50 | ''' Extension helpers for deal with the anonymous type |
51 | ''' </summary> |
52 | Public Module AnonymousJSONExtensions |
53 | |
54 | <Extension> |
55 | Public Function GetJson(obj As String(,)) As String |
56 | With New Dictionary(Of String, String) |
57 | For Each prop As String() In obj.RowIterator |
58 | Call .Add(prop(0), prop(1)) |
59 | Next |
60 | |
61 | Return .GetJson |
62 | End With |
63 | End Function |
64 | |
65 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
66 | <Extension> |
67 | Public Function GetJson(array As IEnumerable(Of String), Optional indent As Boolean = False) As String |
68 | Return array.ToArray.GetJson(indent:=indent) |
69 | End Function |
70 | |
71 | ''' <summary> |
72 | ''' 专门针对字符串集合的 |
73 | ''' </summary> |
74 | ''' <param name="keys"></param> |
75 | ''' <param name="indent"></param> |
76 | ''' <returns></returns> |
77 | ''' <remarks> |
78 | ''' 任意类型的字符串集合都会被首先转换为字符串数组然后再转换为json字符串 |
79 | ''' </remarks> |
80 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
81 | <Extension> |
82 | Public Function GetJson(keys As Dictionary(Of String, Object).KeyCollection, Optional indent As Boolean = False) As String |
83 | Return GetType(String()).GetObjectJson(keys.ToArray, indent) |
84 | End Function |
85 | |
86 | ''' <summary> |
87 | ''' 专门针对字符串集合的 |
88 | ''' </summary> |
89 | ''' <param name="keys"></param> |
90 | ''' <param name="indent"></param> |
91 | ''' <returns></returns> |
92 | ''' <remarks> |
93 | ''' 任意类型的字符串集合都会被首先转换为字符串数组然后再转换为json字符串 |
94 | ''' </remarks> |
95 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
96 | <Extension> |
97 | Public Function GetJson(keys As SortedDictionary(Of String, Object).KeyCollection, Optional indent As Boolean = False) As String |
98 | Return GetType(String()).GetObjectJson(keys.ToArray, indent) |
99 | End Function |
100 | |
101 | <Extension> |
102 | Public Function AnonymousJSON(Of T As Class)(obj As T) As String |
103 | Dim keys = obj.GetType.GetProperties(PublicProperty) |
104 | |
105 | With New Dictionary(Of String, String) |
106 | For Each key As PropertyInfo In keys |
107 | Call .Add(key.Name, key.GetValue(obj).ToString) |
108 | Next |
109 | |
110 | Return .GetJson |
111 | End With |
112 | End Function |
113 | End Module |
114 | End Namespace |