]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | // Licensed to the Apache Software Foundation(ASF) under one |
2 | // or more contributor license agreements.See the NOTICE file | |
3 | // distributed with this work for additional information | |
4 | // regarding copyright ownership.The ASF licenses this file | |
5 | // to you under the Apache License, Version 2.0 (the | |
6 | // "License"); you may not use this file except in compliance | |
7 | // with the License. You may obtain a copy of the License at | |
8 | // | |
9 | // http://www.apache.org/licenses/LICENSE-2.0 | |
10 | // | |
11 | // Unless required by applicable law or agreed to in writing, | |
12 | // software distributed under the License is distributed on an | |
13 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
14 | // KIND, either express or implied. See the License for the | |
15 | // specific language governing permissions and limitations | |
16 | // under the License. | |
17 | ||
18 | using Thrift.Protocol.Entities; | |
19 | ||
20 | namespace Thrift.Protocol.Utilities | |
21 | { | |
22 | // ReSharper disable once InconsistentNaming | |
23 | public static class TJSONProtocolHelper | |
24 | { | |
25 | public static byte[] GetTypeNameForTypeId(TType typeId) | |
26 | { | |
27 | switch (typeId) | |
28 | { | |
29 | case TType.Bool: | |
30 | return TJSONProtocolConstants.TypeNames.NameBool; | |
31 | case TType.Byte: | |
32 | return TJSONProtocolConstants.TypeNames.NameByte; | |
33 | case TType.I16: | |
34 | return TJSONProtocolConstants.TypeNames.NameI16; | |
35 | case TType.I32: | |
36 | return TJSONProtocolConstants.TypeNames.NameI32; | |
37 | case TType.I64: | |
38 | return TJSONProtocolConstants.TypeNames.NameI64; | |
39 | case TType.Double: | |
40 | return TJSONProtocolConstants.TypeNames.NameDouble; | |
41 | case TType.String: | |
42 | return TJSONProtocolConstants.TypeNames.NameString; | |
43 | case TType.Struct: | |
44 | return TJSONProtocolConstants.TypeNames.NameStruct; | |
45 | case TType.Map: | |
46 | return TJSONProtocolConstants.TypeNames.NameMap; | |
47 | case TType.Set: | |
48 | return TJSONProtocolConstants.TypeNames.NameSet; | |
49 | case TType.List: | |
50 | return TJSONProtocolConstants.TypeNames.NameList; | |
51 | default: | |
52 | throw new TProtocolException(TProtocolException.NOT_IMPLEMENTED, "Unrecognized exType"); | |
53 | } | |
54 | } | |
55 | ||
56 | public static TType GetTypeIdForTypeName(byte[] name) | |
57 | { | |
58 | var result = TType.Stop; | |
59 | if (name.Length > 1) | |
60 | { | |
61 | switch (name[0]) | |
62 | { | |
63 | case (byte) 'd': | |
64 | result = TType.Double; | |
65 | break; | |
66 | case (byte) 'i': | |
67 | switch (name[1]) | |
68 | { | |
69 | case (byte) '8': | |
70 | result = TType.Byte; | |
71 | break; | |
72 | case (byte) '1': | |
73 | result = TType.I16; | |
74 | break; | |
75 | case (byte) '3': | |
76 | result = TType.I32; | |
77 | break; | |
78 | case (byte) '6': | |
79 | result = TType.I64; | |
80 | break; | |
81 | } | |
82 | break; | |
83 | case (byte) 'l': | |
84 | result = TType.List; | |
85 | break; | |
86 | case (byte) 'm': | |
87 | result = TType.Map; | |
88 | break; | |
89 | case (byte) 'r': | |
90 | result = TType.Struct; | |
91 | break; | |
92 | case (byte) 's': | |
93 | if (name[1] == (byte) 't') | |
94 | { | |
95 | result = TType.String; | |
96 | } | |
97 | else if (name[1] == (byte) 'e') | |
98 | { | |
99 | result = TType.Set; | |
100 | } | |
101 | break; | |
102 | case (byte) 't': | |
103 | result = TType.Bool; | |
104 | break; | |
105 | } | |
106 | } | |
107 | if (result == TType.Stop) | |
108 | { | |
109 | throw new TProtocolException(TProtocolException.NOT_IMPLEMENTED, "Unrecognized exType"); | |
110 | } | |
111 | return result; | |
112 | } | |
113 | ||
114 | /// <summary> | |
115 | /// Return true if the given byte could be a valid part of a JSON number. | |
116 | /// </summary> | |
117 | public static bool IsJsonNumeric(byte b) | |
118 | { | |
119 | switch (b) | |
120 | { | |
121 | case (byte)'+': | |
122 | case (byte)'-': | |
123 | case (byte)'.': | |
124 | case (byte)'0': | |
125 | case (byte)'1': | |
126 | case (byte)'2': | |
127 | case (byte)'3': | |
128 | case (byte)'4': | |
129 | case (byte)'5': | |
130 | case (byte)'6': | |
131 | case (byte)'7': | |
132 | case (byte)'8': | |
133 | case (byte)'9': | |
134 | case (byte)'E': | |
135 | case (byte)'e': | |
136 | return true; | |
137 | default: | |
138 | return false; | |
139 | } | |
140 | } | |
141 | ||
142 | /// <summary> | |
143 | /// Convert a byte containing a hex char ('0'-'9' or 'a'-'f') into its | |
144 | /// corresponding hex value | |
145 | /// </summary> | |
146 | public static byte ToHexVal(byte ch) | |
147 | { | |
148 | if (ch >= '0' && ch <= '9') | |
149 | { | |
150 | return (byte)((char)ch - '0'); | |
151 | } | |
152 | ||
153 | if (ch >= 'a' && ch <= 'f') | |
154 | { | |
155 | ch += 10; | |
156 | return (byte)((char)ch - 'a'); | |
157 | } | |
158 | ||
159 | throw new TProtocolException(TProtocolException.INVALID_DATA, "Expected hex character"); | |
160 | } | |
161 | ||
162 | /// <summary> | |
163 | /// Convert a byte containing a hex value to its corresponding hex character | |
164 | /// </summary> | |
165 | public static byte ToHexChar(byte val) | |
166 | { | |
167 | val &= 0x0F; | |
168 | if (val < 10) | |
169 | { | |
170 | return (byte)((char)val + '0'); | |
171 | } | |
172 | val -= 10; | |
173 | return (byte)((char)val + 'a'); | |
174 | } | |
175 | } | |
176 | } |