Procedure for Calling Java 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 java.io.Reader or a java.io.InputStream. In the latter case, the character encoding can be detected (except on Java ME, where it is assumed to be UTF-8). 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
      FileInputStream ins = new FileInputStream (filename);

      // 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.out.println ("Decode was successful");
         personnelRecord.print (System.out, "personnelRecord", 0);
      }
   }
   catch (Exception e) {
      System.out.println (e.getMessage());
      e.printStackTrace();
      return;
   }