Explicitly parse JSON string vs JSON.deserialize The 2019 Stack Overflow Developer Survey Results Are InJSON and escaped double quoteParse nested JSONJSON parsing to Visualforce page difficultiesDefault values for Wrapper variables not setConvert date in JSON to Date from StringMethod does not exist or incorrect signature: void parse(String) from the type or_propertyJSONTestHow to parse JSON String through apexDeserializing/Parsing the JSON response to an Apex classJson2apex - Message consuming unrecognized propertyParse JSON using APEX provided JSON Methods
I see my dog run
Understanding the implication of what "well-defined" means for the operation in quotient group
Why don't Unix/Linux systems traverse through directories until they find the required version of a linked library?
"Riffle" two strings
What does "rabbited" mean/imply in this sentence?
How to manage monthly salary
Deadlock Graph and Interpretation, solution to avoid
Are USB sockets on wall outlets live all the time, even when the switch is off?
What are my rights when I have a Sparpreis ticket but can't board an overcrowded train?
Inversion Puzzle
It's possible to achieve negative score?
Inflated grade on resume at previous job, might former employer tell new employer?
Which Sci-Fi work first showed weapon of galactic-scale mass destruction?
Limit the amount of RAM Mathematica may access?
Dual Citizen. Exited the US on Italian passport recently
CiviEvent: Public link for events of a specific type
Should I write numbers in words or as numerals when there are multiple next to each other?
What could be the right powersource for 15 seconds lifespan disposable giant chainsaw?
Why could you hear an Amstrad CPC working?
Is there a name of the flying bionic bird?
What can other administrators access on my machine?
What do the Banks children have against barley water?
Does light intensity oscillate really fast since it is a wave?
Why isn't airport relocation done gradually?
Explicitly parse JSON string vs JSON.deserialize
The 2019 Stack Overflow Developer Survey Results Are InJSON and escaped double quoteParse nested JSONJSON parsing to Visualforce page difficultiesDefault values for Wrapper variables not setConvert date in JSON to Date from StringMethod does not exist or incorrect signature: void parse(String) from the type or_propertyJSONTestHow to parse JSON String through apexDeserializing/Parsing the JSON response to an Apex classJson2apex - Message consuming unrecognized propertyParse JSON using APEX provided JSON Methods
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
The JSON2Apex web tool offers 2 methods of parsing of the JSON string: one uses JSON.deserialize method, and the other creates parser and iterates over the input json. The second option can be enabled by checking "Create explicit parse code" in the tool.
QUESTION
In what cases would a developer prefer explicit parsing
to a simple JSON.deserialize
? If we compare both options the later seems to be much clear and less verbose which makes code more readable.
Explicit parsing
public class JSON2Apex
public class User
public String name get;set;
public String twitter get;set;
public User(JSONParser parser)
while (parser.nextToken() != System.JSONToken.END_OBJECT)
if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME)
String text = parser.getText();
if (parser.nextToken() != System.JSONToken.VALUE_NULL)
if (text == 'name')
name = parser.getText();
else if (text == 'twitter')
twitter = parser.getText();
else
System.debug(LoggingLevel.WARN, 'User consuming unrecognized property: '+text);
consumeObject(parser);
public User user get;set;
public JSON2Apex(JSONParser parser)
while (parser.nextToken() != System.JSONToken.END_OBJECT)
if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME)
String text = parser.getText();
if (parser.nextToken() != System.JSONToken.VALUE_NULL)
if (text == 'user')
user = new User(parser);
else
System.debug(LoggingLevel.WARN, 'JSON2Apex consuming unrecognized property: '+text);
consumeObject(parser);
public static JSON2Apex parse(String json)
System.JSONParser parser = System.JSON.createParser(json);
return new JSON2Apex(parser);
public static void consumeObject(System.JSONParser parser)
Integer depth = 0;
do while (depth > 0 && parser.nextToken() != null);
JSON.deserialize
public class JSON2Apex
public class User
public String name;
public String twitter;
public User user;
public static JSON2Apex parse(String json)
return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);
json parsing
add a comment |
The JSON2Apex web tool offers 2 methods of parsing of the JSON string: one uses JSON.deserialize method, and the other creates parser and iterates over the input json. The second option can be enabled by checking "Create explicit parse code" in the tool.
QUESTION
In what cases would a developer prefer explicit parsing
to a simple JSON.deserialize
? If we compare both options the later seems to be much clear and less verbose which makes code more readable.
Explicit parsing
public class JSON2Apex
public class User
public String name get;set;
public String twitter get;set;
public User(JSONParser parser)
while (parser.nextToken() != System.JSONToken.END_OBJECT)
if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME)
String text = parser.getText();
if (parser.nextToken() != System.JSONToken.VALUE_NULL)
if (text == 'name')
name = parser.getText();
else if (text == 'twitter')
twitter = parser.getText();
else
System.debug(LoggingLevel.WARN, 'User consuming unrecognized property: '+text);
consumeObject(parser);
public User user get;set;
public JSON2Apex(JSONParser parser)
while (parser.nextToken() != System.JSONToken.END_OBJECT)
if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME)
String text = parser.getText();
if (parser.nextToken() != System.JSONToken.VALUE_NULL)
if (text == 'user')
user = new User(parser);
else
System.debug(LoggingLevel.WARN, 'JSON2Apex consuming unrecognized property: '+text);
consumeObject(parser);
public static JSON2Apex parse(String json)
System.JSONParser parser = System.JSON.createParser(json);
return new JSON2Apex(parser);
public static void consumeObject(System.JSONParser parser)
Integer depth = 0;
do while (depth > 0 && parser.nextToken() != null);
JSON.deserialize
public class JSON2Apex
public class User
public String name;
public String twitter;
public User user;
public static JSON2Apex parse(String json)
return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);
json parsing
add a comment |
The JSON2Apex web tool offers 2 methods of parsing of the JSON string: one uses JSON.deserialize method, and the other creates parser and iterates over the input json. The second option can be enabled by checking "Create explicit parse code" in the tool.
QUESTION
In what cases would a developer prefer explicit parsing
to a simple JSON.deserialize
? If we compare both options the later seems to be much clear and less verbose which makes code more readable.
Explicit parsing
public class JSON2Apex
public class User
public String name get;set;
public String twitter get;set;
public User(JSONParser parser)
while (parser.nextToken() != System.JSONToken.END_OBJECT)
if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME)
String text = parser.getText();
if (parser.nextToken() != System.JSONToken.VALUE_NULL)
if (text == 'name')
name = parser.getText();
else if (text == 'twitter')
twitter = parser.getText();
else
System.debug(LoggingLevel.WARN, 'User consuming unrecognized property: '+text);
consumeObject(parser);
public User user get;set;
public JSON2Apex(JSONParser parser)
while (parser.nextToken() != System.JSONToken.END_OBJECT)
if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME)
String text = parser.getText();
if (parser.nextToken() != System.JSONToken.VALUE_NULL)
if (text == 'user')
user = new User(parser);
else
System.debug(LoggingLevel.WARN, 'JSON2Apex consuming unrecognized property: '+text);
consumeObject(parser);
public static JSON2Apex parse(String json)
System.JSONParser parser = System.JSON.createParser(json);
return new JSON2Apex(parser);
public static void consumeObject(System.JSONParser parser)
Integer depth = 0;
do while (depth > 0 && parser.nextToken() != null);
JSON.deserialize
public class JSON2Apex
public class User
public String name;
public String twitter;
public User user;
public static JSON2Apex parse(String json)
return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);
json parsing
The JSON2Apex web tool offers 2 methods of parsing of the JSON string: one uses JSON.deserialize method, and the other creates parser and iterates over the input json. The second option can be enabled by checking "Create explicit parse code" in the tool.
QUESTION
In what cases would a developer prefer explicit parsing
to a simple JSON.deserialize
? If we compare both options the later seems to be much clear and less verbose which makes code more readable.
Explicit parsing
public class JSON2Apex
public class User
public String name get;set;
public String twitter get;set;
public User(JSONParser parser)
while (parser.nextToken() != System.JSONToken.END_OBJECT)
if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME)
String text = parser.getText();
if (parser.nextToken() != System.JSONToken.VALUE_NULL)
if (text == 'name')
name = parser.getText();
else if (text == 'twitter')
twitter = parser.getText();
else
System.debug(LoggingLevel.WARN, 'User consuming unrecognized property: '+text);
consumeObject(parser);
public User user get;set;
public JSON2Apex(JSONParser parser)
while (parser.nextToken() != System.JSONToken.END_OBJECT)
if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME)
String text = parser.getText();
if (parser.nextToken() != System.JSONToken.VALUE_NULL)
if (text == 'user')
user = new User(parser);
else
System.debug(LoggingLevel.WARN, 'JSON2Apex consuming unrecognized property: '+text);
consumeObject(parser);
public static JSON2Apex parse(String json)
System.JSONParser parser = System.JSON.createParser(json);
return new JSON2Apex(parser);
public static void consumeObject(System.JSONParser parser)
Integer depth = 0;
do while (depth > 0 && parser.nextToken() != null);
JSON.deserialize
public class JSON2Apex
public class User
public String name;
public String twitter;
public User user;
public static JSON2Apex parse(String json)
return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);
json parsing
json parsing
asked 21 hours ago
EduardEduard
1,8872723
1,8872723
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Apex code has reserved names (keywords) and special variable rules (e.g. cannot start with a number, can't have __
, etc) that you can't use in JSON objects. You don't want to use explicit mode if you can help it, because it has worse performance compared to JSON.deserialize, but it gets around compilation errors if you have a JSON string like:
"title": "Writing JSON", "abstract": "A short document about how to use JSON."
This would compile to:
public class JSON2Apex
public String title;
public String abstract;
But abstract is a reserved keyword. You can't deploy this code to Salesforce. By changing the code:
public class JSON2Apex
public String title;
public String abstract_x;
The code can then compile, but you need explicit parsing in order to translate abstract
in the JSON string to abstract_x
in Apex.
Thanks for your prompt answer! If this is the only reason why a developer would use explicit parsing, then I would definitely go with deserialize all the time. It's possible to keep a Map of reserved words and their substitutes, and perform replace in the input json string before parsing. This is exaclty how the ffhttp_JsonDeserializer.cls class works.
– Eduard
20 hours ago
1
@Eduard Yes, there are better ways. JSON2Apex is a rather old utility, useful in most cases, but explicit mode wasn't the best idea. There's definitely better ways to do it.
– sfdcfox
20 hours ago
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "459"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f257118%2fexplicitly-parse-json-string-vs-json-deserialize%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Apex code has reserved names (keywords) and special variable rules (e.g. cannot start with a number, can't have __
, etc) that you can't use in JSON objects. You don't want to use explicit mode if you can help it, because it has worse performance compared to JSON.deserialize, but it gets around compilation errors if you have a JSON string like:
"title": "Writing JSON", "abstract": "A short document about how to use JSON."
This would compile to:
public class JSON2Apex
public String title;
public String abstract;
But abstract is a reserved keyword. You can't deploy this code to Salesforce. By changing the code:
public class JSON2Apex
public String title;
public String abstract_x;
The code can then compile, but you need explicit parsing in order to translate abstract
in the JSON string to abstract_x
in Apex.
Thanks for your prompt answer! If this is the only reason why a developer would use explicit parsing, then I would definitely go with deserialize all the time. It's possible to keep a Map of reserved words and their substitutes, and perform replace in the input json string before parsing. This is exaclty how the ffhttp_JsonDeserializer.cls class works.
– Eduard
20 hours ago
1
@Eduard Yes, there are better ways. JSON2Apex is a rather old utility, useful in most cases, but explicit mode wasn't the best idea. There's definitely better ways to do it.
– sfdcfox
20 hours ago
add a comment |
Apex code has reserved names (keywords) and special variable rules (e.g. cannot start with a number, can't have __
, etc) that you can't use in JSON objects. You don't want to use explicit mode if you can help it, because it has worse performance compared to JSON.deserialize, but it gets around compilation errors if you have a JSON string like:
"title": "Writing JSON", "abstract": "A short document about how to use JSON."
This would compile to:
public class JSON2Apex
public String title;
public String abstract;
But abstract is a reserved keyword. You can't deploy this code to Salesforce. By changing the code:
public class JSON2Apex
public String title;
public String abstract_x;
The code can then compile, but you need explicit parsing in order to translate abstract
in the JSON string to abstract_x
in Apex.
Thanks for your prompt answer! If this is the only reason why a developer would use explicit parsing, then I would definitely go with deserialize all the time. It's possible to keep a Map of reserved words and their substitutes, and perform replace in the input json string before parsing. This is exaclty how the ffhttp_JsonDeserializer.cls class works.
– Eduard
20 hours ago
1
@Eduard Yes, there are better ways. JSON2Apex is a rather old utility, useful in most cases, but explicit mode wasn't the best idea. There's definitely better ways to do it.
– sfdcfox
20 hours ago
add a comment |
Apex code has reserved names (keywords) and special variable rules (e.g. cannot start with a number, can't have __
, etc) that you can't use in JSON objects. You don't want to use explicit mode if you can help it, because it has worse performance compared to JSON.deserialize, but it gets around compilation errors if you have a JSON string like:
"title": "Writing JSON", "abstract": "A short document about how to use JSON."
This would compile to:
public class JSON2Apex
public String title;
public String abstract;
But abstract is a reserved keyword. You can't deploy this code to Salesforce. By changing the code:
public class JSON2Apex
public String title;
public String abstract_x;
The code can then compile, but you need explicit parsing in order to translate abstract
in the JSON string to abstract_x
in Apex.
Apex code has reserved names (keywords) and special variable rules (e.g. cannot start with a number, can't have __
, etc) that you can't use in JSON objects. You don't want to use explicit mode if you can help it, because it has worse performance compared to JSON.deserialize, but it gets around compilation errors if you have a JSON string like:
"title": "Writing JSON", "abstract": "A short document about how to use JSON."
This would compile to:
public class JSON2Apex
public String title;
public String abstract;
But abstract is a reserved keyword. You can't deploy this code to Salesforce. By changing the code:
public class JSON2Apex
public String title;
public String abstract_x;
The code can then compile, but you need explicit parsing in order to translate abstract
in the JSON string to abstract_x
in Apex.
answered 20 hours ago
sfdcfoxsfdcfox
264k13210457
264k13210457
Thanks for your prompt answer! If this is the only reason why a developer would use explicit parsing, then I would definitely go with deserialize all the time. It's possible to keep a Map of reserved words and their substitutes, and perform replace in the input json string before parsing. This is exaclty how the ffhttp_JsonDeserializer.cls class works.
– Eduard
20 hours ago
1
@Eduard Yes, there are better ways. JSON2Apex is a rather old utility, useful in most cases, but explicit mode wasn't the best idea. There's definitely better ways to do it.
– sfdcfox
20 hours ago
add a comment |
Thanks for your prompt answer! If this is the only reason why a developer would use explicit parsing, then I would definitely go with deserialize all the time. It's possible to keep a Map of reserved words and their substitutes, and perform replace in the input json string before parsing. This is exaclty how the ffhttp_JsonDeserializer.cls class works.
– Eduard
20 hours ago
1
@Eduard Yes, there are better ways. JSON2Apex is a rather old utility, useful in most cases, but explicit mode wasn't the best idea. There's definitely better ways to do it.
– sfdcfox
20 hours ago
Thanks for your prompt answer! If this is the only reason why a developer would use explicit parsing, then I would definitely go with deserialize all the time. It's possible to keep a Map of reserved words and their substitutes, and perform replace in the input json string before parsing. This is exaclty how the ffhttp_JsonDeserializer.cls class works.
– Eduard
20 hours ago
Thanks for your prompt answer! If this is the only reason why a developer would use explicit parsing, then I would definitely go with deserialize all the time. It's possible to keep a Map of reserved words and their substitutes, and perform replace in the input json string before parsing. This is exaclty how the ffhttp_JsonDeserializer.cls class works.
– Eduard
20 hours ago
1
1
@Eduard Yes, there are better ways. JSON2Apex is a rather old utility, useful in most cases, but explicit mode wasn't the best idea. There's definitely better ways to do it.
– sfdcfox
20 hours ago
@Eduard Yes, there are better ways. JSON2Apex is a rather old utility, useful in most cases, but explicit mode wasn't the best idea. There's definitely better ways to do it.
– sfdcfox
20 hours ago
add a comment |
Thanks for contributing an answer to Salesforce Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f257118%2fexplicitly-parse-json-string-vs-json-deserialize%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown