|
-
Apr 17th, 2018, 10:41 PM
#1
Thread Starter
Member
RestSharp JSON deserialize on multiple different maps using the same header names
What is the best way to address this? My code below works fine for a single JSON map by describing it in a class that matches the header name. But there's no way to deserialize a different command that returns a different map under the same "result" header.
Using the approach below, I think won't work for multiple maps using the same header names, because the class has to match the header name. I think this is because the iRestResponse object tracks the header names and aligns them with the class used. Which it seems would require two different class definitions that both use the same name.
Just to be clear, this code works as long as I only use it on a single command that returns a given data layout.
As soon as I want to use it for a different REST command that returns a different data layout, now I have to redefine the "result" class to match the new command.
In this example I used a "foo" command and a "bar" command. foo returns a data layout that matches the "return" class, "bar" returns a data layout that matches the "return2" class, which will never be used. No matter what command I use, the deserializer always wants to use the class named "return".
What is the right way to organize this for a large set of different commands where I can create a unique map in advance for all the commands that return data?
Code:
Imports RestSharp
Imports RestSharp.Deserializers
Imports System.Net
Public Class Form1
Public HEADER As String
Public TAIL As String
Public client As New RestClient()
Public request As New RestRequest()
Public deserial As RestSharp.Deserializers.JsonDeserializer = New JsonDeserializer()
Public requestid As String
Public Function GetResult(ByVal command As String) As RestResult
client.BaseUrl = New Uri("https://" & IP)
request.Resource = HEADER & command & TAIL & requestid
request.Method = Method.GET
Dim queryResult As IRestResponse = client.Execute(request) 'Send REST URL to web server
Dim Session As RestResult = Nothing
Try
Session = deserial.Deserialize(Of RestResult)(queryResult) 'Deserialize return data
Catch exception As System.Runtime.Serialization.SerializationException
MessageBox.Show(exception.ToString)
Finally
If (Session Is Nothing Or Session.result Is Nothing) Then
MessageBox.Show("In GetResult() - Session.result Is Nothing")
Else
MessageBox.Show("In GetResult() - Session.result.Count = " & Session.result.Count)
End If
End Try
return Session
End Function
Public Function Foo() As RestResult
Dim ListResult As RestResult = GetResult("Foo")
If (ListResult Is Nothing) Then
MessageBox.Show("Failed Foo command")
Else
MessageBox.Show("Click when ready")
End If
Return ListResult
End Function
Public Function Bar() As RestResult
Dim ListResult As RestResult = GetResult("Bar")
If (ListResult Is Nothing) Then
MessageBox.Show("Failed Bar command")
Else
MessageBox.Show("Click when ready")
End If
Return ListResult
End Function
'results that return from Foo command
Public Class result
Public Property param1 As String
Public Property param2 As String
Public Property param3 As String
Public Property param4 As String
Public Property id As Integer
End Class
'Results that return from Bar command - this class also wants to be named "result"
'trying to figure this out - how to handle multiple JSON maps that all have the same "result" wrapper
'on the same web server and need to dynamically switch between them
Public Class result2
Public Property param5 As String
Public Property param6 As String
Public Property param7 As String
Public Property param8 As String
Public Property param9 As String
Public Property param10 As String
Public Property param11 As String
End Class
'Wrapper that returns from all commands
Public Class RestResult
Public Property result As List(Of result)
Public Property status As Integer
End Class
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|