chef 3744 Posted February 14, 2016 Share Posted February 14, 2016 I have a coding question, because I can not understand why this doesn't work. Check this out and maybe someone can explain why this doesn't work. I'm trying to create a reusable class with the IHttpClient object. Here is the "HttpClient" Interface object making a request for a JSON String: I would expect this to Deserialize a stream to String: Note: I pass the url to the Function. Dim responseString As String resultString = JSonSerializer.DeserializeFromStream(Of String)( Await HttpClient.Get( New HttpRequestOptions With { .Url = url, .CancellationToken = CancellationToken.None })) In fact I can easily "msgbox" this response string. Note: I realize that there should be a Class object full of properties which mirrors the Response JSON, instead of "(Of String)". But I just want the JSON as a string, so I can Deserialize the String later. I know I could use a "streamreader" object to get the string, but I have a feeling that the "System.IO" namespace is causing problems across different platforms in my plugin, and was hoping I could just use emby's JSONSerializer object to get the string. When I attempt to "DeserialzeFromString" after collecting the response from the code above, nothing is deserialized... I suppose I am misusing IJsonSerializer object, trying to get it to serialize a string. Link to comment Share on other sites More sharing options...
ebr 14850 Posted February 15, 2016 Share Posted February 15, 2016 That's not c#... Link to comment Share on other sites More sharing options...
chef 3744 Posted February 15, 2016 Author Share Posted February 15, 2016 I know.. someone changed the title... Lol! Vb.net Link to comment Share on other sites More sharing options...
ebr 14850 Posted February 15, 2016 Share Posted February 15, 2016 Did I read your initial post right? You actually want the string of JSON and not an object? If so, just get rid of the serializer and take the result of your HttpClient.get. 1 Link to comment Share on other sites More sharing options...
chef 3744 Posted February 15, 2016 Author Share Posted February 15, 2016 I'll give it a go. I though it was returning a stream and had to be converted to string... Thank you. Link to comment Share on other sites More sharing options...
chef 3744 Posted February 15, 2016 Author Share Posted February 15, 2016 AH-HA! It took some research but here is how you can read a bunch of bytes in a stream, and convert them to a String. Now this still uses the "System.Text", and System.IO" Namespaces, but doesn't use a "streamreader" object to read the stream Note: (which is what I believe is currently the issue with my plugin working across different platforms...this hasn't been tested though...) Public Shared Async Function GetStream(url As String) As Task(Of String) Dim resultString As String Try Dim resultStream As Stream = Await HttpClient.Get( _ New HttpRequestOptions With { .Url = url, .CancellationToken = CancellationToken.None }).ConfigureAwait(False) Dim bytes As Byte() = New Byte(resultStream.Length) {} resultStream.Position = 0 Await resultStream.ReadAsync(bytes, 0, resultStream.Length) resultString = Encoding.ASCII.GetString(bytes) Catch ex As Exception Return String.Empty End Try Return resultString End Function Guess we'll see how this tests out! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now