Serialize and Deserialize Enums in JSON in Salesforce Apex

In our Salesforce org, we were working on integrating our Salesforce instance with a third-party ticketing system for managing customer support cases. The goal was to sync the status of support cases between Salesforce and this external system using an API.

We need to send the current case status to the third-party system via API. Since enums are more efficient for managing statuses, we decided to serialize the enum into a JSON format.

In this Salesforce tutorial, I will explain how to serialize and deserialize Enums in JSON Apex.

Serialize and Deserialize Enums in JSON in Salesforce Apex

To serialize and deserialize enums in JSON in Salesforce Apex, we will use the JSON.serialize() nad JSON.deserialize() methods.

First, we will create an apex class with enum values that we will serialize and deserialize in the further steps.

Create a new Apex class in the Salesforce developer console by selecting File > New > Apex Class. After this, enter the code in the new Apex class.

    public class Enumserialization {
        public enum Status {
            Active,
            Inactive,
            Pending
        }
    }

    Now, we will serialize the ENUM by converting it into a JSON string. To do that, enter the code below in the apex class.

      public class EnumSerialization {
          public enum Status {
              Active,
              Inactive,
              Pending
          }
      
          public static void serializeEnum() {
              Status currentStatus = Status.Active;
              String jsonString = JSON.serialize(currentStatus);
              System.debug('Serialized Enum: ' + jsonString); 
          }
      }
      

      To deserialize the ENUM, we will use JSON.deserialize() method, enter the code below the existing serialization method.

         public static void deserializeEnum(String jsonString) {
                Status status = Status.valueOf(jsonString.replace('"', ''));
                System.debug('Deserialized Enum: ' + status); 
            }

        Now, save the apex class and launch the anonymous window. Then, enter the code below in the anonymous window.

          EnumSerialization.serializeEnum(); 
          EnumSerialization.deserializeEnum('"Active"');

          In the execution log window, we can see the output for the serialized and deserialized values in the debug.

          Serialize and deserailize Enums in JSON in Salesforce Apex

          This way, we can serialize and deserialize the Enums in JSON in Salesforce Apex using the JSON.serialize and JSON.deserialize methods.

          Using Enum.valueOf for Deserialization of Enum in Salesforce Apex

          In Salesforce Apex, we can use the Enum.valueOf method to directly convert a string into the corresponding enum value. This approach is useful when you want to avoid JSON.deserialize method.

          To deserialize an Enum using the Enum.Use the valueOf method, open the Salesforce developer console, and follow the steps below.

          In the Salesforce developer console, create a new Apex class and enter the following.

            public class DeserializeEnum {
                public enum Status {
                    Active,
                    Inactive,
                    Pending
                }
            
                public static void deserializeWithValueOf(String statusString) {
                    Status status = Status.valueOf(statusString);
                    System.debug('Deserialized Enum using valueOf: ' + status);
                }
            }

            In the above code, deserializeWithValueOf takes a single parameter, statusString, that will be a string representation of one of the enum values.

            After this, statusString is deserialized into the corresponding Status enum value using the valueOf method.

            The valueOf method checks if the string matches one of the enum constants. If it does, it returns the matching enum value. I