summaryrefslogtreecommitdiffstats
path: root/veslibrary/ves_cpplibrary/src/lib/encode/XInternal.h
blob: 38aa1dbc49d034165b36286869704a832f43c7db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#pragma once

#include "XEvent.h"
#include "nlohmann/json.hpp"

#ifndef SPDLOG_ACTIVE_LEVEL 
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_TRACE
#endif

#include "spdlog/spdlog.h"

using namespace std;
using namespace vagt::encode;
using json = nlohmann::json;

namespace vagt
{
    namespace encode
    {
        class XJsonable
        {
        public:
            virtual json toJson() = 0;
        };

        class XCommonValue
        {
        public:
            XCommonValue() :dataType_(XDataTypeUnknown) {}
            XCommonValue(const std::string& val) :dataType_(XDataTypeString), strVal_(val) {}
            XCommonValue(XInteger val) :dataType_(XDataTypeInteger), intVal_(val) {}
            XCommonValue(XNumber val) :dataType_(XDataTypeNumber), numberVal_(val) {}

            XDataType dataType_;
            XInteger intVal_;
            XNumber numberVal_;
            XString strVal_;
        };

        json mergeCommonValues(json jsonObj, const std::map<std::string, XCommonValue>& values);

        template <class T>
        inline json fromArray(const T& v)
        {
            json jsonArray = json::array();
            for (auto e : v)
            {
                jsonArray.push_back(dynamic_pointer_cast<XJsonable>(e.imp_)->toJson());
            }
            return jsonArray;
        }
    }
}