Resources | Resources |



IDL union instances in JSON

An IDL union is mapped to a JSON object with a case field for the discriminator, and a value field for the value.

typedef union Un switch(Color) 
   case RED:                   
      IFoo foo;                
   case YELLOW:                
   case GREEN:                 
      long lng;                
      short sh;                

is mapped as:
{"case": 1, "value": 123} 

Note: case is set to 1 and not YELLOW because JSON cannot reference named constants. In ECMAScript, the following is an example of how to write it:

var color = {"RED": 0, "YELLOW": 1, "GREEN": 2}
var un = {"case": color.YELLOW, "value": 123};