Skip to content

Commit

Permalink
fix JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
Reputeless committed Dec 12, 2023
1 parent ce9bd82 commit 8c4b879
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 62 deletions.
19 changes: 17 additions & 2 deletions Siv3D/include/Siv3D/JSON.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,10 @@ namespace s3d
JSON(std::nullptr_t);

SIV3D_NODISCARD_CXX20
JSON(const JSON&) = default;
JSON(const JSON& other);

SIV3D_NODISCARD_CXX20
JSON(JSON&&) = default;
JSON(JSON&& other) noexcept;

SIV3D_NODISCARD_CXX20
JSON(const std::initializer_list<std::pair<String, JSON>>& list);
Expand Down Expand Up @@ -308,6 +308,8 @@ namespace s3d
// JSONValueType::Object
JSON& operator =(const JSON& value);

JSON& operator =(JSON&& value) noexcept;

// JSONValueType::Object
JSON& operator =(const std::initializer_list<std::pair<String, JSON>>& list);

Expand Down Expand Up @@ -637,6 +639,19 @@ namespace s3d
[[nodiscard]]
Blob toMessagePack() const;

/// @brief JSON オブジェクトを交換します。
/// @param other 交換する JSON オブジェクト
void swap(JSON& other) noexcept;

/// @brief 2 つの JSON オブジェクトを交換します。
/// @param lhs JSON オブジェクト
/// @param rhs JSON オブジェクト
friend void swap(JSON& lhs, JSON& rhs) noexcept
{
lhs.m_detail.swap(rhs.m_detail);
std::swap(lhs.m_isValid, rhs.m_isValid);
}

// @brief 無効な JSON オブジェクトを返します。
// @return 無効な JSON オブジェクト
[[nodiscard]]
Expand Down
95 changes: 35 additions & 60 deletions Siv3D/src/Siv3D/JSON/SivJSON.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,14 @@ namespace s3d
JSON::JSON(std::nullptr_t)
: m_detail{ std::make_shared<detail::JSONDetail>() } {}

JSON::JSON(const JSON& other)
: m_detail{ std::make_shared<detail::JSONDetail>(*other.m_detail) }
, m_isValid{ other.m_isValid } {}

JSON::JSON(JSON&& other) noexcept
: m_detail{ std::exchange(other.m_detail, std::make_shared<detail::JSONDetail>()) }
, m_isValid{ std::exchange(other.m_isValid, false) } {}

JSON::JSON(std::shared_ptr<detail::JSONDetail>&& detail)
: m_detail{ std::move(detail) } {}

Expand Down Expand Up @@ -524,122 +532,83 @@ namespace s3d

JSON& JSON::operator =(std::nullptr_t)
{
if (not m_isValid)
{
return *this;
}

m_detail->get() = nullptr;

m_isValid = true;
return *this;
}

JSON& JSON::operator =(const JSON& value)
JSON& JSON::operator =(const JSON& other)
{
if (not m_isValid)
{
return *this;
}

m_detail->get() = value.m_detail->get();
m_detail = std::make_shared<detail::JSONDetail>(*other.m_detail);
m_isValid = other.m_isValid;
return *this;
}

JSON& JSON::operator =(JSON&& other) noexcept
{
m_detail = std::exchange(other.m_detail, std::make_shared<detail::JSONDetail>());
m_isValid = std::exchange(other.m_isValid, false);
return *this;
}

JSON& JSON::operator =(const std::initializer_list<std::pair<String, JSON>>& list)
{
if (not m_isValid)
{
return *this;
}
m_detail->get().clear();

for (const auto& element : list)
{
m_detail->get()[Unicode::ToUTF8(element.first)] = element.second.m_detail->get();
}

m_isValid = true;
return *this;
}

JSON& JSON::operator =(const Array<JSON>& array)
{
if (not m_isValid)
{
return *this;
}
m_detail->get() = nlohmann::json::array();

if (array)
for (const auto& element : array)
{
for (const auto& element : array)
{
m_detail->get().push_back(element.m_detail->get());
}
}
else
{
m_detail->get() = nlohmann::json::array();
m_detail->get().push_back(element.m_detail->get());
}

m_isValid = true;
return *this;
}

JSON& JSON::operator =(const StringView value)
{
if (not m_isValid)
{
return *this;
}

m_detail->get() = Unicode::ToUTF8(value);

m_isValid = true;
return *this;
}

JSON& JSON::operator =(const int64 value)
{
if (not m_isValid)
{
return *this;
}

m_detail->get() = value;

m_isValid = true;
return *this;
}

JSON& JSON::operator =(const uint64 value)
{
if (not m_isValid)
{
return *this;
}

m_detail->get() = value;

m_isValid = true;
return *this;
}

JSON& JSON::operator =(const double value)
{
if (not m_isValid)
{
return *this;
}

m_detail->get() = value;

m_isValid = true;
return *this;
}

JSON& JSON::operator =(const bool value)
{
if (not m_isValid)
{
return *this;
}

m_detail->get() = value;

m_isValid = true;
return *this;
}

Expand Down Expand Up @@ -1156,6 +1125,12 @@ namespace s3d
return Blob{ result.data(), result.size() };
}

void JSON::swap(JSON& other) noexcept
{
m_detail.swap(other.m_detail);
std::swap(m_isValid, other.m_isValid);
}

JSON JSON::Invalid()
{
return JSON(Invalid_{});
Expand Down

0 comments on commit 8c4b879

Please sign in to comment.