Procedure for Calling JSON Encode Methods

Once an object's member variables have been populated, the object's encode method can be invoked to encode the value. The general procedure to do this involves the following three steps:

  1. Create an encode output stream into which the value will be encoded.

  2. Invoke the encode method.

  3. Close the output stream.

The first step is the creation of an encode output stream, an Asn1JsonOutputStream. There is a single constructor which accepts a java.io.Writer. As usual, you may use a buffered writer, file writer, char array writer etc., or some combination thereof.

The second step is the invocation of the encode method. The calling arguments were described earlier.

Finally, close the output stream.

A complete example showing how to invoke an encode method is as follows:

   // Note: personnelRecord object was previously populated with data
            
   Asn1JsonOutputStream encodeStream = null;
            
   try {
      // Step 1: Create an encode output stream, with UTF-8 character encoding
      encodeStream = new Asn1JsonOutputStream (
      new java.io.OutputStreamWriter( new java.io.FileOutputStream(filename), "UTF-8") );
      
      // Step 2: Invoke the encode method.
      
      personnelRecord.encode (encodeStream);
      
      if (trace) {
         System.out.println ("Encoding was successful");
      }
   }
   catch (Exception e) {
      System.out.println(e.getMessage());
      e.printStackTrace();
   }
   finally {
      try {
         if (encodeStream != null) encodeStream.close ();
      }
      catch (Exception e) {}
   }