Java Reference
In-Depth Information
Tim Says. . .
Why Use JavaScript Instead of JSON or XML?
JavaScriptResolution generates and returns JavaScript code—
not JSON and definitely not XML. Often this leads to the ques-
tion, why not just use JSON? Or even better: JSON is a standard,
so why don't you use it? Certainly there are advantages to
using JSON—it's a nice compact format, and it's pretty human-
readable. For cases where you want to send strictly hierarchical
data to the browser, JSON is great.
The main reason to use JavaScript instead is that it can han-
dle circular references properly. No declarative format can do
this, and that includes both JSON and XML. For example, if you
have a FamilyMember object that has properties that refer to its
parents and its children, you quickly end up with circular refer-
ences (for example, Freddy's child is Lily, who's father is Freddy).
Modern persistence technologies like JPA and Hibernate tend
to encourage these well-connected models. Using JavaScript
allows us to do the following sequentially:
1. Convert “Freddy” to a JavaScript object.
2. Convert “Lily” to a JavaScript object.
3. Link Lily in as Freddy's child.
4. Link Freddy in as Lily's parent.
The result is that you get a JavaScript object graph that is linked
up exactly like the object graph was on the Java side.
If you're still worried that JSON is a standard and JavaScript isn't,
I'll gently remind you that JavaScript is actually specified by a
standard called ECMAScript!
 
 
Search WWH ::




Custom Search