The parser can contain several dictionaries - add multiple dictionaries to one text block or execute the method "addDictionaryFromText" several times. You can switch dictionaries rapidly for creating binary documents. When the parser decodes binary documents it selects the dictionary automatically.

Step 2: Create DOM-object

Let's create a DOM-object which will be encoded in the binary document USDS.Add the first root element "rootObject" and initialize it:

One of the fields is an array of the tag; its size is zero at the moment. Let's initialize it:

// Init array field, get link from the structUsdsArray* array_field = tag->getArrayField("arrayField");for(int i =0; i <2; i++){// Add new element to the array, get link to this elementUsdsStruct* struct_element =(UsdsStruct*)(array_field->addTagElement());// Init elementstruct_element->setFieldValue("varintField", i);struct_element->setFieldValue("doubleField", i /2.0);struct_element->setFieldValue("stringField","String value");struct_element->setFieldValue("booleanField", i ==0);}

Initializing the array of the structures is done by:

Add new empty element to the array using the method "addTagElement";

Initialize the added structure.

In the example above, we used the text names of the fields for initialization. This method takes a lot of processor time, because the parser has to find the fields in the dictionary. The better method is to find integer identifiers for all tags and fields and to use them for initialization. Let's do it:

// Now use these IDs// Add second structure to the DOM objecttag = clientParser->addStructTag(rootObject_id);// Init simple fieldstag->setFieldValue(rootObject_intField_id,4321);tag->setFieldValue(rootObject_longField_id,6000000000);// Init array field, get link from the structarray_field = tag->getArrayField(rootObject_arrayField_id);for(int i =0; i <2; i++){// Add new element to the array, get link to this elementUsdsStruct* struct_element =(UsdsStruct*)(array_field->addTagElement());// Init elementstruct_element->setFieldValue(internalObject_varintField_id, i *2);struct_element->setFieldValue(internalObject_doubleField_id, i /3.0);struct_element->setFieldValue(internalObject_stringField_id,"Second root object");struct_element->setFieldValue(internalObject_booleanField_id, i !=0);}

If you know all integer identifiers beforehand (they are indicated in the text dictionary), you can use them at once.

Step 3: Create JSON

We have two tags in the DOM-object. Let's look at them in JSON format:

On the screen we see the size of the binary document - "USDS binary document
size: 286 bytes". The dictionary is about half of the document (140 bytes). If you add to the DOM-object not 2, but 1000 tags, the ratio "dictionary - body" will be better. If the client and the server know the dictionary beforehand, then the client can create a binary document without a dictionary:

clientParser->encode(usds_binary_doc,true,false,true);

Step 5: Cleaning DOM-object

Let's clear away the DOM-object, but we will not delete the dictionary in the parser, so as not to reinitialize:

clientParser->clearBody();

The objects of the classes inside the parser were not destroyed--they will be used again in the next DOM-object (Object Pool). It makes the process faster. All objects will be destroyed when the parser destructor is executed.

The binary document is still available inside the object "usds_binary_doc"; you can get the link to the data array:

Server Side

Step 1: Create Parser

The dictionary initialization isn't necessary - the parser will get the dictionary from the first USDS binary document (if you added it to the document). The parser stores all unique dictionaries before the method "Clear" is executed.

Step 2: Read the Binary Document

Let's read the binary document:

serverParser->decode(binary_data, binary_size);

Now the DOM-object is ready. You can look at it in JSON format:

// DOM object created from the binary// Let's look what we have in itjson.clear();serverParser->getJSON(USDS_UTF8,&json);std::cout<<"JSON:\n"<< json <<"\n";std::cout<<"JSON string size: "<< json.size()<<" symbols\n\n";

As we can see, the document is correct. We can now read the DOM-object.

Step 3: Reading Tags and Fields

Let us assume that we do not know the structure of the binary document. Let's check the type and the name of the first element:

Text Box

// Extract first root objectUsdsBaseType* someTag = serverParser->getFirstTag();std::cout<<"The type of the first Tag: '"<< someTag->getTypeName()<<"'\n";std::cout<<"The name of the first Tag: '"<< someTag->getName()<<"'\nFields:\n";if(someTag->getType()!= USDS_STRUCT)return1;elsetag =(UsdsStruct*)someTag;

On the screen we have the following:

The type of the first Tag: 'STRUCT'The name of the first Tag: 'rootObject'

This is a good response for our program. We can try to read the fields: