Skip to content

Commit

Permalink
jsoncpp --> boost-json (#44)
Browse files Browse the repository at this point in the history
* Start Conversion to Boost.JSON

* Bump Version + Changelog

* More boost-json

* Update CMakeLists.txt

* Windows - Fix Build

* Update Changelog
  • Loading branch information
nlogozzo authored Sep 3, 2024
1 parent dfa26fc commit 814b419
Show file tree
Hide file tree
Showing 17 changed files with 162 additions and 95 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
uses: johnwason/vcpkg-action@v6
id: vcpkg
with:
pkgs: curl gettext-libintl glib gtest jsoncpp libsecret maddy openssl
pkgs: boost-json curl gettext-libintl glib gtest libsecret maddy openssl
triplet: x64-linux
revision: a4cfba036f013aea0347c3efc335186754b696b3
token: ${{ secrets.GITHUB_TOKEN }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
uses: johnwason/vcpkg-action@v6
id: vcpkg
with:
pkgs: curl gettext-libintl glib gtest jsoncpp maddy openssl
pkgs: boost-json curl gettext-libintl glib gtest maddy openssl
triplet: arm64-osx
revision: a4cfba036f013aea0347c3efc335186754b696b3
token: ${{ secrets.GITHUB_TOKEN }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
uses: johnwason/vcpkg-action@v6
id: vcpkg
with:
pkgs: curl gettext-libintl gtest jsoncpp maddy sqlcipher
pkgs: boost-json curl gettext-libintl gtest maddy sqlcipher
triplet: x64-windows
revision: a4cfba036f013aea0347c3efc335186754b696b3
token: ${{ secrets.GITHUB_TOKEN }}
Expand Down
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Changelog

## 2024.9.0 (next)
### Breaking Changes
- Replaced `jsoncpp` library with `boost-json`
#### App
- `Nickvision::App::DataFileBase`'s `m_json` object is now of type `boost::json::object`
### New APIs
None
### Fixes
None

## 2024.8.3
### Breaking Changes
#### Keyring
Expand Down
6 changes: 3 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ endif()
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake")

#libnick Definition
project ("libnick" LANGUAGES C CXX VERSION 2024.8.3 DESCRIPTION "A cross-platform base for native Nickvision applications.")
project ("libnick" LANGUAGES C CXX VERSION 2024.9.0 DESCRIPTION "A cross-platform base for native Nickvision applications.")
include(CMakePackageConfigHelpers)
include(GNUInstallDirs)
include(CTest)
Expand Down Expand Up @@ -83,10 +83,10 @@ else()
endif()

#libnick Packages
find_package(Boost REQUIRED COMPONENTS json)
find_package(CURL REQUIRED)
find_package(jsoncpp CONFIG REQUIRED)
find_package(Intl REQUIRED)
target_link_libraries(${PROJECT_NAME} PUBLIC CURL::libcurl JsonCpp::JsonCpp Intl::Intl)
target_link_libraries(${PROJECT_NAME} PUBLIC Boost::json CURL::libcurl Intl::Intl)
if(USING_VCPKG)
find_package(unofficial-maddy CONFIG REQUIRED)
target_link_libraries(${PROJECT_NAME} PUBLIC unofficial::maddy::maddy)
Expand Down
2 changes: 1 addition & 1 deletion Doxyfile
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ PROJECT_NAME = "libnick"
# could be handy for archiving the generated documentation or if some version
# control system is used.

PROJECT_NUMBER = "2024.8.3"
PROJECT_NUMBER = "2024.9.0"

# Using the PROJECT_BRIEF tag one can provide an optional one line description
# for a project that appears at the top of each page and should give viewer a
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ Documentation for this library and its modules can be found [here](https://nickv
The following are a list of dependencies used by libnick.

### All Platforms
- boost-json
- gtest
- jsoncpp
- libcurl
- libintl
- maddy
Expand Down Expand Up @@ -62,16 +62,16 @@ A C++20 compiler is also required to build libnick.
1. Set the `VCPKG_ROOT` environment variable to the path of your vcpkg installation's root directory.
#### Windows
1. Set the `VCPKG_DEFAULT_TRIPLET` environment variable to `x64-windows`
1. Run `vcpkg install curl gettext-libintl gtest jsoncpp maddy sqlcipher`
1. Run `vcpkg install boost-json curl gettext-libintl gtest maddy sqlcipher`
#### Linux
1. Set the `VCPKG_DEFAULT_TRIPLET` environment variable to `x64-linux`
1. Run `vcpkg install curl gettext-libintl glib gtest jsoncpp libsecret maddy openssl`
1. Run `vcpkg install boost-json curl gettext-libintl glib gtest libsecret maddy openssl`
#### macOS (Intel)
1. Set the `VCPKG_DEFAULT_TRIPLET` environment variable to `x64-osx`
1. Run `vcpkg install curl gettext-libintl glib gtest jsoncpp libsecret maddy openssl`
1. Run `vcpkg install boost-json curl gettext-libintl glib gtest libsecret maddy openssl`
#### macOS (Apple Silicon)
1. Set the `VCPKG_DEFAULT_TRIPLET` environment variable to `arm64-osx`
1. Run `vcpkg install curl gettext-libintl glib gtest jsoncpp libsecret maddy openssl`
1. Run `vcpkg install boost-json curl gettext-libintl glib gtest libsecret maddy openssl`

### Building
1. First, clone/download the repo.
Expand Down
2 changes: 1 addition & 1 deletion cmake/config.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ if(DEFINED ENV{VCPKG_ROOT})
endif()
endif()

find_dependency(Boost REQUIRED COMPONENTS json)
find_dependency(CURL REQUIRED)
find_dependency(jsoncpp CONFIG REQUIRED)
find_dependency(Intl REQUIRED)
if(USING_VCPKG)
find_dependency(unofficial-maddy REQUIRED)
Expand Down
4 changes: 2 additions & 2 deletions include/app/datafilebase.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

#include <filesystem>
#include <string>
#include <json/json.h>
#include <boost/json.hpp>
#include "events/event.h"

namespace Nickvision::App
Expand Down Expand Up @@ -65,7 +65,7 @@ namespace Nickvision::App
bool save();

protected:
Json::Value m_json;
mutable boost::json::object m_json;

private:
std::string m_key;
Expand Down
6 changes: 3 additions & 3 deletions include/network/web.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

#include <filesystem>
#include <string>
#include <json/json.h>
#include <boost/json.hpp>
#include "curleasy.h"

namespace Nickvision::Network::Web
Expand All @@ -39,9 +39,9 @@ namespace Nickvision::Network::Web
/**
* @brief Fetches a json string from a url.
* @param url The url of the json string
* @return The fetched json object
* @return The fetched json value
*/
Json::Value fetchJson(const std::string& url);
boost::json::value fetchJson(const std::string& url);
/**
* @brief Downloads a file to disk.
* @param url The url of the file to download
Expand Down
7 changes: 2 additions & 5 deletions manual/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@

libnick provides Nickvision apps with a common set of cross-platform APIs for managing system and desktop app functionality such as network management, taskbar icons, translations, app updates, and more.

## 2024.8.3
## 2024.9.0 (next)
### Breaking Changes
#### Keyring
- The `Nickvision::Keyring::Keyring` class has been rewritten for better performance and a cleaner API. Keyrings created with previous versions of libnick are no longer compatible.
- Removed `Nickvision::Keyring::KeyringDialogController`
- Removed `Nickvision::Keyring::Store`
- Replaced `jsoncpp` library with `boost-json`
### New APIs
None
### Fixes
Expand Down
38 changes: 21 additions & 17 deletions manual/datafiles.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Here are some key points when defining your own configuration objects:
- Although you will not use `key` and `appName` in your own implementation, it is required for `DataFileBase`'s functionality and will be filled-in by the `DataFileManager`.
- `DataFileBase` exposes a protected `m_json` object which you must use in your implementation of getting and storing variables of your data object.
- If this `m_json` object is not used, your data object will not be stored to disk correctly.
- You must explicitly call the `save` method on your configuration object when you want to save the configuration to disk. Writing to the `m_json` object is not enough to trigger saving the file on disk.
- You must explicitly call the `save` method on your configuration object when you want to save the configuration to disk. Writing to the `m_json` (of type `boost::json::object`) object is not enough to trigger saving the file on disk.

Here is an example of a custom configuration object using `DataFileBase`:
```cpp
Expand All @@ -16,22 +16,26 @@ using namespace Nickvision::Events;
class AppConfig : public DataFileBase
{
public:
AppConfig(const std::string& key, const std::string& appName)
: DataFileBase{ key, appName }
{

}

int getPreviousCount() const
{
//0 is the default value of PreviousCount (i.e. if it does not exist in the file)
return m_json.get("PreviousCount", 0).asInt();
}

void setPreviousCount(int count)
{
m_json["PreviousCount"] = count;
}
AppConfig(const std::string& key, const std::string& appName)
: DataFileBase{ key, appName }
{

}

bool getAutomaticallyCheckForUpdates() const
{
const boost::json::value& value{ m_json["AutomaticallyCheckForUpdates"] };
if(!value.is_bool())
{
return true;
}
return value.as_bool();
}

void setAutomaticallyCheckForUpdates(bool value)
{
m_json["AutomaticallyCheckForUpdates"] = value;
}
};

//This object can now be used with the DataFileManager:
Expand Down
21 changes: 18 additions & 3 deletions src/app/datafilebase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,23 @@ namespace Nickvision::App
m_path = UserDirectories::get(ApplicationUserDirectory::Config, appName) / (m_key + ".json");
if (std::filesystem::exists(m_path))
{
std::ifstream in{ m_path };
in >> m_json;
try
{
std::ifstream in{ m_path };
boost::json::stream_parser parser;
std::string line;
while(std::getline(in, line))
{
parser.write(line);
}
parser.finish();
boost::json::value value{ parser.release() };
if(value.is_object())
{
m_json = value.as_object();
}
}
catch(...) { }
}
}

Expand All @@ -39,7 +54,7 @@ namespace Nickvision::App
bool DataFileBase::save()
{
std::ofstream out{ m_path };
out << m_json;
out << m_json << std::endl;
m_saved({});
return true;
}
Expand Down
11 changes: 3 additions & 8 deletions src/network/web.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ namespace Nickvision::Network
return curl.perform() == CURLE_OK;
}

Json::Value Web::fetchJson(const std::string& url)
boost::json::value Web::fetchJson(const std::string& url)
{
if(url.empty())
{
return "";
return {};
}
CurlEasy curl{ url };
std::stringstream out;
Expand All @@ -31,12 +31,7 @@ namespace Nickvision::Network
std::string data{ out.str() };
if(!data.empty())
{
Json::Value root;
Json::Reader reader;
if(reader.parse(out.str(), root, false))
{
return root;
}
return boost::json::parse(data);
}
}
return {};
Expand Down
Loading

0 comments on commit 814b419

Please sign in to comment.