C++ Logo

std-proposals

Advanced search

Re: [std-proposals] A Minimal JSON Support Library for C++

From: Tiago Freire <tmiguelf_at_[hidden]>
Date: Tue, 13 Feb 2024 18:41:09 +0000
I think that parsers are an immediate no go for standardization.
They are better kept for an independent library project.
They just have too many behavior details that are simply unrealistic to force upon developers as default.

What’s the underlying character type? char? char8_t? char16_t? char32_t?
How does it handle the polymorphic types?
Can I stream it from memory? Can I stream it from a file? Can I stream it with my own custom streamer?
What type of encodings does it support? And how do I choose it?
What is the view model? Is it a copy of the block into memory? Is it a mapping of the stream block into a reference?
How do you handle errors?
Can I diagnose errors as it is being parsed?
How do I handle meta-data?

You may think that there is a “standard way” of doing it, but there’s no such thing.
I can give you many different design choices, one is not necessarily better than the other, it is only better depending on what you do with it.

These are my 2cents


From: Std-Proposals <std-proposals-bounces_at_[hidden]> On Behalf Of Yexuan Xiao via Std-Proposals
Sent: Tuesday, February 13, 2024 6:47 PM
To: std-proposals_at_[hidden]
Cc: Yexuan Xiao <bizwen_at_[hidden]>
Subject: [std-proposals] A Minimal JSON Support Library for C++

Hello everyone, I recently designed a JSON library that is suitable to the C++ standard, but I don’t have much experience with using allocators, so I need an allocator expert to help me improve it. Any suggestions are welcome and I hope you like it.

https://storage.nykz.org/proposals/minimal-json/

Here are some use cases:

#include "basic_json.hpp"
#include <string_view>
int main()
{
      /*
          Note: This code is for demonstration purposes only and can be compiled only, not be run.
      */
      // json
      using json = bizwen::json;
      using namespace std::literals;
      json j01{};
      json j02{ j01 };
      // j j03{ nullptr }; deleted
      json j04{ 1. };
      json j05{ true };
      json j06{ 1.f };
      json j07{ 1u };
      json j08{ 1l };
      json j09{ 1ll };
      json j10{ 1ull };
      json j11{ "aaa" };
      auto str = "bbb";
      json j12{ str, str + 3 };
      json j13{ str, 3 };
      json j14{ str };
      json j15{ "bbb"sv };
      // since initializer_list returns a reference to a const object, this method is inefficient
      // json j16{ json::array_type{ json{0}, json{1} } };
      // json j17{ json::object_type{ { "key0"s, json{ 0 } }, { "key1"s, json{ 1 } } } };
      json j16{ json::array{ 0, 1 } };
      json j17{ json::object{"key0"s, 0, "key1"s, 1} };
      json j18{ bizwen::basic_json_node<>{} };
      swap(j17, j18); // adl hidden friend
      j17.swap(j18);
      j17 = j18;
      std::swap(j17, j18);
      json::node_type n{ std::move(j18) };

      // const_slice
      using const_slice = bizwen::const_json_slice;
   const_slice c1;
      c1.empty();
      c1.array();
      c1.string();
      c1.null();
      c1.boolean();
      c1.number();
      c1.object();
      c1.array();
      c1.object();
      c1.integer();
      c1.uinteger();
      c1["key"];
      c1["key"s];
      // requires transparent comparable
      // c1["key"sv];
      c1[1];
      const_slice c2{ j17 };
      c2.swap(c1);
      std::swap(c1, c2);
      const_slice c3{ std::move(j17) };
      const_slice c4 = c3;
      const_slice c5 = std::move(c4);
      c4 = c5;
      c5 = std::move(c4);
      bool b{ c5 };
      bizwen::nulljson_t nj{ c5 };
      json::string_type const& s{ c5 };
      json::array_type const& a{ c5 };
      for (auto const& i : a)
      {
            const_slice item{ i };
      }
      json::object_type const& o{ c5 };
      for (auto const& [k, v] : o)
      {
            const_slice item{ v };
      }
      long long ll{ c5 };
      unsigned long long ull{ c5 };

      // slice
      using slice = bizwen::json_slice;
      slice s1{};
      slice s2{ j17 };
      const_slice c6 = s2;
      std::string str1;
      s2 = str1;
      s2 = std::string{};
      s2 = "aaa";
      s2 = std::string_view{};
      s2 = bizwen::nulljson;
      s2 = true;
      s2 = 1.;
      s2 = 1;
      s2 = 1u;
      s2 = 1ll;
      s2 = 1ull;
      s2 = n;
      s2["key"] = 1;
}

Received on 2024-02-13 18:41:12