© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
15 replies
Pannekoekje

JSON deserialization (proper way?)

Hi,

I'm writing a client that consumes some JSON from a websocket.
The socket can give multiple types of messages over it, what would be the most efficient (fastest) way to deserialize the JSON objects to classes? All the type of messages are known beforehand.

The way I'm doing it now is:

var jsonObject = JsonDocument.Parse(inputJson);
IReponseObject returnObject = null;

if (jsonObject.RootElement.TryGetProperty("error", out JsonElement error))
{
    throw new Exception($"Issue found in response: \"{error.ToString()}\"");
}

if (jsonObject.RootElement.TryGetProperty("event", out JsonElement jsonEvent))
{
    switch (jsonEvent.ToString())
    {
        case "authenticate":
            returnObject = JsonSerializer.Deserialize<AuthenticatedResponse>(jsonObject);
            break;
        default:
            break;
    }
}

if (jsonObject.RootElement.TryGetProperty("action", out JsonElement jsonAction))
{
    switch (jsonAction.ToString())
    {
        case "getTime":
            responseContent = jsonObject.RootElement.GetProperty("response");
            IReponseObject = JsonSerializer.Deserialize<TimeResponse>(responseContent);

            break;
        case "getPressure":
            responseContent = jsonObject.RootElement.GetProperty("response");
            IReponseObject = new PressureResponse() { Pressures = new List<Pressure>() };
            if (responseContent.ValueKind == JsonValueKind.Object)
            {
                var pressure = JsonSerializer.Deserialize<PressureResponse>(responseContent);
                (IReponseObject as PressureResponse).Pressure.Add(Pressure);
            }
        else
        {
        var pressure = JsonSerializer.Deserialize<List<PressureResponse>>(responseContent);
                (IReponseObject as PressureResponses).Pressures.AddRange(Pressure);
        }
            break;
}
var jsonObject = JsonDocument.Parse(inputJson);
IReponseObject returnObject = null;

if (jsonObject.RootElement.TryGetProperty("error", out JsonElement error))
{
    throw new Exception($"Issue found in response: \"{error.ToString()}\"");
}

if (jsonObject.RootElement.TryGetProperty("event", out JsonElement jsonEvent))
{
    switch (jsonEvent.ToString())
    {
        case "authenticate":
            returnObject = JsonSerializer.Deserialize<AuthenticatedResponse>(jsonObject);
            break;
        default:
            break;
    }
}

if (jsonObject.RootElement.TryGetProperty("action", out JsonElement jsonAction))
{
    switch (jsonAction.ToString())
    {
        case "getTime":
            responseContent = jsonObject.RootElement.GetProperty("response");
            IReponseObject = JsonSerializer.Deserialize<TimeResponse>(responseContent);

            break;
        case "getPressure":
            responseContent = jsonObject.RootElement.GetProperty("response");
            IReponseObject = new PressureResponse() { Pressures = new List<Pressure>() };
            if (responseContent.ValueKind == JsonValueKind.Object)
            {
                var pressure = JsonSerializer.Deserialize<PressureResponse>(responseContent);
                (IReponseObject as PressureResponse).Pressure.Add(Pressure);
            }
        else
        {
        var pressure = JsonSerializer.Deserialize<List<PressureResponse>>(responseContent);
                (IReponseObject as PressureResponses).Pressures.AddRange(Pressure);
        }
            break;
}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

JSON Deserialization Multiple Entries
C#CC# / help
2y ago
Problem with json deserialization
C#CC# / help
2y ago
Custom Newtonsoft JSON deserialization
C#CC# / help
2y ago
Deserialization of json object
C#CC# / help
3y ago