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