Created
March 4, 2013 21:05
-
-
Save markcoleman/5085650 to your computer and use it in GitHub Desktop.
Modified toJson method for AngularJs so Json.Net remains happy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$httpProvider.defaults.transformRequest = function (data) { | |
function isWindow(obj) { | |
return obj && obj.document && obj.location && obj.alert && obj.setInterval; | |
} | |
function isScope(obj) { | |
return obj && obj.$evalAsync && obj.$watch; | |
} | |
function isFile(obj) { | |
return toString.apply(obj) === '[object File]'; | |
} | |
function toJsonWithJsonDotNetSupport(obj, pretty) { | |
return JSON.stringify(obj, function (key, value) { | |
var val = value; | |
if(/^\$+/.test(key) && key !== "$type") { | |
val = undefined; | |
} else { | |
if(isWindow(value)) { | |
val = '$WINDOW'; | |
} else { | |
if(value && document === value) { | |
val = '$DOCUMENT'; | |
} else { | |
if(isScope(value)) { | |
val = '$SCOPE'; | |
} | |
} | |
} | |
} | |
return val; | |
}, pretty ? ' ' : null); | |
} | |
return angular.isObject(data) && !isFile(data) ? toJsonWithJsonDotNetSupport(data, false) : data; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment