microsoft-visualbasic-runtime/Serialization/JSON/AnonymousJSON.vb

115 lines
4.0 KiB
VB.net
Raw Normal View History

2019-03-10 18:10:46 +08:00
#Region "Microsoft.VisualBasic::1282c9850a210f0608f163bd3742dbe9, Microsoft.VisualBasic.Core\Serialization\JSON\AnonymousJSON.vb"
2018-10-06 00:02:05 +08:00
2018-12-23 00:49:32 +08:00
' Author:
'
' asuka (amethyst.asuka@gcmodeller.org)
' xie (genetics@smrucc.org)
' xieguigang (xie.guigang@live.com)
'
' Copyright (c) 2018 GPL3 Licensed
'
'
' GNU GENERAL PUBLIC LICENSE (GPL3)
'
'
' This program is free software: you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation, either version 3 of the License, or
' (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
' GNU General Public License for more details.
'
' You should have received a copy of the GNU General Public License
' along with this program. If not, see <http://www.gnu.org/licenses/>.
2018-12-23 00:42:21 +08:00
2018-12-23 00:49:32 +08:00
' /********************************************************************************/
2018-12-23 00:42:21 +08:00
2018-12-23 00:49:32 +08:00
' Summaries:
2018-12-23 00:42:21 +08:00
2018-12-23 00:49:32 +08:00
' Module AnonymousJSONExtensions
'
' Function: AnonymousJSON, (+4 Overloads) GetJson
'
'
' /********************************************************************************/
2018-10-06 00:02:05 +08:00
#End Region
Imports System.Reflection
2018-10-05 04:32:52 +08:00
Imports System.Runtime.CompilerServices
Imports Microsoft.VisualBasic.ComponentModel.DataSourceModel
2018-10-05 04:26:11 +08:00
Namespace Serialization.JSON
''' <summary>
''' Extension helpers for deal with the anonymous type
''' </summary>
Public Module AnonymousJSONExtensions
2018-10-05 04:26:11 +08:00
<Extension>
Public Function GetJson(obj As String(,)) As String
With New Dictionary(Of String, String)
For Each prop As String() In obj.RowIterator
Call .Add(prop(0), prop(1))
Next
Return .GetJson
End With
End Function
2018-10-05 04:32:52 +08:00
<MethodImpl(MethodImplOptions.AggressiveInlining)>
<Extension>
Public Function GetJson(array As IEnumerable(Of String), Optional indent As Boolean = False) As String
Return array.ToArray.GetJson(indent:=indent)
End Function
2018-12-23 00:42:21 +08:00
''' <summary>
''' 专门针对字符串集合的
''' </summary>
''' <param name="keys"></param>
''' <param name="indent"></param>
''' <returns></returns>
''' <remarks>
''' 任意类型的字符串集合都会被首先转换为字符串数组然后再转换为json字符串
''' </remarks>
<MethodImpl(MethodImplOptions.AggressiveInlining)>
<Extension>
Public Function GetJson(keys As Dictionary(Of String, Object).KeyCollection, Optional indent As Boolean = False) As String
Return GetType(String()).GetObjectJson(keys.ToArray, indent)
End Function
''' <summary>
''' 专门针对字符串集合的
''' </summary>
''' <param name="keys"></param>
''' <param name="indent"></param>
''' <returns></returns>
''' <remarks>
''' 任意类型的字符串集合都会被首先转换为字符串数组然后再转换为json字符串
''' </remarks>
<MethodImpl(MethodImplOptions.AggressiveInlining)>
<Extension>
Public Function GetJson(keys As SortedDictionary(Of String, Object).KeyCollection, Optional indent As Boolean = False) As String
Return GetType(String()).GetObjectJson(keys.ToArray, indent)
End Function
2018-10-05 04:32:52 +08:00
<Extension>
Public Function AnonymousJSON(Of T As Class)(obj As T) As String
Dim keys = obj.GetType.GetProperties(PublicProperty)
With New Dictionary(Of String, String)
For Each key As PropertyInfo In keys
Call .Add(key.Name, key.GetValue(obj).ToString)
Next
Return .GetJson
End With
End Function
End Module
2018-10-06 00:02:05 +08:00
End Namespace