michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef nsJSON_h__ michael@0: #define nsJSON_h__ michael@0: michael@0: #include "nsIJSON.h" michael@0: #include "nsString.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nsIOutputStream.h" michael@0: #include "nsIUnicodeEncoder.h" michael@0: #include "nsIUnicodeDecoder.h" michael@0: #include "nsIRequestObserver.h" michael@0: #include "nsIStreamListener.h" michael@0: #include "nsTArray.h" michael@0: michael@0: class nsIURI; michael@0: michael@0: class MOZ_STACK_CLASS nsJSONWriter michael@0: { michael@0: public: michael@0: nsJSONWriter(); michael@0: nsJSONWriter(nsIOutputStream *aStream); michael@0: virtual ~nsJSONWriter(); michael@0: nsresult SetCharset(const char *aCharset); michael@0: nsCOMPtr mStream; michael@0: nsresult Write(const char16_t *aBuffer, uint32_t aLength); michael@0: nsString mOutputString; michael@0: bool DidWrite(); michael@0: void FlushBuffer(); michael@0: michael@0: protected: michael@0: char16_t *mBuffer; michael@0: uint32_t mBufferCount; michael@0: bool mDidWrite; michael@0: nsresult WriteToStream(nsIOutputStream *aStream, nsIUnicodeEncoder *encoder, michael@0: const char16_t *aBuffer, uint32_t aLength); michael@0: michael@0: nsCOMPtr mEncoder; michael@0: }; michael@0: michael@0: class nsJSON : public nsIJSON michael@0: { michael@0: public: michael@0: nsJSON(); michael@0: virtual ~nsJSON(); michael@0: michael@0: NS_DECL_ISUPPORTS michael@0: NS_DECL_NSIJSON michael@0: michael@0: protected: michael@0: nsresult EncodeInternal(JSContext* cx, michael@0: const JS::Value& val, michael@0: nsJSONWriter* writer); michael@0: michael@0: nsresult DecodeInternal(JSContext* cx, michael@0: nsIInputStream* aStream, michael@0: int32_t aContentLength, michael@0: bool aNeedsConverter, michael@0: JS::MutableHandle aRetVal); michael@0: nsCOMPtr mURI; michael@0: }; michael@0: michael@0: nsresult michael@0: NS_NewJSON(nsISupports* aOuter, REFNSIID aIID, void** aResult); michael@0: michael@0: class nsJSONListener : public nsIStreamListener michael@0: { michael@0: public: michael@0: nsJSONListener(JSContext *cx, JS::Value *rootVal, bool needsConverter); michael@0: virtual ~nsJSONListener(); michael@0: michael@0: NS_DECL_ISUPPORTS michael@0: NS_DECL_NSIREQUESTOBSERVER michael@0: NS_DECL_NSISTREAMLISTENER michael@0: michael@0: protected: michael@0: bool mNeedsConverter; michael@0: JSContext *mCx; michael@0: JS::Value *mRootVal; michael@0: nsCOMPtr mDecoder; michael@0: nsCString mSniffBuffer; michael@0: nsTArray mBufferedChars; michael@0: nsresult ProcessBytes(const char* aBuffer, uint32_t aByteLength); michael@0: nsresult ConsumeConverted(const char* aBuffer, uint32_t aByteLength); michael@0: nsresult Consume(const char16_t *data, uint32_t len); michael@0: }; michael@0: michael@0: #endif