Advertisement
Guest User

jsonvbo global code

a guest
Jul 21st, 2021
530
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 6.05 KB | None | 0 0
  1.     Dim Private datetime = new DateTime()
  2.  
  3.     Private Class JSON
  4.         Public Const Array As String = "JSON:Array"
  5.         Public Const Null As String = "JSON:Null"
  6.     End Class
  7.  
  8.  
  9.     Public Function ConvertToJSON(ByVal dt As DataTable) As String
  10.         Dim o As Object = SerialiseGeneric(dt, True)
  11.         Return JsonConvert.SerializeObject(o)
  12.     End Function
  13.  
  14.     Public Function SerialiseGeneric(ByVal o As Object, ByVal removeArray As Boolean) As Object
  15.         Dim dt As DataTable = TryCast(o, DataTable)
  16.         If dt IsNot Nothing Then
  17.             Return SerialiseDataTable(dt)
  18.         End If
  19.  
  20.         Dim dr As DataRow = TryCast(o, DataRow)
  21.         If dr IsNot Nothing Then
  22.             Return SerialiseDataRow(dr, removeArray)
  23.         End If
  24.  
  25.         Dim s As String = TryCast(o, String)
  26.         If s IsNot Nothing AndAlso s = JSON.Null Then
  27.             Return Nothing
  28.         End If
  29.  
  30.         If o IsNot Nothing Then
  31.             Return o
  32.         End If
  33.  
  34.         Return Nothing
  35.     End Function
  36.  
  37.     Public Function SerialiseDataTable(ByVal dt As DataTable) As Object
  38.         If  IsSingleRow(dt) Then
  39.             Return SerialiseGeneric(dt.Rows(0), False)
  40.         Else
  41.             Dim ja As New JArray()
  42.             For Each r As DataRow In dt.Rows
  43.                 ja.Add(SerialiseGeneric(r, True))
  44.             Next
  45.             Return ja
  46.         End If
  47.     End Function
  48.  
  49.     Public Function IsSingleRow(ByVal dt As DataTable) As Boolean
  50.         If dt.ExtendedProperties.Contains("SingleRow") Then
  51.             Return CBool(dt.ExtendedProperties("SingleRow"))
  52.         End If
  53.         'Fallback for older versions of blueprism
  54.         Return dt.Rows.Count = 1
  55.     End Function
  56.  
  57.     Public Function SerialiseDataRow(ByVal dr As DataRow, ByVal removeArray As Boolean) As Object
  58.         Dim jo As New JObject()
  59.         For Each c As DataColumn In dr.Table.Columns
  60.             Dim s As String = c.ColumnName
  61.             If removeArray AndAlso s = JSON.Array Then
  62.                 Return SerialiseGeneric(dr(s), True)
  63.             End If
  64.             jo(s) = JToken.FromObject(SerialiseGeneric(dr(s), False))
  65.         Next
  66.         Return jo
  67.     End Function
  68.  
  69.     Public Function ConvertToDataTable(ByVal json As String) As DataTable
  70.         Dim o As Object = JsonConvert.DeserializeObject(json)
  71.         Return DirectCast(DeserialiseGeneric(o, True), DataTable)
  72.     End Function
  73.  
  74.     Private Function DeserialiseGeneric(ByVal o As Object, ByVal populate As Boolean) As Object
  75.         Dim a As JArray = TryCast(o, JArray)
  76.         If a IsNot Nothing Then
  77.             Return DeserialiseArray(a, populate)
  78.         End If
  79.  
  80.         Dim jo As JObject = TryCast(o, JObject)
  81.         If jo IsNot Nothing Then
  82.             Return DeserialiseObject(jo, populate)
  83.         End If
  84.  
  85.         Dim jv As JValue = TryCast(o, JValue)
  86.         If jv IsNot Nothing Then
  87.             If GetTypeOf(jv.Value) = GetTypeOf(datetime) Then
  88.                 Return jv.Value.ToLocalTime()
  89.             Else
  90.                 Return jv.Value
  91.             End if
  92.         End If
  93.  
  94.         Return JSON.Null
  95.     End Function
  96.  
  97.     Private Function GetKey(ByVal kv As KeyValuePair(Of String, JToken)) As String
  98.         If kv.Key IsNot Nothing Then
  99.             Return kv.Key.ToString()
  100.         End If
  101.         Return ""
  102.     End Function
  103.  
  104.  
  105.     Private Function DeserialiseObject(ByVal o As JObject, ByVal populate As Boolean) As DataTable
  106.         Dim dt As New DataTable
  107.  
  108.         For Each kv As KeyValuePair(Of String, JToken) In o
  109.             Dim type As Type = GetTypeOf(DeserialiseGeneric(kv.Value, False))
  110.             dt.Columns.Add(GetKey(kv), type)
  111.         Next
  112.  
  113.         If populate Then
  114.             Dim dr As DataRow = dt.NewRow()
  115.             For Each kv As KeyValuePair(Of String, JToken) In o
  116.                 dr(getKey(kv)) = DeserialiseGeneric(kv.Value, True)
  117.             Next
  118.             dt.Rows.Add(dr)
  119.         End If
  120.  
  121.         Return dt
  122.     End Function
  123.  
  124.     Private Function DeserialiseArray(ByVal o As JArray, ByVal populate As Boolean) As DataTable
  125.         Dim dt As New DataTable
  126.  
  127.         Dim first As Type = Nothing
  128.         For Each e As Object In o
  129.             If first Is Nothing Then
  130.                 first = GetTypeOf(DeserialiseGeneric(e, False))
  131.             End If
  132.             If GetTypeOf(DeserialiseGeneric(e, False)) IsNot first Then
  133.                 Throw New Exception("Data Type mismatch in array")
  134.             End If
  135.         Next
  136.  
  137.         If first Is GetTypeOf(new DataTable) Then
  138.             For Each e As Object In o
  139.                 Dim ja As JArray = TryCast(e, JArray)
  140.                 If ja IsNot Nothing Then
  141.                     If Not dt.Columns.Contains(JSON.Array) then
  142.                         dt.Columns.Add(JSON.Array, first)
  143.                     End If
  144.                 End If
  145.  
  146.                 Dim je As JObject = TryCast(e, JObject)
  147.                 If je IsNot Nothing Then
  148.                     For Each kv As KeyValuePair(Of String, JToken) In je
  149.                         Dim type As Type = GetTypeOf(DeserialiseGeneric(kv.Value, False))
  150.                         If Not dt.Columns.Contains(GetKey(kv)) then
  151.                             dt.Columns.Add(GetKey(kv), type)
  152.                         End if
  153.                     Next
  154.                 End If
  155.             Next
  156.         ElseIf first IsNot Nothing Then
  157.             dt.Columns.Add(JSON.Array, first)
  158.         End If
  159.  
  160.         If populate Then
  161.             If first Is GetTypeOf(new DataTable) Then
  162.                 For Each e As Object In o
  163.                     Dim ja As JArray = TryCast(e, JArray)
  164.                     If ja IsNot Nothing Then
  165.                         Dim dr As DataRow = dt.NewRow()
  166.                         dr(JSON.Array) = DeserialiseGeneric(ja, True)
  167.                         dt.Rows.Add(dr)
  168.                     End If
  169.  
  170.                     Dim je As JObject = TryCast(e, JObject)
  171.                     If je IsNot Nothing Then
  172.                         Dim dr As DataRow = dt.NewRow()
  173.                         For Each kv As KeyValuePair(Of String, JToken) In je
  174.                             dr(getKey(kv)) = DeserialiseGeneric(kv.Value, True)
  175.                         Next
  176.                         dt.Rows.Add(dr)
  177.                     End If
  178.                 Next
  179.             Else
  180.                 For Each e As Object In o
  181.                     Dim dr As DataRow = dt.NewRow()
  182.                     dr(JSON.Array) = DeserialiseGeneric(e, True)
  183.                     dt.Rows.Add(dr)
  184.                 Next
  185.             End If
  186.         End If
  187.  
  188.         Return dt
  189.     End Function
  190.  
  191.     Private Function GetTypeOf(ByVal o As Object) As Type
  192.         If o Is Nothing Then Return GetType(String)
  193.         Return o.GetType
  194.     End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement