Bug 675797 - Warn in calls to nsIJSON.encode and nsIJSON.decode that the two methods are deprecated. r=jst

--HG--
extra : rebase_source : ec4bda0ae1c71955bd42e45f51b996141cf4372c
This commit is contained in:
Jeff Walden 2011-08-01 15:14:31 -07:00
Родитель 1e07dbc39c
Коммит 8f1222d785
2 изменённых файлов: 31 добавлений и 5 удалений

Просмотреть файл

@ -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.

Просмотреть файл

@ -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,11 +443,15 @@ 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<nsIInputStream> stream;
nsresult rv = NS_NewByteInputStream(getter_AddRefs(stream),
(const char*) data,
rv = NS_NewByteInputStream(getter_AddRefs(stream),
reinterpret_cast<const char*>(data),
len * sizeof(PRUnichar),
NS_ASSIGNMENT_DEPEND);
NS_ENSURE_SUCCESS(rv, rv);