3 ms·
Empty string isn't enough for this situation since it doesn't work for data types other than string, I actually prefer null vs undefined in javascript / json he
by SigmundA 7y ago
Empty string isn't enough for this situation since it doesn't work for data types other than string, I actually prefer null vs undefined in javascript / json here, xml also has the concept of null vs undefined.
Emtpy string vs null is not that useful if you have null and undefined.
- giornogiovanna 7y agoJust to be clear, undefined is not valid in JSON. The only types in JSON are nulls, strings, numbers, arrays, booleans and objects.
- cyphar 7y ago"undefined" in this context means that the JSON object has had the relevant key omitted rather than explicitly set to null. This is valid JSON (it just so happens that JavaScript handles this scenario by returning "undefined" as with all undefined variables).
- giornogiovanna 7y agoAh, right, my bad. In my defense, JavaScript treats a value of `undefined` differently from an unset field / variable (e.g. when testing `object.hasOwnProperty('field')`).
- tabtab 7y agoJavaScript has too many "non-visible" value types in my opinion. This makes for both messy code and messy debugging.
- SigmundA 7y agoYes omitted value vs provided with null which is easy to work with in JS because of null and undefined and them being falsey. Working with xml or json in more typed language like C# is less fun because there is not distinction in the language instead you end up using dom like checks to see if the key was provided or was set to null. .Net code generator use to add an extra property called [property name]Specified for xml serialization so you could do null values or not send the node. Definitely prefer more direct language support for no value provided vs a null value.
- vbezhenar 7y agoUndefined is very underappreciated invention. I wish I had undefined in Java, throwing exception on read. That would make a lot of software much more reliable. I'm going to write my own AutoValue library just to generate java beans which will allow uninitialized values. PS undefined in JavaScript is not that useful IMO. But it could be useful with more strict language.
- fnord123 7y agoOptional was added in java 8.