beware JSON.stringify04 Feb 2015
TL;DR: Don't trust JSON.stringify to serialize objects returned by navigator.getCurrentPosition, or any other complex object with prototypical inheritance. This will give you different results on different browsers. Use custom toJSON functions or use POJO/anonymous objects to serialize the values you need.
obj.property = JSON.parse(JSON.stringify(object));
In certain environments, you may need to use a custom method for creating json representations of your object because framework code adds 'temporary' properties to your object for it's own purposes that you probably don't want copied to your new object. Angular provides an angular.toJson() method for this purpose, which strips out properties with names starting with '$$'.
All of this is fine and relatively well-known. JSON.stringify is a reliable cross-browser tool that is used a lot - it 'just works'. Until it doesn't 🙂
The specific case I was dealing with was the location/position object provided by the
In a mobile device environment, it's tricky to debug your code remotely. We chose to provide a very simple view of the console.log calls on one of the screens in our app as a way for the user/developer to quickly check for interesting log messages while debugging unexpected behaviour. Yes I'm aware of other remote debugging and error reporting solutions (we've already implemented jslog.me). But this option took 3 minutes to spin up and served it's purpose at the time.
However the simple code we were using to display the results was running objects through the JSON.stringify process in order to have a nice human readable view of the object you might be debugging. In this case you'd see log statements showing you an empty object on one line, then the next line successfully printing out a property of that object. Very confusing 🙂
This is the stackoverflow post that led me in the right direction:
a related post: