JSON and Undefined Object Properties

Ran into an interesting case where the JavaScript object coming out of JSON wasn’t the same as the JavaScript object encoded going in.

When are JSON.parse() and JSON.stringify() not inverses of each another?

Here’s when.

In JavaScript it’s possible to define an object that has an undefined property:

var o = {
    a: "Hello",
    b: undefined
};

It’s then possible to iterate over all the objects properties and see the values as well.

for (var p in o) {
    alert( p + ": " + o[p] );
}

This will show “a: Hello” and “b: undefined”.

Now, to JSON-ify the object and reconstitute it back.
o = JSON.parse( JSON.stringify(o) );

Doing the for-loop again will reveal the object now only has the “a” property (the value is still “Hello”).

Try it.

While I get it, I’m not sure I like it. Ideally, I’d like anything coming out of JSON to be the same as anything going into it.

Assume this object:

var me = {
    mySiblingsGender: undefined
};

This is one of those cases where several subtleties rear their head on edge cases.

First, a known value for mySiblingsGender might be male or female. A null would indicate there is no gender, perhaps by a horrible vegetable peeler accident. (Oh my!) But an undefined value indicates there is one, we just don’t know what it is.

Removing the property completely instead conveys that there is no sibling (absence) — something totally different than null (not having any) or undefined (unknown).

While it’s splitting hairs, if not kindling for religious coding wars, it’s useful to know that JSON-out may not always be the same as JSON-in.

Updates, please don’t do that…

Using a MySQL DOT-Net Conncetor and getting these error messages? Shared Memory Provider: No Process is on the other end of the pipe. Error 0. Unable to find the request .NET Framework Data Provider. It may not be installed. Or your login not working, but your password is right? Try this solution.

Using Visual Studio 2010, it’s possible to use MySQL databases. The trick is to use the MySQL DotNET Connector.

Everything was going swimmingly until for what appeared to be no reason, until one day I went to work on my project and I started getting a strange error message for what appeared to be no reason.

Shared Memory Provider: No Process is on the other end of the pipe.
Error 0.
Unable to find the request .NET Framework Data Provider. It may not be installed.

Exploring the exception shows that a connection to the database is made, but the login has failed for the username. However, the username and password work when logging in using the MySQL client. No drivers have changed, and MySQL works with all other applications just fine, just not the Microsoft development IDE. The connection string validated in a test environment, as did a raw connection with the MySqlConnection. EntityFrameworks, not so much.

Attempting to use EntityFrameworks from VS2010 results in an error dialog box that when dismissed goes back to the database wizard that brings up the dialog box again; the only way out is using Task Manager and violently killing the IDE’s process.

Attempting to use the .MySQL .NET/Connector installer to repair the installation doesn’t help, either. Yes, it updates the .dll files, yes it tweaks the registry, but no, it doesn’t help.

Making a new project and creating a new ADO.NET Data Model reveals something interesting: the MySQL providers are not listed anymore in VS2010.

The solution is to UNINSTALL the DotConnector and then re-INSTALL the same MySQL .NET/Connector.

The full installer indicates that it updates the VS2008 and VS2010 development environments. Trying the IDE again works perfectly. The ADO.NET Data Model shows the MySQL Data Provider, the Entity Frameworks doesn’t get stuck in an endless loop, and my code worked again without change.

What happened to cause all this? Turns out I had installed a minor VS2010 hotfix service pack at the end of the weekend. The result? The update silently uninstalled all non-Microsoft data providers.

Updates, please don’t do that…

Clearly, I’m not happy about that. Especially since it did it silently. If it told me that it was going to, or if it asked me to reinstall after the fact, I could live with it.