paulbartrum / jurassic

A .NET library to parse and execute JavaScript code.
MIT License
873 stars 122 forks source link

Currently JSONObject.Stringify returns object instead of string after upgrading from 3.1.0 to 3.2.6 #216

Closed portaljacker closed 2 years ago

portaljacker commented 2 years ago

What's the reasoning for a stringify function not returning a string? That's a pretty big breaking change and I can't find anything mentioning it in this repository...

https://github.com/paulbartrum/jurassic/blob/4ecc4685f26128fbc38441ed6e7da72dd96de050/Jurassic/Library/JSON/JSONObject.cs#L58

paulbartrum commented 2 years ago

This method implements JSON.stringify(), which can return undefined in rare circumstances (e.g. JSON.stringify(undefined)). Undefined is not a string, so to match the spec the return value had to change. Generally from a compatibility perspective, I've prioritized matching the JS spec over maintaining backwards compatibility in the C# API.

portaljacker commented 2 years ago

Makes sense. So I just wrapped the block where I was using my result with this if statement: if (result is string s) { doStuff(s); } and everything worked fine.

paulbartrum commented 2 years ago

Or you can just do: result = (string)JSONObject.Stringify(...). The method will only return Undefined.Value if you pass in something that can't be converted to JSON, like a function. If you know that'll never happen then it's safe just to immediately cast the result :-)