Reuse of C# Decoding Objects

The sample above showed the JSON decoding of a single message. In a typical application, a loop would be involved to decode a series of messages.

A single decode buffer can be used to process a stream of messages (assuming all messages are using the same character encoding). If the decode buffer is created using an input stream that contains a series of messages (for example, a file containing multiple records, or a communications device), you can repeatedly invoke the JSON decode method on the given message type.

Note that you can also use the same instance of your message type for repeated decoding, rather than creating a new object and leaving the old one to be garbage collected. Nothing special needs to be done to do this. The generated decode method will automatically call the internal Init() method before decoding to make sure all items are reset to their starting state.

In the example above, all that would need to be done to decode a series of personnel records is the inclusion of a loop after the PersonnelRecord object was created in step 2:

   for (;;) {
      personnelRecord.Decode (decodeBuffer);

      if (trace) {
         System.Console.Out.WriteLine ("Decode was successful");
         personnelRecord.Print ("personnelRecord");
      }
   }