Convert Figma logo to code with AI

jbeder logoyaml-cpp

A YAML parser and emitter in C++

5,054
1,812
5,054
333

Top Related Projects

8,063

A C++ library for interacting with JSON.

42,154

JSON for Modern C++

14,146

A fast JSON parser/generator for C++ with both SAX/DOM style API

Quick Overview

yaml-cpp is a YAML parser and emitter in C++. It is designed to be a lightweight and fast YAML implementation that is compatible with the YAML 1.2 specification. The library provides a simple and intuitive API for parsing and generating YAML data.

Pros

  • Lightweight and Fast: yaml-cpp is designed to be a lightweight and fast YAML implementation, making it suitable for use in performance-critical applications.
  • Comprehensive YAML Support: The library supports the full YAML 1.2 specification, including features like anchors, aliases, and tags.
  • Intuitive API: The API provided by yaml-cpp is simple and easy to use, making it accessible to developers of all skill levels.
  • Cross-Platform: yaml-cpp is a cross-platform library that can be used on a variety of operating systems, including Windows, macOS, and Linux.

Cons

  • Limited Documentation: The project's documentation could be more comprehensive, which may make it more difficult for new users to get started with the library.
  • Lack of Active Maintenance: The project has not been actively maintained in recent years, which could be a concern for some users who require ongoing support and updates.
  • Limited Ecosystem: Compared to some other YAML libraries, yaml-cpp has a relatively small ecosystem of third-party tools and integrations, which may limit its usefulness in certain scenarios.
  • Dependency on Boost: yaml-cpp depends on the Boost library, which may be a concern for some users who prefer to avoid large dependencies.

Code Examples

Here are a few examples of how to use yaml-cpp:

  1. Parsing YAML Data:
#include <iostream>
#include <yaml-cpp/yaml.h>

int main() {
    YAML::Node config = YAML::LoadFile("config.yaml");
    std::string name = config["name"].as<std::string>();
    int age = config["age"].as<int>();
    std::cout << "Name: " << name << ", Age: " << age << std::endl;
    return 0;
}
  1. Generating YAML Data:
#include <iostream>
#include <yaml-cpp/yaml.h>

int main() {
    YAML::Emitter out;
    out << YAML::BeginMap;
    out << YAML::Key << "name" << YAML::Value << "John Doe";
    out << YAML::Key << "age" << YAML::Value << 30;
    out << YAML::EndMap;
    std::cout << out.c_str() << std::endl;
    return 0;
}
  1. Handling Exceptions:
#include <iostream>
#include <yaml-cpp/yaml.h>

int main() {
    try {
        YAML::Node config = YAML::LoadFile("config.yaml");
        // Use the config data
    } catch (const YAML::Exception& e) {
        std::cerr << "YAML exception: " << e.what() << std::endl;
        return 1;
    }
    return 0;
}

Getting Started

To get started with yaml-cpp, follow these steps:

  1. Install the library:

    • On Windows, you can use a package manager like vcpkg or Conan to install yaml-cpp.
    • On macOS or Linux, you can install yaml-cpp using a package manager like Homebrew or apt-get.
  2. Include the yaml-cpp header in your C++ file:

    #include <yaml-cpp/yaml.h>
    
  3. Link against the yaml-cpp library in your build process.

  4. Start using the library to parse and generate YAML data. Refer to the code examples above for guidance on common usage patterns.

Competitor Comparisons

8,063

A C++ library for interacting with JSON.

Pros of jsoncpp

  • Simpler syntax and structure, making it easier to learn and use for JSON parsing
  • Faster parsing and serialization performance for JSON data
  • More widespread adoption and community support for JSON-related tasks

Cons of jsoncpp

  • Limited to JSON format, while yaml-cpp supports both YAML and JSON
  • Less flexible for complex data structures compared to YAML's advanced features
  • Lacks some of the more advanced features present in yaml-cpp, such as anchors and aliases

Code Comparison

yaml-cpp:

YAML::Node config = YAML::LoadFile("config.yaml");
std::string name = config["name"].as<std::string>();
int age = config["age"].as<int>();
std::vector<std::string> hobbies = config["hobbies"].as<std::vector<std::string>>();

jsoncpp:

Json::Value root;
std::ifstream config_file("config.json");
config_file >> root;
std::string name = root["name"].asString();
int age = root["age"].asInt();
std::vector<std::string> hobbies;
for (const auto& hobby : root["hobbies"])
    hobbies.push_back(hobby.asString());
42,154

JSON for Modern C++

Pros of json

  • Header-only library, easier to integrate
  • More intuitive API with operator overloading
  • Better performance for parsing and serialization

Cons of json

  • Limited to JSON format only
  • Larger compile times due to heavy template usage
  • Less flexible for custom data types

Code Comparison

json:

#include <nlohmann/json.hpp>
using json = nlohmann::json;

