The function angular.toJson
will take an object and serialize it into a JSON formatted string.
Unlike the native function JSON.stringify
, This function will remove all properties beginning with $$
(as angular usually prefixes internal properties with $$
)
angular.toJson(object)
As data needs to be serialized before passing through a network, this function is useful to turn any data you wish to transmit into JSON.
This function is also useful for debugging as it works similarly to a .toString
method would act.
Examples:
angular.toJson({name: "barf", occupation: "mog", $$somebizzareproperty: 42})
// "{"name":"barf","occupation":"mog"}"
angular.toJson(42)
// "42"
angular.toJson([1, "2", 3, "4"])
// "[1,"2",3,"4"]"
var fn = function(value) {return value}
angular.toJson(fn)
// undefined, functions have no representation in JSON