summaryrefslogtreecommitdiffstats
path: root/veslibrary/ves_cpplibrary/src/lib/encode/XJsonObjectInstanceImp.cpp
blob: e371f6ee41bb1ba6fb0ae2cb7b9e9ff08c84cd58 (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
#include "XJsonObjectInstanceImp.h"

XJsonObjectInstanceImp::XJsonObjectInstanceImp():XJsonObjectInstance(nullptr)
{
}

void XJsonObjectInstanceImp::setJsonObject(const XJsonObject& jsonObject)
{
    jsonObject_ = jsonObject.imp_;
}
            
void XJsonObjectInstanceImp::setObjectInstance(const XString& objectInstance)
{
    objectInstance_ = objectInstance;
}
            
void XJsonObjectInstanceImp::setObjectInstanceEpochMicrosec(XNumber objectInstanceEpochMicrosec)
{
    comVals_["objectInstanceEpochMicrosec"] = objectInstanceEpochMicrosec;
}
            
void XJsonObjectInstanceImp::setObjectKeys(const XArrayOfXKey& objectKeys)
{
    objectKeys_ = objectKeys;
}

json XJsonObjectInstanceImp::toJson()
{
    try
    {
        json field;

        //optional val
        if (jsonObject_)
        {
            auto jsobj = dynamic_pointer_cast<XJsonable>(jsonObject_);
            if (!jsobj->toJson().empty())
            {
                field["jsonObject"] = jsobj->toJson();
            }
        }

        if (!objectKeys_.empty())
        {
            field["objectKeys"] = fromArray(objectKeys_);
        }

        if (!objectInstance_.empty())
        {
            field["objectInstance"] = json::parse(objectInstance_);
        }

        field = mergeCommonValues(field, comVals_);

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