<commit_before>//
//  ofxJsonParseFunctions.h
//
//  Created by ISHII 2bit on 2016/05/19.
//
//

#pragma once

#include "ofMain.h"
#include "json_compatible.h"

namespace ofx {
    namespace JsonUtils {
        namespace detail {
            template <typename T>
            class has_loadJson {
            private:
                template <typename U, U> struct really_has;
                template <typename C>
                static std::true_type Test(really_has<void (C::*)(const ofJson &), &C::loadJson> *);
                template <typename>
                static std::false_type Test(...);
                
            public:
                static bool const value = decltype(Test<T>(0))::value;
            };
            
            static bool has_keys(const ofJson &json, const std::vector<std::string> &keys) {
                auto end = json.end();
                bool all = true;
                for(const auto &key : keys) {
                    all &= (json.find(key) != end);
                }
                return all;
            }
        };
        
        static constexpr char const skip_invalid_format[] = "skip: invalid format.";
        static constexpr char const skip_json_isnt_string[] = "skip: json isn't string.";
        static constexpr char const skip_json_isnt_number[] = "skip: json isn't number.";
        static constexpr char const skip_json_isnt_array[] = "skip: json isn't array.";
        static constexpr char const skip_json_isnt_object[] = "skip: json isn't object.";
        static constexpr char const skip_json_isnt_object_or_array[] = "skip: json isn't object or array.";
        
        inline void parse(const ofJson &json, std::string &value) {
            if(!json.is_string()) return ofLogVerbose("ofxJsonUtils::parse string") << skip_json_isnt_string;
            value = json;
        }
        
        template <typename T>
        auto parse(const ofJson &json, T &value)
        -> typename std::enable_if<std::is_arithmetic<T>::value>::type {
            if(!json.is_number()) return ofLogVerbose("ofxJsonUtils::parse number") << skip_json_isnt_number;
            value = json;
        }
        
        template <typename T>
        auto parse(const ofJson &json, T &value)
        -> typename std::enable_if<detail::has_loadJson<T>::value>::type {
            value.loadJson(json);
        }
        
        static void parse(const ofJson &json, ofVec2f &v) {
            if(json.is_object()) {
                auto end = json.end();
                if(!detail::has_keys(json, {"x", "y"})) return ofLogVerbose("ofxJsonUtils::parse ofVec2f") << skip_invalid_format;<commit_msg>Fix boolean parse issue.<commit_after>//
//  ofxJsonParseFunctions.h
//
//  Created by ISHII 2bit on 2016/05/19.
//
//

#pragma once

#include "ofMain.h"
#include "json_compatible.h"

namespace ofx {
    namespace JsonUtils {
        namespace detail {
            template <typename T>
            class has_loadJson {
            private:
                template <typename U, U> struct really_has;
                template <typename C>
                static std::true_type Test(really_has<void (C::*)(const ofJson &), &C::loadJson> *);
                template <typename>
                static std::false_type Test(...);
                
            public:
                static bool const value = decltype(Test<T>(0))::value;
            };
            
            static bool has_keys(const ofJson &json, const std::vector<std::string> &keys) {
                auto end = json.end();
                bool all = true;
                for(const auto &key : keys) {
                    all &= (json.find(key) != end);
                }
                return all;
            }
        };
        
        static constexpr char const skip_invalid_format[] = "skip: invalid format.";
        static constexpr char const skip_json_isnt_string[] = "skip: json isn't string.";
        static constexpr char const skip_json_isnt_boolean[] = "skip: json isn't boolean.";
        static constexpr char const skip_json_isnt_number[] = "skip: json isn't number.";
        static constexpr char const skip_json_isnt_array[] = "skip: json isn't array.";
        static constexpr char const skip_json_isnt_object[] = "skip: json isn't object.";
        static constexpr char const skip_json_isnt_object_or_array[] = "skip: json isn't object or array.";
        
        inline void parse(const ofJson &json, std::string &value) {
            if(!json.is_string()) return ofLogVerbose("ofxJsonUtils::parse string") << skip_json_isnt_string;
            value = json;
        }
        
        inline void parse(const ofJson &json, bool &value) {
            if(!json.is_boolean()) return ofLogVerbose("ofxJsonUtils::parse boolean") << skip_json_isnt_boolean;
            value = json;
        }
        
        template <typename T>
        auto parse(const ofJson &json, T &value)
        -> typename std::enable_if<std::is_arithmetic<T>::value>::type {
            if(!json.is_number()) return ofLogVerbose("ofxJsonUtils::parse number") << skip_json_isnt_number;
            value = json;
        }
        
        template <typename T>
        auto parse(const ofJson &json, T &value)
        -> typename std::enable_if<detail::has_loadJson<T>::value>::type {
            value.loadJson(json);
        }
        
        static void parse(const ofJson &json, ofVec2f &v) {
            if(json.is_object()) {
                auto end = json.end();
                if(!detail::has_keys(json, {"x", "y"})) return ofLogVerbose("ofxJsonUtils::parse ofVec2f") << skip_invalid_format;
{'fraction_non_alphanumeric': 0.11612215909090909, 'fraction_numerical': 0.004261363636363636, 'mean_word_length': 2.0026652452025586, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 33, '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': '6315920', 'n_tokens_mistral': 1635, 'n_tokens_neox': 1559, 'n_words': 409}