Fetch: Support object literal serialization (as JSON)
See original GitHub issueSince #128 was fixed, the built-in object serialization was removed in favor of either diy-serialization or hooks.
I’m proposing to reintroduce object serialization, but as JSON, not query strings, because of the many issues and implementation details to settle with query strings.
Problems with query strings
- Converting nested data to to query strings is somewhat complex, likely adding at least ~20-30 lines of code.
- Some form of recursion is needed to optimally “walk” over the object. Tail call optimisation is currently only implemented in Chrome (I think?) so this could cause stack overflows on huge nested objects.
- The only supported data type is string, so it’s not possible on the receiving end to distinguish between
"true"
vstrue
or5
vs"5"
or["a"]
vs{0:'a'}
. - It’s not a standardized format. From the jQuery docs: “Because there is no universally agreed-upon specification for param strings, it is not possible to encode complex data structures using this method in a manner that works ideally across all languages supporting such input. Use JSON format as an alternative for encoding complex data instead.”
- Because it’s not a universally agreed-upon spec, many serializers have optional arguments for small details such as how to encode arrays, or encoding spaces as
+
or%20
. Besides making the serializer more complex and harder to both maintain and use, this also meansfetch
would have to support these arguments as well, or only support the defaults. I think this partly defeats the purpose of supporting object serialization infetch
.
JSON (application/json
) doesn’t have these problems
- No custom method needed.
JSON.stringify()
is supported since IE8. - No stack overflow risk
- No ambiguity or string coercion.
- No difference between server implementations or extra arguments needed.
- Very few implementation details to settle, making it very easy to implement.
On the negative side, it could be confusing if strings are treated as query strings (current behavior) but object literals are serialized as application/json (this proposal). Depending on your server environment it could also take some extra effort to support.
Issue Analytics
- State:
- Created 6 years ago
- Comments:9 (8 by maintainers)
Top Results From Across the Web
Fetch data dynamically - Dart programming language
Use the json.encode() function to serialize an object that supports JSON. The _showJson() function, from the example, converts all of the data to...
Read more >JSON.stringify() - JavaScript - MDN Web Docs
The JSON.stringify() method converts a JavaScript value to a JSON string ... original (assuming the object is completely JSON-serializable).
Read more >JSON methods, toJSON - The Modern JavaScript Tutorial
The resulting json string is called a JSON-encoded or serialized or stringified or marshalled object. We are ready to send it over the...
Read more >What is the difference between JSON and Object Literal ...
10 Answers 10 · JavaScript Object Notation (JSON) is a text format for the serialization of structured data. · JSON can represent four...
Read more >Working With JSON Data in Python
A Little Vocabulary; Serializing JSON; A Simple Serialization Example ... subset of the JavaScript programming language dealing with object literal syntax.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Is the
fetch
function still needed? Is it stated somewhere what range of browser versions this is now intended for?Use the Bliss
$.type()
function. Your check would fail if the object comes from another window/frame.