json j = {
  {"name", "John"},
  {"age", 30},
  {"city", "New York"}
};
std::string s = j.dump();

yaml-cpp:

#include <yaml-cpp/yaml.h>

YAML::Node node;
node["name"] = "John";
node["age"] = 30;
node["city"] = "New York";
std::string s = YAML::Dump(node);

Both libraries offer straightforward ways to create and manipulate data structures. json uses a more modern C++ approach with its use of initializer lists and method chaining, while yaml-cpp follows a more traditional style. json's syntax is generally more concise, but yaml-cpp offers greater flexibility in terms of supported formats and custom types.

14,146

A fast JSON parser/generator for C++ with both SAX/DOM style API

Pros of rapidjson

  • Faster parsing and serialization performance
  • Supports in-situ parsing, reducing memory allocation
  • More comprehensive JSON support, including JSON Pointer and JSON Schema

Cons of rapidjson

  • Limited to JSON format only, while yaml-cpp supports YAML
  • Steeper learning curve due to more complex API
  • Requires more manual memory management in some cases

Code Comparison

yaml-cpp:

YAML::Node config = YAML::LoadFile("config.yaml");
std::string name = config["name"].as<std::string>();
int age = config["age"].as<int>();

rapidjson:

rapidjson::Document doc;
doc.Parse(json_string);
std::string name = doc["name"].GetString();
int age = doc["age"].GetInt();

Summary

rapidjson is a high-performance JSON parser and generator for C++, while yaml-cpp is a YAML parser and emitter for C++. rapidjson excels in speed and JSON-specific features, making it ideal for projects requiring fast JSON processing. yaml-cpp offers broader support for YAML format, which can be more human-readable and flexible. The choice between the two depends on the specific project requirements, such as data format needs, performance constraints, and ease of use.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

yaml-cpp Build Status Documentation

yaml-cpp is a YAML parser and emitter in C++ matching the YAML 1.2 spec.

Usage

See Tutorial and How to Emit YAML for reference. For the old API (until 0.5.0), see How To Parse A Document.

Any Problems?

If you find a bug, post an issue! If you have questions about how to use yaml-cpp, please post it on http://stackoverflow.com and tag it yaml-cpp.

How to Build

yaml-cpp uses CMake to support cross-platform building. Install CMake (Resources -> Download) before proceeding. The basic steps to build are:

Note: If you don't use the provided installer for your platform, make sure that you add CMake's bin folder to your path.

1. Navigate into the source directory, create build folder and run CMake:

mkdir build
cd build
cmake [-G generator] [-DYAML_BUILD_SHARED_LIBS=on|OFF] ..
  • The generator option is the build system you'd like to use. Run cmake without arguments to see a full list of available generators.

    • On Windows, you might use "Visual Studio 12 2013" (VS 2013 32-bits), or "Visual Studio 14 2015 Win64" (VS 2015 64-bits).
    • On OS X, you might use "Xcode".
    • On a UNIX-like system, omit the option (for a Makefile).
  • yaml-cpp builds a static library by default, you may want to build a shared library by specifying -DYAML_BUILD_SHARED_LIBS=ON.

  • Debug mode of the GNU standard C++ library can be used when both yaml-cpp and client code is compiled with the _GLIBCXX_DEBUG flag (e.g. by calling CMake with -D CMAKE_CXX_FLAGS_DEBUG='-g -D_GLIBCXX_DEBUG' option).

    Note that for yaml-cpp unit tests to run successfully, the GoogleTest library also must be built with this flag, i.e. the system one cannot be used (the YAML_USE_SYSTEM_GTEST CMake option must be OFF, which is the default).

  • For more options on customizing the build, see the CMakeLists.txt file.

2. Build it!

  • The command you'll need to run depends on the generator you chose earlier.

Note: To clean up, just remove the build directory.

How to Integrate it within your project using CMake

You can use for example FetchContent :

include(FetchContent)

FetchContent_Declare(
  yaml-cpp
  GIT_REPOSITORY https://github.com/jbeder/yaml-cpp.git
  GIT_TAG <tag_name> # Can be a tag (yaml-cpp-x.x.x), a commit hash, or a branch name (master)
)
FetchContent_MakeAvailable(yaml-cpp)

target_link_libraries(YOUR_LIBRARY PUBLIC yaml-cpp::yaml-cpp) # The library or executable that require yaml-cpp library

Recent Releases

yaml-cpp 0.6.0 released! This release requires C++11, and no longer depends on Boost.

yaml-cpp 0.3.0 is still available if you want the old API.

The old API will continue to be supported, and will still receive bugfixes! The 0.3.x and 0.4.x versions will be old API releases, and 0.5.x and above will all be new API releases.

API Documentation

The autogenerated API reference is hosted on CodeDocs

Third Party Integrations

The following projects are not officially supported: