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:
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;
}