在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:spotify/spotify-json开源软件地址:https://github.com/spotify/spotify-json开源编程语言:C++ 96.7%开源软件介绍:spotify-jsonA C++17 JSON writer and parser library. It
spotify-json depends on Google's double-conversion library, which must be linked in to the code that uses spotify-json. Example#include <iostream>
#include <map>
#include <string>
#include <spotify/json.hpp>
using namespace spotify::json;
struct Track {
std::string uri;
std::string uid;
std::map<std::string, std::string> metadata;
};
namespace spotify {
namespace json {
// Specialize spotify::json::default_codec_t to specify default behavior when
// encoding and decoding objects of certain types.
template <>
struct default_codec_t<Track> {
static object_t<Track> codec() {
auto codec = object<Track>();
codec.required("uri", &Track::uri);
codec.optional("uid", &Track::uid);
codec.optional("metadata", &Track::metadata);
return codec;
}
};
} // namespace json
} // namespace spotify
int main() {
const auto parsed_track = decode<Track>(R"({ "uri": "spotify:track:xyz", "metadata": { "a": "b" } })");
std::cout << "Parsed track with uri " << parsed_track.uri << std::endl;
Track track;
track.uri = "spotify:track:abc";
track.uid = "a-uid";
const auto json = encode(track);
std::cout << "Encoded the track into " << json << std::endl;
return 0;
} Usagespotify-json offers a range of codec types that can serialize and parse specific JSON values. There are codecs for each of the basic data types that JSON offers: strings, numbers, arrays, booleans, objects and null. Constructing and composing codecsA codec for integers can be made using
Codecs are composable. It is for example possible to construct a codec for
parsing and serialization of JSON arrays of numbers, such as Constructing deeply nested codecs manually as above can become tedious. To ease
this pain, It is possible to work with JSON objects with arbitrary keys. For example,
Parsing and serializationParsing is done using the try {
decode(codec::number<int>(), "123") == 123;
decode<int>("123") == 123; // Shortcut for decode(default_codec<int>(), "123")
decode<std::vector<int>>("[1,2,3]") == std::vector{ 1, 2, 3 };
} catch (const decode_exception &e) {
std::cout << "Failed to decode: " << e.what() << std::endl;
}
int result = 0;
if (try_decode(result, "123")) {
result == 123;
} else {
// Decoding failed!
} Similarly, serialization is done using encode(codec::number<int>(), 123) == "123";
encode(123) == "123"; // Shortcut for encode(default_codec<int>(), 123)
encode(std::vector<int>{ 1, 2, 3 }) == "[1,2,3]"; Working with rich objectsWorking with basic types such as numbers, strings, booleans and arrays is all nice and dandy, but most practical applications need to deal with rich JSON schemas that involve objects. Many JSON libraries work by parsing JSON strings into a tree structure that can be read by the application. In our experience, this approach often leads to large amounts of boilerplate code to extract the information in this tree object into statically typed counterparts that are practical to use in C++. This boilerplate is painful to write, bug-prone and slow due to unnecessary copying. SAX-style event based libraries such as yajl avoid the slowdown but require even more boilerplate. spotify-json avoids these issues by parsing the JSON directly into statically
typed data structures. To explain how, let's use the example of a basic
two-dimensional coordinate, represented in JSON as struct Coordinate {
Coordinate() = default;
Coordinate(int x, int y) : x(x), y(y) {}
int x = 0;
int y = 0;
}; With spotify-json, it is possible to construct a codec that can convert
auto coordinate_codec = object<Coordinate>();
coordinate_codec.required("x", &Coordinate::x);
coordinate_codec.required("y", &Coordinate::y); The use of This codec can be used with encode(coordinate_codec, Coordinate(10, 0)) == R"({"x":10,"y":0})";
const Coordinate coord = decode(coordinate_codec, R"({ "x": 12, "y": 13 })");
coord.x == 12;
coord.y == 13; Objects can be nested. To demonstrate this, let's introduce another data type: struct Player {
std::string name;
std::string instrument;
Coordinate position;
}; A codec for auto player_codec = object<Player>();
player_codec.required("name", &Player::name);
player_codec.required("instrument", &Player::instrument);
// Because there is no default_codec for Coordinate, we need to pass in the
// codec explicitly:
player_codec.required("position", &Player::position, coordinate_codec);
// Let's use it:
Player player;
player.name = "Daniel";
player.instrument = "guitar";
encode(player_codec, player) == R"({"name":"Daniel","instrument":"guitar","position":{"x":0,"y":0}})"; Since codecs are just normal objects, it is possible to create and use
several different codecs for any given data type. This makes it possible to
parameterize parsing and do other fancy things, but for most data types there
will only really exist one codec. For these cases, it is possible to extend
the namespace spotify {
namespace json {
template <>
struct default_codec_t<Coordinate> {
static object_t<Coordinate> codec() {
auto codec = object<Coordinate>();
codec.required("x", &Coordinate::x);
codec.required("y", &Coordinate::y);
return codec;
}
};
template <>
struct default_codec_t<Player> {
static object_t<Player> codec() {
auto codec = object<Player>();
codec.required("name", &Player::name);
codec.required("instrument", &Player::instrument);
codec.required("position", &Player::position);
return codec;
}
};
} // namespace json
} // namespace spotify
encode(Coordinate(10, 0)) == R"({"x":10,"y":0})";
decode<std::vector<Coordinate>>(R"([{ "x": 1, "y": -1 }])") == std::vector<Coordinate>{ Coordinate(1, -1) };
Player player;
player.name = "Martin";
player.instrument = "drums";
encode(player) == R"({"name":"Martin","instrument":"drums","position":{"x":0,"y":0}})"; Advanced usageThe examples above cover the most commonly used parts of spotify-json. The library supports more things that sometimes come in handy:
Detailed API documentationLinking against the library in a projectIf your project is built with CMake, it is easy to use spotify-json. Here is an example of how it can be done:
Building and running testsRequirements
1. Make CMake find Boost
2. Run CMake
Run "cmake --help" for a list of generators available on your system. 3. Build project with Visual Studio / Xcode / Ninja4. Run CTest
Code of conductThis project adheres to the Open Code of Conduct. By participating, you are expected to honor this code. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论