summaryrefslogtreecommitdiffstats
path: root/veslibrary/ves_cpplibrary/src/lib/encode/XOtherFieldsImp.cpp
blob: e9b4487a95f699f5b7c6c31d41cb5883b4ff6d8f (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "XOtherFieldsImp.h"

const std::string XEnumOtherFieldsOtherFieldsVersionMapping[] = {
    "3.0"
};

XOtherFieldsImp::XOtherFieldsImp(std::shared_ptr<XCommonEventHeader> header):
        header_(header),
        otherFieldsVersion_(XEnumOtherFieldsOtherFieldsVersion3_0)
{

}

std::string XOtherFieldsImp::toString()
{
    try
    {
        auto js = toJson();
        return js.dump();
    }
    catch (json::exception& e)
    {
        SPDLOG_ERROR("Fail to dump XOtherFields to json string:{}.", e.what());
        return "";
    }
}

void XOtherFieldsImp::setJsonObjects(const XArrayOfJsonObject& jsonObjects)
{
    jsonObjects_ = jsonObjects;
}
            
void XOtherFieldsImp::setArrayOfNamedHashMap(const XArrayOfNamedHashMap& arrayOfNamedHashMap)
{
    arrayOfNamedHashMap_ = arrayOfNamedHashMap;
}
            
void XOtherFieldsImp::setHashMap(const XHashMap& hashMap)
{
    hashMap_ = hashMap;
}

json XOtherFieldsImp::toJson()
{
    try
    {
        json field = {
            {"otherFieldsVersion", XEnumOtherFieldsOtherFieldsVersionMapping[otherFieldsVersion_]}
        };

        //optional val
        if (!jsonObjects_.empty())
        {
            field["jsonObjects"] = fromArray(jsonObjects_);
        }

        if (!arrayOfNamedHashMap_.empty())
        {
            field["arrayOfNamedHashMap"] = fromArray(arrayOfNamedHashMap_);
        }

        if (!hashMap_.empty())
        {
            field["hashMap"] = hashMap_;
        }

        json jsEvent;
        header_->setDomain(XEnumCommonEventHeaderDomainOther);
        jsEvent["event"]["commonEventHeader"] = dynamic_pointer_cast<XJsonable>(header_)->toJson();
        jsEvent["event"]["otherFields"] = field;

        return jsEvent;
    }
    catch (json::exception& e)
    {
        SPDLOG_ERROR("Fail to build XOtherFields to json object:{}.", e.what());
        return json();
    }
}

std::shared_ptr<XCommonEventHeader> XOtherFieldsImp::getCommonHeader()
{
    return header_;
}