function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
mysteriousauramysteriousaura 

Pros and Cons typed vs untyped JSON de-serializers

Hi,

I was curious if anyone has any opinion about using typed vs untyped JSON de-serializers in salesforce. What are the pros and cons specially from a ISV or product dev perspective.
Best Answer chosen by mysteriousaura
pconpcon
Personally, I like the ability to have methods that do things against my object (such as the getLineItemTotal method in the blog post) that having it untyped does not allow for.  Again this would all depend on what type of product you are developing.  If you are doing something like developing a dashboard based on data coming in from an external source and you know the structure of the data but you want the end user to be able to manpulate that data and add more (but not change the overall structure) then untyped would be better.  But if you are controlling the data as well as the Apex code then typed would give you a better result in the long run.  I would say that even if you add new fields, most of the time you will still have to update your Apex code to do something reasonable with those new fields, so having untyped doesn't buy you much.

Also, writing Apex tests against typed JSON work is much easier.  This is because you can quickly generate a new class instance and pass that around in your test.  With the deserialized version you are passing around raw JSON all the time.  Also, if your JSON structure changes you can update the typed data and you'll instantly get Apex compile errors if you miss something where as with untyped you may not know until you hit that path of your code.

All Answers

pconpcon
I would try to use typed JSON de-serialization if at all possible.  This just give you better testability and makes it easier to reference / use.  Take a look at this post [1] to see what I mean.  The untyped has some use if you have no idea what's coming in like when developing JSON parsers or soemthing apex lodash [2].  Unless you know 100% that all of your data will always be there I would steer clear of deserializedStrict.

[1] http://blog.deadlypenguin.com/blog/2015/11/30/json-deserialization-in-salesforce/
[2] https://github.com/apex-lodash/lo
mysteriousauramysteriousaura
Thanks pcon!

For most part, I am pretty sure of what fields will be in the JSON, but need to make sure it will scale in the long run! I have worked with typed before and it makes it super easy but the counter argument being what if the JSON changes or we add more fields. I will have to compile all the apex classes again - does not work so well when you are developing a product?
Do you still think you would choose typed vs untyped? Is untyped strictly when you have no idea what the JSON looks like? My experience with untyped is that you still have to kind of know some sort of structure, you can't make it completely dynamic! Is that true?
pconpcon
Personally, I like the ability to have methods that do things against my object (such as the getLineItemTotal method in the blog post) that having it untyped does not allow for.  Again this would all depend on what type of product you are developing.  If you are doing something like developing a dashboard based on data coming in from an external source and you know the structure of the data but you want the end user to be able to manpulate that data and add more (but not change the overall structure) then untyped would be better.  But if you are controlling the data as well as the Apex code then typed would give you a better result in the long run.  I would say that even if you add new fields, most of the time you will still have to update your Apex code to do something reasonable with those new fields, so having untyped doesn't buy you much.

Also, writing Apex tests against typed JSON work is much easier.  This is because you can quickly generate a new class instance and pass that around in your test.  With the deserialized version you are passing around raw JSON all the time.  Also, if your JSON structure changes you can update the typed data and you'll instantly get Apex compile errors if you miss something where as with untyped you may not know until you hit that path of your code.
This was selected as the best answer
mysteriousauramysteriousaura
Thanks pcon!
This helps!