Skip to content

Instantly share code, notes, and snippets.

@markcoleman
Created March 4, 2013 21:05
Show Gist options
  • Save markcoleman/5085650 to your computer and use it in GitHub Desktop.
Save markcoleman/5085650 to your computer and use it in GitHub Desktop.
Modified toJson method for AngularJs so Json.Net remains happy
$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