question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Fetch: Support object literal serialization (as JSON)

See original GitHub issue

Since #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" vs true or 5 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 means fetch would have to support these arguments as well, or only support the defaults. I think this partly defeats the purpose of supporting object serialization in fetch.

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:open
  • Created 6 years ago
  • Comments:9 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
joyouslycommented, Nov 7, 2020

Is the fetch function still needed? Is it stated somewhere what range of browser versions this is now intended for?

1reaction
LeaVeroucommented, Nov 20, 2017

Use the Bliss $.type() function. Your check would fail if the object comes from another window/frame.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found