Procedure for Calling C# JSON Decode Methods

The general procedure to decode an ASN.1 JSON message involves the following three steps:

  1. Create a decode buffer on the message to be decoded

  2. Invoke the decode method

  3. Process the decoded data values

The first step is the creation of a decode buffer. An Asn1JsonDecodeBuffer can be constructed on either a System.IO.TextReader or a System.IO.Stream. In the latter case, the character encoding is assumed to be UTF-8 if it cannot be detected (as long as the Stream is seekable, the character encoding can be detected). Thus, messages can easily be sourced from streams, files, or arrays.

The second step is to invoke the generated decode method. The calling arguments were described earlier.

The final step is to apply your application-specific processing to the data. All data is contained within public member variables so access is quite easy.

A complete example showing how to invoke a decode method is as follows:

   try {
      // Step 1: create a decode buffer for the message to be decoded. 
      // This example will use a file input stream to decode a message
      // in a binary file.

      // Create an input file stream object
      FileStream ins = new FileStream (filename, System.IO.FileMode.Open, System.IO.FileAccess.Read);

      // Create a decode buffer object
      Asn1JsonDecodeBuffer decodeBuffer = new Asn1JsonDecodeBuffer (ins);

      // Step 2: create an object of the generated type and invoke the
      // decode method..
      PersonnelRecord personnelRecord = new PersonnelRecord ();
      personnelRecord.Decode (decodeBuffer);

      // Step 3: process the data
      if (trace) {
         System.Console.Out.WriteLine("Decode was successful");
         personnelRecord.Print ("value");
      }
   }
   catch (Exception e) {
      System.Console.Out.WriteLine(e.Message);
      Asn1Util.WriteStackTrace(e, Console.Error);
      Environment.Exit(1);
   }