Friday, 20 April 2018

ASN.1: Specifying OPTIONAL fields

Sometimes, you may not require to fill all the data, some data may be optional also. In that cases, use the OPTIONAL keyword to specify optional (not mandatory) fields.

Example
favoriteColor VisibleString OPTIONAL

Let me explain with an example.

Example Schema
Person ::= SEQUENCE {
  firstName VisibleString,
  lastName VisibleString,
  id VisibleString,
  favoriteColor VisibleString
}

Try to encode below data using above schema.
person Person ::= {
  firstName "Rama Krishna",
  lastName "Gurram",
  id "I123456" 
}


You will end up in below kind of error.


Failed to parse value notation. Missing mandatory component 'favoriteColor' in SET or SEQUENCE value notation:
{ firstName "Rama Krishna", lastName "Gurram", id "I123456"

To make the things work, either set the favoriteColor as OPTIONAL or pass the value to favoriteColor.
Person ::= SEQUENCE {
  firstName VisibleString,
  lastName VisibleString,
  id VisibleString,
  favoriteColor VisibleString OPTIONAL
}





Previous                                                 Next                                                 Home

No comments:

Post a Comment