<commit_before>#pragma once

#include <stdexcept>
#include <string>
#include <vector>
#include <algorithm>

#include "acmacs-base/float.hh"

// ----------------------------------------------------------------------

class RapidjsonAssert : public std::exception { public: using std::exception::exception; };

#define RAPIDJSON_ASSERT(x) {if (!(x)) throw RapidjsonAssert{}; }

// ----------------------------------------------------------------------

#pragma GCC diagnostic push
#include "acmacs-base/rapidjson-diagnostics.hh"
#include "rapidjson/reader.h"
#include "rapidjson/document.h"
#include "rapidjson/error/en.h"
#include "rapidjson/istreamwrapper.h"
#pragma GCC diagnostic pop

// ----------------------------------------------------------------------

namespace json_importer
{
    template <typename Value> Value get(const rapidjson::Value& aValue, const char* aName);

    template<> inline std::string get(const rapidjson::Value& aValue, const char* aName) { return aValue[aName].GetString(); }
    template<> inline size_t get(const rapidjson::Value& aValue, const char* aName) { return aValue[aName].GetUint(); }
    template<> inline unsigned get(const rapidjson::Value& aValue, const char* aName) { return aValue[aName].GetUint(); }
    template<> inline int get(const rapidjson::Value& aValue, const char* aName) { return aValue[aName].GetInt(); }

    template<> inline rapidjson::Value::ConstArray get(const rapidjson::Value& aValue, const char* aName) { return aValue[aName].GetArray(); }

    template<> inline std::vector<std::string> get(const rapidjson::Value& aValue, const char* aName)
    {
        std::vector<std::string> result;
        const auto value = get<rapidjson::Value::ConstArray>(aValue, aName);
        std::transform(value.begin(), value.end(), std::back_inserter(result), [](const auto& elt) { return elt.GetString(); });
        return result;
    }

    template <typename Value> inline Value get(const rapidjson::Value& aValue, const char* aName, const Value& aDefault)
    {
        try {
            return get<Value>(aValue, aName);
        }
        catch (RapidjsonAssert&) {
            return aDefault;
        }
    }

// ----------------------------------------------------------------------

    class Object
    {
     public:
        inline Object(std::string aSrc) { mDoc.Parse(aSrc.c_str(), aSrc.size()); }
        inline Object(Object&& aSrc) : mDoc{std::move(aSrc.mDoc)} {}
        virtual inline ~Object() {}

        template <typename Value> inline Value get(const char* aName) const { return json_importer::get<Value>(mDoc, aName); }
        template <typename Value> inline Value get(const char* aName, const Value& aDefault) const { return json_importer::get(mDoc, aName, aDefault); }

        inline std::string get_string(const char* aName) const { return get(aName, std::string{}); }

     private:
        rapidjson::Document mDoc;

    }; // class Object

} // namespace json_importer

// ----------------------------------------------------------------------

class json_raw : public std::string
{
 public:
    inline json_raw(const std::string& src) : std::string{src} {}
    inline json_raw(std::string&& src) : std::string{std::move(src)} {}
};

namespace json_object_internal
{
    inline std::string make_value(std::string value) { return "\"" + value + "\""; }
    inline std::string make_value(json_raw value) { return value; }
    inline std::string make_value(const char* value) { return std::string{"\""} + value + "\""; }
    inline std::string make_value(bool value) { return value ? "true" : "false"; }
    inline std::string make_value(std::nullptr_t) { return "null"; }<commit_msg>json_importer get_array<commit_after>#pragma once

#include <stdexcept>
#include <string>
#include <vector>
#include <algorithm>

#include "acmacs-base/float.hh"

// ----------------------------------------------------------------------

class RapidjsonAssert : public std::exception { public: using std::exception::exception; };

#define RAPIDJSON_ASSERT(x) {if (!(x)) throw RapidjsonAssert{}; }

// ----------------------------------------------------------------------

#pragma GCC diagnostic push
#include "acmacs-base/rapidjson-diagnostics.hh"
#include "rapidjson/reader.h"
#include "rapidjson/document.h"
#include "rapidjson/error/en.h"
#include "rapidjson/istreamwrapper.h"
#pragma GCC diagnostic pop

// ----------------------------------------------------------------------

namespace json_importer
{
    using ConstArray = rapidjson::Value::ConstArray;

    template <typename Value> Value get(const rapidjson::Value& aValue, const char* aName);

    template<> inline std::string get(const rapidjson::Value& aValue, const char* aName) { return aValue[aName].GetString(); }
    template<> inline size_t get(const rapidjson::Value& aValue, const char* aName) { return aValue[aName].GetUint(); }
    template<> inline unsigned get(const rapidjson::Value& aValue, const char* aName) { return aValue[aName].GetUint(); }
    template<> inline int get(const rapidjson::Value& aValue, const char* aName) { return aValue[aName].GetInt(); }

    template<> inline ConstArray get(const rapidjson::Value& aValue, const char* aName) { return aValue[aName].GetArray(); }

    inline const char* get_string(const rapidjson::Value& aValue) { return aValue.GetString(); }

    template<> inline std::vector<std::string> get(const rapidjson::Value& aValue, const char* aName)
    {
        std::vector<std::string> result;
        const auto value = get<ConstArray>(aValue, aName);
          //std::transform(value.begin(), value.end(), std::back_inserter(result), [](const auto& elt) { return elt.GetString(); });
        std::transform(value.begin(), value.end(), std::back_inserter(result), &get_string);
        return result;
    }

    template <typename Value> inline Value get(const rapidjson::Value& aValue, const char* aName, const Value& aDefault)
    {
        try {
            return get<Value>(aValue, aName);
        }
        catch (RapidjsonAssert&) {
            return aDefault;
        }
    }

// ----------------------------------------------------------------------

    class Object
    {
     public:
        inline Object(std::string aSrc) { mDoc.Parse(aSrc.c_str(), aSrc.size()); }
        inline Object(Object&& aSrc) : mDoc{std::move(aSrc.mDoc)} {}
        virtual inline ~Object() {}

        template <typename Value> inline Value get(const char* aName) const { return json_importer::get<Value>(mDoc, aName); }
        template <typename Value> inline Value get(const char* aName, const Value& aDefault) const { return json_importer::get(mDoc, aName, aDefault); }

        inline std::string get_string(const char* aName) const { return get(aName, std::string{}); }
        inline json_importer::ConstArray get_array(const char* aName) const { return json_importer::get<json_importer::ConstArray>(mDoc, aName); } // throws RapidjsonAssert

     private:
        rapidjson::Document mDoc;

    }; // class Object

} // namespace json_importer

// ----------------------------------------------------------------------

class json_raw : public std::string
{
 public:
    inline json_raw(const std::string& src) : std::string{src} {}
    inline json_raw(std::string&& src) : std::string{std::move(src)} {}
};

namespace json_object_internal
{
    inline std::string make_value(std::string value) { return "\"" + value + "\""; }
    inline std::string make_value(json_raw value) { return value; }
    inline std::string make_value(const char* value) { return std::string{"\""} + value + "\""; }
    inline std::string make_value(bool value) { return value ? "true" : "false"; }
    inline std::string make_value(std::nullptr_t) { return "null"; }
{'fraction_non_alphanumeric': 0.22227965882657016, 'fraction_numerical': 0.0, 'mean_word_length': 2.718885151369534, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '296723', 'n_tokens_mistral': 2200, 'n_tokens_neox': 2173, 'n_words': 559}