Step 6: File input/output

Now that we can create contribution objects and convert them to and from JSON, the next step is to manage a list of contributions and make this list persistent in a JSON file.

We will achieve this with a service called se_contribution_list. Besides maintaining a persistent list of contribution objects, this service will have two more responsibilities:

se_contribution_list will have two public commands:

The signature of command add_contribution is defined as follows:

   command add_contribution
      in first_name type:user_name end
      in last_name type:user_name end
      in random_number type:random_1_15 end
      in error_handler type:system_error_handler default:se_system_utilities.default_system_error_handler end
      
      out result type:contribution voidable:yes end
      out error type:system_error voidable:yes end
      out_check check: i_result =r void xor i_error =r void end

      script
      end
   end

As you can see, there is again an error_handler input argument and an error output argument because many things can go wrong. For example the input can be invalid or there is a file write error.

Command get_list can also fail and is therefore defined like this:

   command get_list
      in error_handler type:system_error_handler default:se_system_utilities.default_system_error_handler end
      
      out result type:!indexed_list<contribution> voidable:yes end
      out error type:system_error voidable:yes end
      out_check check: i_result =r void xor i_error =r void end
      
      script
      end
   end

Note the type for output argument result: !indexed_list<contribution>. Here we use a generic type that denotes an indexed_list containing only contribution objects. The ! introduces the usage of a generic type, and the value between < and > defines the type of object stored in the list.

[Note]Note
For Java programmers: Although the syntax for generic types in Obix is similar to the syntax used in Java, generic types in Obix are conceptually different from those in Java. For example, in Obix there is no type erasure at runtime. This is important because type safety is always guaranteed and type parameter information is available at runtime.

Now we are ready to write se_contribution_list. The full code with comments and a global test script at the end is shown below. To create the service: