Reading PJSIP settings from XML and JSON files in non-serial way

comments

I just implemented open-source library for reading/writing settings from JSON and XML files based on PJSIP PJSUA2 persistent object model. You can find PJSettings library on GitHub.

xml-based settings

pjSettings::PugixmlDocument class uses pugixml to read from/write to XML files.

So, you can use code from PJSUA2 book to write settings to file:

using namespace pj;
using namespace pjsettings;
EpConfig epCfg;
PugixmlDocument doc;
epCfg.uaConfig.maxCalls = 61;
epCfg.uaConfig.userAgent = "Just XML Test";
doc.writeObject(epCfg);
doc.saveFile("xmltest.xml");

and read settings from file:

using namespace pj;
using namespace pjsettings;
EpConfig epCfg;
PugixmlDocument doc;
doc.loadFile("xmltest.xml");
doc.readObject(epCfg);

xmltest.xml file will look as:

<?xml version="1.0"?>
<root>
    <EpConfig>
        <UaConfig maxCalls="61" threadCnt="1" mainThreadOnly="false" userAgent="Just XML Test" stunIgnoreFailure="true" natTypeInSdp="1" mwiUnsolicitedEnabled="true">
            <nameserver />
            <stunServer />
        </UaConfig>
        <LogConfig msgLogging="1" level="5" consoleLevel="4" decor="25328" filename="" fileFlags="0" />
        <MediaConfig clockRate="16000" sndClockRate="0" channelCount="1" audioFramePtime="20" maxMediaPorts="254" hasIoqueue="true" threadCnt="1" quality="8" ptime="0" noVad="false" ilbcMode="30" txDropPct="0" rxDropPct="0" ecOptions="0" ecTailLen="200" sndRecLatency="100" sndPlayLatency="140" jbInit="-1" jbMinPre="-1" jbMaxPre="-1" jbMax="-1" sndAutoCloseTime="1" vidPreviewEnableNative="true" />
    </EpConfig>
</root>

Note that xml element names and attributes is listed in the order in which they were written. But when you read xml document with PugixmlDocument order of xml element names and attributes is not important.

json-based settings

pjSettings::JsonCppDocument class uses jsoncpp to read from/write to XML files.

So, if you run code above with JsonCppDocument class instead PugixmlDocument and jsontest.json file instead xmltest.xml, jsontest.json file will look as:

{
    "EpConfig" :
    {
        "LogConfig" :
        {
            "consoleLevel" : 4,
            "decor" : 25328,
            "fileFlags" : 0,
            "filename" : "",
            "level" : 5,
            "msgLogging" : 1
        },
        "MediaConfig" :
        {
            "audioFramePtime" : 20,
            "channelCount" : 1,
            "clockRate" : 16000,
            "ecOptions" : 0,
            "ecTailLen" : 200,
            "hasIoqueue" : true,
            "ilbcMode" : 30,
            "jbInit" : -1,
            "jbMax" : -1,
            "jbMaxPre" : -1,
            "jbMinPre" : -1,
            "maxMediaPorts" : 254,
            "noVad" : false,
            "ptime" : 0,
            "quality" : 8,
            "rxDropPct" : 0,
            "sndAutoCloseTime" : 1,
            "sndClockRate" : 0,
            "sndPlayLatency" : 140,
            "sndRecLatency" : 100,
            "threadCnt" : 1,
            "txDropPct" : 0,
            "vidPreviewEnableNative" : true
        },
        "UaConfig" :
        {
            "mainThreadOnly" : true,
            "maxCalls" : 61,
            "mwiUnsolicitedEnabled" : true,
            "nameserver" : [],
            "natTypeInSdp" : 1,
            "stunIgnoreFailure" : true,
            "stunServer" : [],
            "threadCnt" : 1,
            "userAgent" : "Just JSON Test"
        }
    }
}

Note that all properties is lexical sorted - it is jsoncpp feature. It can help easy compare configuration files in SCM and on production servers.

Also note that pj::JsonDocument from PJSUA2 doesn't support non-serially access to properties: order of reading properties must be same as order of writing properties (and if order of properties in file is differ, you get error). In PJSettings classes (PugixmlDocument and JsonCppDocument) non-serial access to properties is supported.

I hope this library will be helpful for you.

Comments