Converting json to MemoryStream and back breaks the json
For context, I have the following JSON (most of the stuff omitted for brevity):
After I convert it to a
MemoryStream
and back to string, it surrounds the string with quotation marks and re-escapes the already-escaped characters.
Method used to convert to MemoryStream
:
I also tried using a StreamWriter
approach, but to no avail.
The snippet of code that converts the MemoryStream
back to string
:
It results in the following broken JSON:
I can't pinpoint exactly why this is happening, I guess it has to do with the fact that when it reads it back to a string
, because the content is already a "string
", it escapes the already-escaped quotes and surrounds it with the quotemarks. I don't know how to avoid this behaviour tho.
Edit: change wording36 Replies
it's not memorystream
it looks to me like string is being encoded one more time somewhere by json
I'm fairly sure this might just be your IDE showing the string as escaped like that
if you use the viewer that opens in a new window iirc it doesn't escape the content
I copied the content from the viewer
yea thats always escaped
hovering the converted string:

hovering the original string:

the conversion surrounds the string with quotation marks and escapes again the already-escaped characters
hmm thats weird though

agreed
what if you convert it to a memory stream?
unfortunately I can't work with a
byte[]
, the method I'm calling expects a Stream
whence why I convert to a MemoryStream


I cannot replicate, can you try to output the resulting string read via the stream reader?
by the way, you don't need to set
Position
to 0 on the memory reader, that's a given if constructed from a byte array
you mean like
string result = await reader.ReadToEndAsync()
?yes
the result is the broken string
the code
hmm in that case there is definitely some intermediate layer between the parts of code you've shown
Are those two strings from different applications and transmitted via HTTP or similar?
I'm testing an azure function, so the way they implemented it here is by expecting a HttpRequestData, instead of a HttpRequest
I feel like maybe that layer causes the escaping
So what I'm (trying) to do is to create a HttpRequestData and set its Body property as the json
but to do so, I have to convert to a Stream, as it does not accept a string
Are you setting the header to
application/json
? Maybe the receiving end expects that and otherwise escapes it or something like thatI think it's plausible, but wouldn't it also break incoming jsons? I tested it using postman and it was fine
yup I found something
nvm its related to something else
in the payload? lemme check
I'm using a premade function
When you manually send the request, what do you receive in postman?
Stack Overflow
HttpRequestData wraps body in { "json" : "" }
I am trying to set up an .NET 5 Azure Http function that takes a request with a json body and outputs it to the logs.
using System.Threading.Tasks;
using Microsoft.Azure.Functions.Worker;
using Mic...
Maybe that's of help?
https://github.com/Azure/azure-functions-dotnet-worker/blob/main/src/DotNetWorker.Core/Http/HttpRequestDataExtensions.cs or this
in the headers stuff?
Transfer-Encoding: chunked
Server: kestrel
and the date
the body
what is sent?
{"schemaId":"azureMonitorCommonAlertSchema","data":{...}}
the same as the hardcoded string in the code
I checked and I have the same packages (different version tho, as they are more recent)
so the sending part doesn't seem to be at fault
I tried expliciting the content-type for the HttpRequestData that is created, but same result
Can you share the whole code responsible for receiving the request and outputting the resulting json?
I guess I can
one minute
The validPayload is the valid json