Skip to content

caozhanhao/libczh

Repository files navigation

libczh

An easy-to-use data serialization format

build License Release

Document Document

Example

Tutorial

Syntax

Type

  • int,double,string,bool,Array,Reference

Statement

  • Indentation is not required
  • The ; after the statement is not required

Comment

  • <xxxx>

Node

  • Duplicate names are not allowed
  • Node: id: end
  • Value: id = xxx

Array

  • {} .

Reference

  • id = a::b::c::id

Usage of libczh

Setup

  • just #include "libczh/czh.hpp"!
  • Requires C++ 20

Czh::Czh(str, mode)

mode
  • czh::InputMode::file -> str is a path
  • czh::InputMode::string -> str is a std::string where czh is stored
  Czh("example: a = 1; end;", czh::InputMode::string);

Node::operator[str]

  • Returns a Node named str

Node::operator(str)

  • Similar to Node::operator[str], but it provides a better error message.

Node::get()

  • When the Array value's type in czh is not unique, T must be czh::value::Array
auto arr = node["czh"]["any_array"].get<czh::value::Array>();

value_map

  • When the values under Node are of the same type, use value_map() to get a std::map consisting of all ids and values.

Node::value_map()

  • Returns std::map<std::string, T>
auto value_map = example["example"]["arrays"].value_map<vector<int>>();
example: 
  arrays:
    a = {1,2,3}
    b = {1,2,3}
  end
end

Node::operator=(value)

node["czh"]["int_array"] = Range(1, 10);        // custom container
node["czh"]["int_array"] = std::ranges::views::iota(1,10); // std::ranges
node["czh"]["int_array"] = {1, 2, 3};           // brace-enclosed initializer list
node["czh"]["any_array"] = {false, 1, "2", 3.0};// czh::value::Array

Add

Node::add(key, value, before)
  • Add a new Value named key whose value is value before the Node named before. -before defaults to be empty, which will add at the end.
  • Returns a reference to the added Node.
example["add"].add("add", "123", "abc");
Node::add_node(name, before)
  • Add a new Node named name before the Node named before.
  • Returns a reference to the added Node.
example.add_node("new", "before");

Remove

Node::remove()
example["example"].remove();

Clear

Node::clear()
example["example"].clear();

Rename

Node::rename(name, newname)

example["a"].rename("b");

Output

Writer
  • libczh originally support three writers
Writer Format
BasicWriter No Format
PrettyWriter Format
ColorWriter Format + Highlight(ANSI Escape Code)
Node::accept()
  • accept a Writer
    writer::BasicWriter<std::ostream> w{ std::cout };
node.accept(w);
operator<<
  • equal to BasicWriter
Write a Writer
  • All we need is to write a class satisfied the following concept.
template<typename T>
concept Writer =
requires(T w)
{
{ w.node_begin(std::string()) };
{ w.node_end() };
{ w.value_begin(std::string()) };
{ w.value(value::Value{}) };
{ w.value_ref_path_set_global() };
{ w.value_ref_path(std::string()) };
{ w.value_ref_id(std::string()) };
{ w.value_array_begin() };
{ w.value_array_value(value::Array::value_type{}) };
{ w.value_array_end(value::Array::value_type{}) };
{ w.value_array_end() };
};

Contact

  • If you have any questions or suggestions, please submit an issue or email me.
  • Email: cao2013zh at 163 dot com

Contribution

  • Any contributions are welcomed, just send a PR.

License