|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #ifndef nsJSON_h__ |
|
7 #define nsJSON_h__ |
|
8 |
|
9 #include "nsIJSON.h" |
|
10 #include "nsString.h" |
|
11 #include "nsCOMPtr.h" |
|
12 #include "nsIOutputStream.h" |
|
13 #include "nsIUnicodeEncoder.h" |
|
14 #include "nsIUnicodeDecoder.h" |
|
15 #include "nsIRequestObserver.h" |
|
16 #include "nsIStreamListener.h" |
|
17 #include "nsTArray.h" |
|
18 |
|
19 class nsIURI; |
|
20 |
|
21 class MOZ_STACK_CLASS nsJSONWriter |
|
22 { |
|
23 public: |
|
24 nsJSONWriter(); |
|
25 nsJSONWriter(nsIOutputStream *aStream); |
|
26 virtual ~nsJSONWriter(); |
|
27 nsresult SetCharset(const char *aCharset); |
|
28 nsCOMPtr<nsIOutputStream> mStream; |
|
29 nsresult Write(const char16_t *aBuffer, uint32_t aLength); |
|
30 nsString mOutputString; |
|
31 bool DidWrite(); |
|
32 void FlushBuffer(); |
|
33 |
|
34 protected: |
|
35 char16_t *mBuffer; |
|
36 uint32_t mBufferCount; |
|
37 bool mDidWrite; |
|
38 nsresult WriteToStream(nsIOutputStream *aStream, nsIUnicodeEncoder *encoder, |
|
39 const char16_t *aBuffer, uint32_t aLength); |
|
40 |
|
41 nsCOMPtr<nsIUnicodeEncoder> mEncoder; |
|
42 }; |
|
43 |
|
44 class nsJSON : public nsIJSON |
|
45 { |
|
46 public: |
|
47 nsJSON(); |
|
48 virtual ~nsJSON(); |
|
49 |
|
50 NS_DECL_ISUPPORTS |
|
51 NS_DECL_NSIJSON |
|
52 |
|
53 protected: |
|
54 nsresult EncodeInternal(JSContext* cx, |
|
55 const JS::Value& val, |
|
56 nsJSONWriter* writer); |
|
57 |
|
58 nsresult DecodeInternal(JSContext* cx, |
|
59 nsIInputStream* aStream, |
|
60 int32_t aContentLength, |
|
61 bool aNeedsConverter, |
|
62 JS::MutableHandle<JS::Value> aRetVal); |
|
63 nsCOMPtr<nsIURI> mURI; |
|
64 }; |
|
65 |
|
66 nsresult |
|
67 NS_NewJSON(nsISupports* aOuter, REFNSIID aIID, void** aResult); |
|
68 |
|
69 class nsJSONListener : public nsIStreamListener |
|
70 { |
|
71 public: |
|
72 nsJSONListener(JSContext *cx, JS::Value *rootVal, bool needsConverter); |
|
73 virtual ~nsJSONListener(); |
|
74 |
|
75 NS_DECL_ISUPPORTS |
|
76 NS_DECL_NSIREQUESTOBSERVER |
|
77 NS_DECL_NSISTREAMLISTENER |
|
78 |
|
79 protected: |
|
80 bool mNeedsConverter; |
|
81 JSContext *mCx; |
|
82 JS::Value *mRootVal; |
|
83 nsCOMPtr<nsIUnicodeDecoder> mDecoder; |
|
84 nsCString mSniffBuffer; |
|
85 nsTArray<char16_t> mBufferedChars; |
|
86 nsresult ProcessBytes(const char* aBuffer, uint32_t aByteLength); |
|
87 nsresult ConsumeConverted(const char* aBuffer, uint32_t aByteLength); |
|
88 nsresult Consume(const char16_t *data, uint32_t len); |
|
89 }; |
|
90 |
|
91 #endif |