1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/src/json/nsJSON.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,91 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef nsJSON_h__ 1.10 +#define nsJSON_h__ 1.11 + 1.12 +#include "nsIJSON.h" 1.13 +#include "nsString.h" 1.14 +#include "nsCOMPtr.h" 1.15 +#include "nsIOutputStream.h" 1.16 +#include "nsIUnicodeEncoder.h" 1.17 +#include "nsIUnicodeDecoder.h" 1.18 +#include "nsIRequestObserver.h" 1.19 +#include "nsIStreamListener.h" 1.20 +#include "nsTArray.h" 1.21 + 1.22 +class nsIURI; 1.23 + 1.24 +class MOZ_STACK_CLASS nsJSONWriter 1.25 +{ 1.26 +public: 1.27 + nsJSONWriter(); 1.28 + nsJSONWriter(nsIOutputStream *aStream); 1.29 + virtual ~nsJSONWriter(); 1.30 + nsresult SetCharset(const char *aCharset); 1.31 + nsCOMPtr<nsIOutputStream> mStream; 1.32 + nsresult Write(const char16_t *aBuffer, uint32_t aLength); 1.33 + nsString mOutputString; 1.34 + bool DidWrite(); 1.35 + void FlushBuffer(); 1.36 + 1.37 +protected: 1.38 + char16_t *mBuffer; 1.39 + uint32_t mBufferCount; 1.40 + bool mDidWrite; 1.41 + nsresult WriteToStream(nsIOutputStream *aStream, nsIUnicodeEncoder *encoder, 1.42 + const char16_t *aBuffer, uint32_t aLength); 1.43 + 1.44 + nsCOMPtr<nsIUnicodeEncoder> mEncoder; 1.45 +}; 1.46 + 1.47 +class nsJSON : public nsIJSON 1.48 +{ 1.49 +public: 1.50 + nsJSON(); 1.51 + virtual ~nsJSON(); 1.52 + 1.53 + NS_DECL_ISUPPORTS 1.54 + NS_DECL_NSIJSON 1.55 + 1.56 +protected: 1.57 + nsresult EncodeInternal(JSContext* cx, 1.58 + const JS::Value& val, 1.59 + nsJSONWriter* writer); 1.60 + 1.61 + nsresult DecodeInternal(JSContext* cx, 1.62 + nsIInputStream* aStream, 1.63 + int32_t aContentLength, 1.64 + bool aNeedsConverter, 1.65 + JS::MutableHandle<JS::Value> aRetVal); 1.66 + nsCOMPtr<nsIURI> mURI; 1.67 +}; 1.68 + 1.69 +nsresult 1.70 +NS_NewJSON(nsISupports* aOuter, REFNSIID aIID, void** aResult); 1.71 + 1.72 +class nsJSONListener : public nsIStreamListener 1.73 +{ 1.74 +public: 1.75 + nsJSONListener(JSContext *cx, JS::Value *rootVal, bool needsConverter); 1.76 + virtual ~nsJSONListener(); 1.77 + 1.78 + NS_DECL_ISUPPORTS 1.79 + NS_DECL_NSIREQUESTOBSERVER 1.80 + NS_DECL_NSISTREAMLISTENER 1.81 + 1.82 +protected: 1.83 + bool mNeedsConverter; 1.84 + JSContext *mCx; 1.85 + JS::Value *mRootVal; 1.86 + nsCOMPtr<nsIUnicodeDecoder> mDecoder; 1.87 + nsCString mSniffBuffer; 1.88 + nsTArray<char16_t> mBufferedChars; 1.89 + nsresult ProcessBytes(const char* aBuffer, uint32_t aByteLength); 1.90 + nsresult ConsumeConverted(const char* aBuffer, uint32_t aByteLength); 1.91 + nsresult Consume(const char16_t *data, uint32_t len); 1.92 +}; 1.93 + 1.94 +#endif