2015-05-03 12:32:37 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-12-27 13:34:03 -08:00
|
|
|
|
|
|
|
#ifndef nsJSON_h__
|
|
|
|
#define nsJSON_h__
|
|
|
|
|
|
|
|
#include "nsIJSON.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIOutputStream.h"
|
|
|
|
#include "nsIUnicodeEncoder.h"
|
|
|
|
#include "nsIUnicodeDecoder.h"
|
|
|
|
#include "nsIRequestObserver.h"
|
|
|
|
#include "nsIStreamListener.h"
|
|
|
|
#include "nsTArray.h"
|
|
|
|
|
2008-02-06 23:19:26 -08:00
|
|
|
class nsIURI;
|
|
|
|
|
2013-04-11 20:20:18 -07:00
|
|
|
class MOZ_STACK_CLASS nsJSONWriter
|
2007-12-27 13:34:03 -08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsJSONWriter();
|
2014-09-01 17:49:25 -07:00
|
|
|
explicit nsJSONWriter(nsIOutputStream* aStream);
|
2007-12-27 13:34:03 -08:00
|
|
|
virtual ~nsJSONWriter();
|
|
|
|
nsresult SetCharset(const char *aCharset);
|
|
|
|
nsCOMPtr<nsIOutputStream> mStream;
|
2014-01-04 07:02:17 -08:00
|
|
|
nsresult Write(const char16_t *aBuffer, uint32_t aLength);
|
2008-02-06 23:19:26 -08:00
|
|
|
nsString mOutputString;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool DidWrite();
|
2008-02-06 23:19:26 -08:00
|
|
|
void FlushBuffer();
|
2007-12-27 13:34:03 -08:00
|
|
|
|
|
|
|
protected:
|
2014-01-04 07:02:17 -08:00
|
|
|
char16_t *mBuffer;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t mBufferCount;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mDidWrite;
|
2007-12-27 13:34:03 -08:00
|
|
|
nsresult WriteToStream(nsIOutputStream *aStream, nsIUnicodeEncoder *encoder,
|
2014-01-04 07:02:17 -08:00
|
|
|
const char16_t *aBuffer, uint32_t aLength);
|
2007-12-27 13:34:03 -08:00
|
|
|
|
|
|
|
nsCOMPtr<nsIUnicodeEncoder> mEncoder;
|
|
|
|
};
|
|
|
|
|
|
|
|
class nsJSON : public nsIJSON
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsJSON();
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIJSON
|
|
|
|
|
|
|
|
protected:
|
2014-06-23 12:56:07 -07:00
|
|
|
virtual ~nsJSON();
|
|
|
|
|
2011-11-26 02:21:47 -08:00
|
|
|
nsresult EncodeInternal(JSContext* cx,
|
|
|
|
const JS::Value& val,
|
|
|
|
nsJSONWriter* writer);
|
2010-07-14 11:48:36 -07:00
|
|
|
|
2011-11-26 02:21:29 -08:00
|
|
|
nsresult DecodeInternal(JSContext* cx,
|
|
|
|
nsIInputStream* aStream,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aContentLength,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aNeedsConverter,
|
2014-01-09 09:39:36 -08:00
|
|
|
JS::MutableHandle<JS::Value> aRetVal);
|
2008-02-06 23:19:26 -08:00
|
|
|
nsCOMPtr<nsIURI> mURI;
|
2007-12-27 13:34:03 -08:00
|
|
|
};
|
|
|
|
|
2010-06-10 11:11:11 -07:00
|
|
|
nsresult
|
2007-12-27 13:34:03 -08:00
|
|
|
NS_NewJSON(nsISupports* aOuter, REFNSIID aIID, void** aResult);
|
|
|
|
|
|
|
|
class nsJSONListener : public nsIStreamListener
|
|
|
|
{
|
|
|
|
public:
|
2013-05-23 15:28:31 -07:00
|
|
|
nsJSONListener(JSContext *cx, JS::Value *rootVal, bool needsConverter);
|
2007-12-27 13:34:03 -08:00
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIREQUESTOBSERVER
|
|
|
|
NS_DECL_NSISTREAMLISTENER
|
|
|
|
|
|
|
|
protected:
|
2014-06-23 12:56:07 -07:00
|
|
|
virtual ~nsJSONListener();
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mNeedsConverter;
|
2007-12-27 13:34:03 -08:00
|
|
|
JSContext *mCx;
|
2013-04-02 16:05:37 -07:00
|
|
|
JS::Value *mRootVal;
|
2007-12-27 13:34:03 -08:00
|
|
|
nsCOMPtr<nsIUnicodeDecoder> mDecoder;
|
|
|
|
nsCString mSniffBuffer;
|
2014-01-04 07:02:17 -08:00
|
|
|
nsTArray<char16_t> mBufferedChars;
|
2012-08-22 08:56:38 -07:00
|
|
|
nsresult ProcessBytes(const char* aBuffer, uint32_t aByteLength);
|
|
|
|
nsresult ConsumeConverted(const char* aBuffer, uint32_t aByteLength);
|
2014-01-04 07:02:17 -08:00
|
|
|
nsresult Consume(const char16_t *data, uint32_t len);
|
2007-12-27 13:34:03 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|