Does JSON Serialization Break SObject Equality?

Question:
When an SObject containing Decimal
fields is serialized and deserialized using JSON, the equality operator (==
) no longer returns true
, even if the field values appear identical. Why does this happen, and how can it be fixed?
For example:
Opportunity o1 = new Opportunity(Amount = 12.3);
Opportunity o2 = (Opportunity) JSON.deserialize(JSON.serialize(o1), Opportunity.class);
System.debug(o1 == o2); // false
Interestingly, if the Amount
field values are logged:
System.debug(o1.Amount); // 12.3
System.debug(o2.Amount); // 12.3
The values look identical, yet o1 == o2
evaluates to false
.
In contrast, SObjects without Decimal
fields, such as:
Opportunity o3 = new Opportunity(Name = 'Example');
Opportunity o4 = (Opportunity) JSON.deserialize(JSON.serialize(o3), Opportunity.class);
System.debug(o3 == o4); // true
Answer:
The issue arises because JSON serialization and deserialization can subtly alter the internal representation of Decimal
fields. While the deserialized value may display identically when logged, it is no longer considered the same instance or representation, breaking the equality contract.
Join CRS Info Solutions, a leading institute for  Salesforce training in Kolkata. Sign up for a free demo session today!!!
To fix this, you can explicitly cast the Decimal
fields back to their original type after deserialization.
Here’s the corrected code:
Opportunity o1 = new Opportunity(Amount = 12.3);
Opportunity o2 = (Opportunity) JSON.deserialize(JSON.serialize(o1), Opportunity.class);
System.debug(o1.Amount); // 12.3
System.debug(o2.Amount); // 12.3
System.debug(o1 == o2); // false
// Fix: Recast the Amount field
o2.Amount = (Decimal) o2.Amount;
System.debug(o1 == o2); // true
Explanation:
The code demonstrates that JSON serialization and deserialization can alter the internal representation of Decimal
fields, causing the equality operator (==
) to return false
even when the values appear identical. Recasting the Decimal
field after deserialization ensures the internal representations match, restoring equality.
Master Salesforce Skills with Expert Training in Kolkata
Take your Salesforce expertise to new heights with professional Salesforce Course in Kolkata, tailored for beginners and seasoned professionals alike. Learn from industry-certified instructors and gain hands-on experience with real-world projects. Whether you’re looking to master cloud-based CRM solutions or enhance your career prospects,, this comprehensive training program offers the skills and confidence needed to excel in the Salesforce ecosystem.
CRS Info Solutions offers an industry-leading Salesforce training in Kolkata, featuring a practical, project-driven curriculum. Dive into Salesforce Admin, Developer, Integration, Marketing Cloud, CPQ, and Lightning Web Components (LWC) with expert guidance and interactive sessions.
Reserve your spot for a free demo today at CRS Info Solutions and take the first step toward an exciting career in Salesforce!!!