Skip to content

A lightweight C++ JSON library for fast JSON parsing, editing, and stringification.

License

Notifications You must be signed in to change notification settings

littlepeace2005/peace-json

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

47 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Peace JSON

peace-json is a lightweight, open source JSON library written in C++. It offers efficient JSON deserialization and serialization capabilities for projects requiring fast and flexible JSON handling.

Features

  • Fast Deserialization: Efficiently parses JSON string.
  • Simple Serialization: Easily converts C++ objects back into JSON string.
  • STL Integration: Seamlessly integrates with C++ STL, with strings, objects, and arrays derived from std::string, std::unordered_map, and std::vector.

Getting Started

To use peace-json in your project, follow these steps:

Prerequisites

C++ compiler that supports C++11 or later.

Installation

  1. Clone the repository:
    git clone https://github.com/littlepeace2005/peace-json.git
    
  2. Include the header in your project:
    #include "json.h"

Usage

Parsing JSON

std::string personStr = R"(
  {
    "name": "John Smith",
    "age": 27,
    "is_alive": true,
    "address": {
      "street_address": "21 2nd Street",
      "city": "New York",
      "state": "NY",
      "postal_code": "10021-3100"
    },
    "phone_numbers": [
      {
        "type": "home",
        "number": "212 555-1234"
      },
      {
        "type": "office",
        "number": "646 555-4567"
      }
    ],
    "children": [
      "Catherine",
      "Thomas",
      "Trevor"
    ],
    "spouse": null
  }
)";

peace::Json person = peace::Json::parse(personStr);

Stringifying JSON

peace::Json person = peace::JsonObject{
  {"name", "John Smith"},
  {"age", 27},
  {"is_alive", true},
  {"address", peace::JsonObject{
    {"street_address", "21 2nd Street"},
    {"city", "New York"},
    {"state", "NY"},
    {"postal_code", "10021-3100"}
  }},
  {"phone_numbers", peace::JsonArray{
    peace::JsonObject{
      {"type", "home"},
      {"number", "212 555-1234"}
    },
    peace::JsonObject{
      {"type", "office"},
      {"number", "646 555-4567"}
    }
  }},
  {"children", peace::JsonArray{
    "Catherine",
    "Thomas",
    "Trevor"
  }},
  {"spouse", peace::JsonNULL}
};

std::string personStr = peace::Json::stringify(person);

Reading from and Writing to Files

This example demonstrates how to read JSON from a file, modify its content, and then write it to another file.

template.json
{
  "name": null,
  "age": null,
  "phone_numbers": [
    {"type": "home", "number": null},
    {"type": "office", "number": null}
  ],
  "spouse": null
}
main.cc
#include <fstream>
#include "json.h"
using namespace peace;

int main() try
{
  std::fstream file;
  Json person;

  // read JSON from a file
  file.open("template.json", std::ios_base::in);
  if (file) {
    file >> person; file.close();
  } else
    throw std::runtime_error("Failed to open the file");

  // modify JSON data
  person["name"] = "John Smith";
  person["age"] = 27;
  person["is_alive"] = true;
  person["address"]["street_address"] = "21 2nd Street";
  person["address"]["city"] = "New York";
  person["address"]["state"] = "NY";
  person["address"]["postal_code"] = "10021-3100";
  person["phone_numbers"][0]["number"] = "212 555-1234";
  person["phone_numbers"][1]["number"] = "646 555-4567";
  person["children"] = JsonArray{"Catherine", "Thomas", "Trevor"};

  // write modified JSON to a file
  file.open(person["name"].asString() + ".json", std::ios_base::out);
  if (file) {
    file << person; file.close();
  } else
    throw std::runtime_error("Failed to open the file");

  return EXIT_SUCCESS;
}
catch(const std::exception &e)
{
  // exception handling
  std::cerr << e.what() << std::endl;
  return EXIT_FAILURE;
}

Contributing

Contributions are welcome.

License

peace-json is licensed under the MIT License. See the LICENSE file for more details.

About

A lightweight C++ JSON library for fast JSON parsing, editing, and stringification.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published