2015-05-03 19:32:37 +00: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 11:12:37 +00: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 21:34:03 +00: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-07 07:19:26 +00:00
|
|
|
class nsIURI;
|
|
|
|
|
2013-04-12 03:20:18 +00:00
|
|
|
class MOZ_STACK_CLASS nsJSONWriter
|
2007-12-27 21:34:03 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsJSONWriter();
|
2014-09-02 00:49:25 +00:00
|
|
|
explicit nsJSONWriter(nsIOutputStream* aStream);
|
2007-12-27 21:34:03 +00:00
|
|
|
virtual ~nsJSONWriter();
|
|
|
|
nsresult SetCharset(const char *aCharset);
|
|
|
|
nsCOMPtr<nsIOutputStream> mStream;
|
2014-01-04 15:02:17 +00:00
|
|
|
nsresult Write(const char16_t *aBuffer, uint32_t aLength);
|
2008-02-07 07:19:26 +00:00
|
|
|
nsString mOutputString;
|
2011-09-29 06:19:26 +00:00
|
|
|
bool DidWrite();
|
2008-02-07 07:19:26 +00:00
|
|
|
void FlushBuffer();
|
2007-12-27 21:34:03 +00:00
|
|
|
|
|
|
|
protected:
|
2014-01-04 15:02:17 +00:00
|
|
|
char16_t *mBuffer;
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t mBufferCount;
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mDidWrite;
|
2007-12-27 21:34:03 +00:00
|
|
|
nsresult WriteToStream(nsIOutputStream *aStream, nsIUnicodeEncoder *encoder,
|
2014-01-04 15:02:17 +00:00
|
|
|
const char16_t *aBuffer, uint32_t aLength);
|
2007-12-27 21:34:03 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIUnicodeEncoder> mEncoder;
|
|
|
|
};
|
|
|
|
|
|
|
|
class nsJSON : public nsIJSON
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsJSON();
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIJSON
|
|
|
|
|
|
|
|
protected:
|
2014-06-23 19:56:07 +00:00
|
|
|
virtual ~nsJSON();
|
|
|
|
|
2011-11-26 10:21:47 +00:00
|
|
|
nsresult EncodeInternal(JSContext* cx,
|
|
|
|
const JS::Value& val,
|
|
|
|
nsJSONWriter* writer);
|
2010-07-14 18:48:36 +00:00
|
|
|
|
2011-11-26 10:21:29 +00:00
|
|
|
nsresult DecodeInternal(JSContext* cx,
|
|
|
|
nsIInputStream* aStream,
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t aContentLength,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool aNeedsConverter,
|
2014-01-09 17:39:36 +00:00
|
|
|
JS::MutableHandle<JS::Value> aRetVal);
|
2008-02-07 07:19:26 +00:00
|
|
|
nsCOMPtr<nsIURI> mURI;
|
2007-12-27 21:34:03 +00:00
|
|
|
};
|
|
|
|
|
2010-06-10 18:11:11 +00:00
|
|
|
nsresult
|
2007-12-27 21:34:03 +00:00
|
|
|
NS_NewJSON(nsISupports* aOuter, REFNSIID aIID, void** aResult);
|
|
|
|
|
|
|
|
class nsJSONListener : public nsIStreamListener
|
|
|
|
{
|
|
|
|
public:
|
2013-05-23 22:28:31 +00:00
|
|
|
nsJSONListener(JSContext *cx, JS::Value *rootVal, bool needsConverter);
|
2007-12-27 21:34:03 +00:00
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIREQUESTOBSERVER
|
|
|
|
NS_DECL_NSISTREAMLISTENER
|
|
|
|
|
|
|
|
protected:
|
2014-06-23 19:56:07 +00:00
|
|
|
virtual ~nsJSONListener();
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mNeedsConverter;
|
2007-12-27 21:34:03 +00:00
|
|
|
JSContext *mCx;
|
2013-04-02 23:05:37 +00:00
|
|
|
JS::Value *mRootVal;
|
2007-12-27 21:34:03 +00:00
|
|
|
nsCOMPtr<nsIUnicodeDecoder> mDecoder;
|
|
|
|
nsCString mSniffBuffer;
|
2014-01-04 15:02:17 +00:00
|
|
|
nsTArray<char16_t> mBufferedChars;
|
2012-08-22 15:56:38 +00:00
|
|
|
nsresult ProcessBytes(const char* aBuffer, uint32_t aByteLength);
|
|
|
|
nsresult ConsumeConverted(const char* aBuffer, uint32_t aByteLength);
|
2014-01-04 15:02:17 +00:00
|
|
|
nsresult Consume(const char16_t *data, uint32_t len);
|
2007-12-27 21:34:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|