]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | /** |
2 | * Licensed to the Apache Software Foundation (ASF) under one | |
3 | * or more contributor license agreements. See the NOTICE file | |
4 | * distributed with this work for additional information | |
5 | * regarding copyright ownership. The ASF licenses this file | |
6 | * to you under the Apache License, Version 2.0 (the | |
7 | * "License"); you may not use this file except in compliance | |
8 | * with the License. You may obtain a copy of the License at | |
9 | * | |
10 | * http://www.apache.org/licenses/LICENSE-2.0 | |
11 | * | |
12 | * Unless required by applicable law or agreed to in writing, | |
13 | * software distributed under the License is distributed on an | |
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
15 | * KIND, either express or implied. See the License for the | |
16 | * specific language governing permissions and limitations | |
17 | * under the License. | |
18 | */ | |
19 | ||
20 | using System; | |
21 | using System.Collections.Generic; | |
22 | using System.Diagnostics; | |
23 | using System.IO; | |
24 | using System.Linq; | |
25 | using System.Text; | |
26 | using Thrift.Protocol; | |
27 | using Thrift.Transport; | |
28 | ||
29 | namespace JSONTest | |
30 | { | |
31 | class Program | |
32 | { | |
33 | static void Main(string[] args) | |
34 | { | |
35 | TestThrift2365(); // JSON binary decodes too much data | |
36 | TestThrift2336(); // hex encoding using \uXXXX where 0xXXXX > 0xFF | |
37 | TestThrift3403(); // JSON escaped unicode surrogate pair support. | |
38 | } | |
39 | ||
40 | ||
41 | public static void TestThrift2365() | |
42 | { | |
43 | var rnd = new Random(); | |
44 | for (var len = 0; len < 10; ++len) | |
45 | { | |
46 | byte[] dataWritten = new byte[len]; | |
47 | rnd.NextBytes(dataWritten); | |
48 | ||
49 | Stream stm = new MemoryStream(); | |
50 | TTransport trans = new TStreamTransport(null, stm); | |
51 | TProtocol prot = new TJSONProtocol(trans); | |
52 | prot.WriteBinary(dataWritten); | |
53 | ||
54 | stm.Position = 0; | |
55 | trans = new TStreamTransport(stm, null); | |
56 | prot = new TJSONProtocol(trans); | |
57 | byte[] dataRead = prot.ReadBinary(); | |
58 | ||
59 | Debug.Assert(dataRead.Length == dataWritten.Length); | |
60 | for (var i = 0; i < dataRead.Length; ++i) | |
61 | Debug.Assert(dataRead[i] == dataWritten[i]); | |
62 | } | |
63 | } | |
64 | ||
65 | ||
66 | public static void TestThrift2336() | |
67 | { | |
68 | const string RUSSIAN_TEXT = "\u0420\u0443\u0441\u0441\u043a\u043e\u0435 \u041d\u0430\u0437\u0432\u0430\u043d\u0438\u0435"; | |
69 | const string RUSSIAN_JSON = "\"\\u0420\\u0443\\u0441\\u0441\\u043a\\u043e\\u0435 \\u041d\\u0430\\u0437\\u0432\\u0430\\u043d\\u0438\\u0435\""; | |
70 | ||
71 | // prepare buffer with JSON data | |
72 | byte[] rawBytes = new byte[RUSSIAN_JSON.Length]; | |
73 | for (var i = 0; i < RUSSIAN_JSON.Length; ++i) | |
74 | rawBytes[i] = (byte)(RUSSIAN_JSON[i] & (char)0xFF); // only low bytes | |
75 | ||
76 | // parse and check | |
77 | var stm = new MemoryStream(rawBytes); | |
78 | var trans = new TStreamTransport(stm, null); | |
79 | var prot = new TJSONProtocol(trans); | |
80 | Debug.Assert(prot.ReadString() == RUSSIAN_TEXT, "reading JSON with hex-encoded chars > 8 bit"); | |
81 | } | |
82 | ||
83 | public static void TestThrift3403() | |
84 | { | |
85 | string GCLEF_TEXT = "\ud834\udd1e"; | |
86 | const string GCLEF_JSON = "\"\\ud834\\udd1e\""; | |
87 | ||
88 | // parse and check | |
89 | var stm = new MemoryStream(Encoding.UTF8.GetBytes(GCLEF_JSON)); | |
90 | var trans = new TStreamTransport(stm, null); | |
91 | var prot = new TJSONProtocol(trans); | |
92 | Debug.Assert(prot.ReadString() == GCLEF_TEXT, "reading JSON with surrogate pair hex-encoded chars"); | |
93 | } | |
94 | } | |
95 | } |