|
-
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
-
Apr 18th, 2018, 04:22 AM
#2
Re: RestSharp JSON deserialize on multiple different maps using the same header names
Maybe Set a big Class with all the params combined from all the classes.
You can then set boundaries on which params have data and decide which way to go.
For example if params1 to params4 have data and params5 to paramsX do not then you are in a Class Foo situation.
If params1 to params4 do not have data but params5 to params11 have then you are in a Class Bar situation etc...
If you find data on params1 to params4 but also data to params5 to paramas11 then there is something went wrong.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Apr 18th, 2018, 08:11 AM
#3
Thread Starter
Member
Re: RestSharp JSON deserialize on multiple different maps using the same header names
All the commands that I will be using will have data. It is a user interface into the web server to issue arbitrary commands as the user desires. So we need to switch dynamically between various different commands/result-class-definitions on the fly as a user decides they want to issue different commands throughout a session.
I am thinking I will need to use multiple private classes (with the same name) for the deserialize class name, with a unique definition within each unique REST command, and then create a class for each command to contain each private deserialize class.
-
Apr 18th, 2018, 08:51 AM
#4
Re: RestSharp JSON deserialize on multiple different maps using the same header names
How would you get classes with the same name?
You must use different namepaces if you want to do so.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Apr 18th, 2018, 10:38 AM
#5
Thread Starter
Member
Re: RestSharp JSON deserialize on multiple different maps using the same header names
The underlying data for the JSON data that is being returned from the web server to be deserialized determines the required name of the class in order to successfully deserialize it.
In this case using a different command returns data under a different JSON map, but the header name is the same. So the class name has to be the same but the definition now has to be different.
Yes, because of this constraint it looks like I will need to use a different namespace for each command that I enable in the interface.
-
Apr 18th, 2018, 11:03 AM
#6
Thread Starter
Member
Re: RestSharp JSON deserialize on multiple different maps using the same header names
Unfortunately then it looks like I will need to create a uniquely named public class using the same map, for each command, and copy the private version into the corresponding public one each time I parse it.
In a way, casting the same interface, to uniquely named public namespace, so I can use the data after I parse it.
This feels like I am making it too complicated but I don't know how else to do it.
-
Apr 18th, 2018, 11:08 AM
#7
Thread Starter
Member
Re: RestSharp JSON deserialize on multiple different maps using the same header names
OK never mind - yes I was making it too complicated.
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
|