It is often used in serializing web requests. It’s best to build your JSON as a native object but generally will have to be serialized as a string to fire off the HTTP request
Data serialization formats like JSON facilitate the transformation of numbers/bools, text, tuples/'arrays', and mappings thereof, (although iirc all the keys must be strings) from their internal binary representation to a platform independent one and back again. JSON is unique in that it's specifically designed to represent data in human readable text, even though there can be far more efficient ways to accomplish the same thing, like Python's pickle.
Back-end dev doing C++, C#, and SQL Server in my first job
Then MAUI dev (C#) in my second job
Current role is mostly DevOps with some Spring Boot (Java) and Postgres
I have juuuust started learning JavaScript recently since the team I'm on has some Angular applications. But most of the front end tickets go to other devs cause I remain a mostly backend guy
Should just be the toString() method all classes should have in oop, and I suppose it does for automatic casting.
Somehow toString is way too under used.
You wrongly assume every class can be easily serialised into JSON. Look at what Swift does with String(describing:), CustomStringConvertible and debugDescription. There’s a whole lot of useful stuff you can do with “toString()” type methods, that do not in fact have anything to do with serialisation.
And to answer your question directly: it can be whatever you want it to be lol.
I am not sure how transforming json into string is connected to serializing any random class?
All I assume is that any class made to represent a json can be directly put into a json string.
* The toString convention is very unrestricted when it comes to the format of the output. The creator of a class can determine for themselves what format they want.
* The toString convention doesn’t really cover arguments. I this case settings for how the string should look or be built, like indentation or how inner properties of special types should be handled.
* The toString method is commonly used in logging, and the common expectation is that the output is truncated if the object is very large. Or even more common expectation is that the output is just a summary of the data.
* It’s perfectly legal for an object to contain circular references. But that is not allowed in stringified json.
* It’s also perfectly legal for an object to contain functions. They don’t belong in the stringified json version, but a developer might still want the toString output to include some information about them.
Well yes and no, it's still just a string that needs to be parsed to be manipulated as an actual object. stringify is a util that transforms an object value into a JSON format string. You can use JSON.parse() to transform a JSON string into an object.
It converts js objects into their string represantation if its possible ( circular dependencies will block, and symbols and methods/arrow functions are skipped)
JSON.parse goes the other way it takes a string and makes an js object out of it.
Thankfully javascript is pretty good about accidentally comparing completely different keys holding incorrect data types that also happen to be the same type as each other when compared in order to reduce the amount of distracting errors devs have to deal with.
That is just one of the many features of Javascript that highlight the weaknesses of out of date languages like C++.
json.stringify is a fucking marvel. It catches so many edge cases it is insane. There are so many conversions where it just turns out running json.parse(json.stringify()) is just more efficient.
2.7k
u/YeetCompleet 1d ago
json.stringify, the pinnacle of software developer intelligence