Update for jart/json.cpp change

This commit is contained in:
Peter Johnson
2026-03-29 15:38:18 -07:00
parent de3e211fdb
commit 9ca93fa190
120 changed files with 1240 additions and 1087 deletions

View File

@@ -54,16 +54,12 @@ void HALSimHttpConnection::ProcessWsUpgrade() {
return;
}
wpi::util::json j;
try {
j = wpi::util::json::parse(msg);
} catch (const wpi::util::json::parse_error& e) {
std::string err("JSON parse failed: ");
err += e.what();
m_websocket->Fail(400, err);
auto j = wpi::util::json::parse(msg);
if (!j) {
m_websocket->Fail(400, fmt::format("JSON parse failed: {}", j.error()));
return;
}
m_server->OnNetValueChanged(j);
m_server->OnNetValueChanged(*j);
});
m_websocket->closed.connect([this](uint16_t, auto) {
@@ -80,11 +76,11 @@ void HALSimHttpConnection::ProcessWsUpgrade() {
void HALSimHttpConnection::OnSimValueChanged(const wpi::util::json& msg) {
// Skip sending if this message is not in the allowed filter list
try {
auto& type = msg.at("type").get_ref<const std::string&>();
auto& type = msg.at("type").get_string();
if (!m_server->CanSendMessage(type)) {
return;
}
} catch (wpi::util::json::exception& e) {
} catch (std::logic_error& e) {
wpi::util::print(stderr, "Error with message: {}\n", e.what());
}

View File

@@ -162,8 +162,8 @@ void HALSimWeb::OnNetValueChanged(const wpi::util::json& msg) {
// generate the key
try {
auto& type = msg.at("type").get_ref<const std::string&>();
auto& device = msg.at("device").get_ref<const std::string&>();
auto& type = msg.at("type").get_string();
auto& device = msg.at("device").get_string();
wpi::util::SmallString<64> key;
key.append(type);
@@ -176,7 +176,7 @@ void HALSimWeb::OnNetValueChanged(const wpi::util::json& msg) {
if (provider) {
provider->OnNetValueChanged(msg.at("data"));
}
} catch (wpi::util::json::exception& e) {
} catch (std::logic_error& e) {
wpi::util::print(stderr, "Error with incoming message: {}\n", e.what());
}
}

View File

@@ -4,7 +4,6 @@
#pragma once
#include <cinttypes>
#include <memory>
#include <string_view>
#include <utility>
@@ -12,10 +11,11 @@
#include "wpi/halsim/ws_core/HALSimBaseWebSocketConnection.hpp"
#include "wpi/halsim/ws_server/HALSimWeb.hpp"
#include "wpi/net/HttpWebSocketServerConnection.hpp"
#include "wpi/net/uv/AsyncFunction.hpp"
#include "wpi/net/uv/Buffer.hpp"
#include "wpi/util/json_fwd.hpp"
#include "wpi/util/mutex.hpp"
namespace wpi::util {
class json;
} // namespace wpi::util
namespace wpilibws {

View File

@@ -16,7 +16,10 @@
#include "wpi/net/uv/Loop.hpp"
#include "wpi/net/uv/Tcp.hpp"
#include "wpi/util/StringMap.hpp"
#include "wpi/util/json_fwd.hpp"
namespace wpi::util {
class json;
} // namespace wpi::util
namespace wpilibws {

View File

@@ -44,18 +44,15 @@ void WebServerClientTest::InitializeWebSocket(const std::string& host, int port,
});
m_websocket->text.connect([this](auto msg, bool) {
wpi::util::json j;
try {
j = wpi::util::json::parse(msg);
} catch (const wpi::util::json::parse_error& e) {
std::string err("JSON parse failed: ");
err += e.what();
auto j = wpi::util::json::parse(msg);
if (!j) {
auto err = fmt::format("JSON parse failed: {}", j.error());
wpi::util::print(stderr, "{}\n", err);
m_websocket->Fail(1003, err);
return;
}
// Save last message received
m_json = j;
m_json = *j;
});
m_websocket->closed.connect([this](uint16_t, auto) {

View File

@@ -7,6 +7,7 @@
#include <memory>
#include <string>
#include <thread>
#include <utility>
#include <gtest/gtest.h>
@@ -78,14 +79,13 @@ TEST_F(WebServerIntegrationTest, DISABLED_DigitalOutput) {
bool test_value = true; // Default value from HAL initialization
try {
auto& msg = m_webserverClient->GetLastMessage();
test_type = msg.at("type").get_ref<const std::string&>();
test_device = msg.at("device").get_ref<const std::string&>();
test_type = msg.at("type").get_string();
test_device = msg.at("device").get_string();
auto& data = msg.at("data");
wpi::util::json::const_iterator it = data.find("<>value");
if (it != data.end()) {
test_value = it.value();
if (auto val = data.lookup("<>value"); val && val->is_bool()) {
test_value = val->get_bool();
}
} catch (wpi::util::json::exception& e) {
} catch (std::logic_error& e) {
wpi::util::print(stderr, "Error with incoming message: {}\n", e.what());
}
@@ -109,10 +109,11 @@ TEST_F(WebServerIntegrationTest, DISABLED_DigitalInput) {
return;
}
if (IsConnectedClientWS()) {
wpi::util::json msg = {{"type", "DIO"},
{"device", std::to_string(PIN)},
{"data", {{"<>value", EXPECTED_VALUE}}}};
wpi::util::print("***** Input JSON: {}\n", msg.dump());
wpi::util::json msg = wpi::util::json::object();
msg["type"] = "DIO";
msg["device"] = std::to_string(PIN);
msg["data"] = wpi::util::json::object("<>value", EXPECTED_VALUE);
wpi::util::print("***** Input JSON: {}\n", msg.to_string());
m_webserverClient->SendMessage(msg);
done = true;
}
@@ -143,11 +144,14 @@ TEST_F(WebServerIntegrationTest, DriverStation) {
return;
}
if (IsConnectedClientWS()) {
wpi::util::json msg = {
{"type", "DriverStation"},
{"device", ""},
{"data", {{">enabled", EXPECTED_VALUE}, {">new_data", true}}}};
wpi::util::print("***** Input JSON: {}\n", msg.dump());
auto data = wpi::util::json::object();
data[">enabled"] = EXPECTED_VALUE;
data[">new_data"] = true;
wpi::util::json msg = wpi::util::json::object();
msg["type"] = "DriverStation";
msg["device"] = "";
msg["data"] = std::move(data);
wpi::util::print("***** Input JSON: {}\n", msg.to_string());
m_webserverClient->SendMessage(msg);
done = true;
}