diff --git a/dom/locales/en-US/chrome/dom/dom.properties b/dom/locales/en-US/chrome/dom/dom.properties index 19e7c4777ab..3a63cd3f8ad 100644 --- a/dom/locales/en-US/chrome/dom/dom.properties +++ b/dom/locales/en-US/chrome/dom/dom.properties @@ -108,3 +108,6 @@ IsSupportedWarning=Use of attributes' isSupported() is deprecated. IsEqualNodeWarning=Use of attributes' isEqualNode() is deprecated. TextContentWarning=Use of attributes' textContent attribute is deprecated. Use value instead. EnablePrivilegeWarning=Use of enablePrivilege is deprecated. Please use code that runs with the system principal (e.g. an extension) instead. + +nsIJSONDecodeDeprecatedWarning=nsIJSON.decode is deprecated. Please use JSON.parse instead. +nsIJSONEncodeDeprecatedWarning=nsIJSON.encode is deprecated. Please use JSON.stringify instead. diff --git a/dom/src/json/nsJSON.cpp b/dom/src/json/nsJSON.cpp index a8377266ba0..94b13f127c2 100644 --- a/dom/src/json/nsJSON.cpp +++ b/dom/src/json/nsJSON.cpp @@ -52,6 +52,7 @@ #include "nsXPCOMStrings.h" #include "nsNetUtil.h" #include "nsContentUtils.h" +#include "nsIScriptError.h" #include "nsCRTGlue.h" #include "nsAutoPtr.h" #include "nsIScriptSecurityManager.h" @@ -76,11 +77,29 @@ nsJSON::~nsJSON() { } +enum DeprecationWarning { EncodeWarning, DecodeWarning }; + +static nsresult +WarnDeprecatedMethod(DeprecationWarning warning) +{ + return nsContentUtils::ReportToConsole(nsContentUtils::eDOM_PROPERTIES, + warning == EncodeWarning + ? "nsIJSONEncodeDeprecatedWarning" + : "nsIJSONDecodeDeprecatedWarning", + nsnull, 0, + nsnull, + EmptyString(), 0, 0, + nsIScriptError::warningFlag, + "DOM Core"); +} + NS_IMETHODIMP nsJSON::Encode(nsAString &aJSON) { // This function should only be called from JS. - nsresult rv; + nsresult rv = WarnDeprecatedMethod(EncodeWarning); + if (NS_FAILED(rv)) + return rv; nsJSONWriter writer; rv = EncodeInternal(&writer); @@ -424,13 +443,17 @@ nsJSONWriter::WriteToStream(nsIOutputStream *aStream, NS_IMETHODIMP nsJSON::Decode(const nsAString& json) { + nsresult rv = WarnDeprecatedMethod(DecodeWarning); + if (NS_FAILED(rv)) + return rv; + const PRUnichar *data; PRUint32 len = NS_StringGetData(json, &data); nsCOMPtr stream; - nsresult rv = NS_NewByteInputStream(getter_AddRefs(stream), - (const char*) data, - len * sizeof(PRUnichar), - NS_ASSIGNMENT_DEPEND); + rv = NS_NewByteInputStream(getter_AddRefs(stream), + reinterpret_cast(data), + len * sizeof(PRUnichar), + NS_ASSIGNMENT_DEPEND); NS_ENSURE_SUCCESS(rv, rv); return DecodeInternal(stream, len, PR_FALSE); }