blob: 67c7bc065a02205b84418e1428d6368ced565bf3 [file] [log] [blame]
Jens Geyer62445c12022-06-29 00:00:00 +02001// Licensed to the Apache Software Foundation(ASF) under one
Jens Geyeraa0c8b32019-01-28 23:27:45 +01002// 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
18using Thrift.Protocol.Entities;
19
20namespace 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;
Jens Geyer62445c12022-06-29 00:00:00 +020051 case TType.Uuid:
52 return TJSONProtocolConstants.TypeNames.NameUuid;
Jens Geyeraa0c8b32019-01-28 23:27:45 +010053 default:
54 throw new TProtocolException(TProtocolException.NOT_IMPLEMENTED, "Unrecognized exType");
55 }
56 }
57
58 public static TType GetTypeIdForTypeName(byte[] name)
59 {
60 var result = TType.Stop;
61 if (name.Length > 1)
62 {
63 switch (name[0])
64 {
65 case (byte) 'd':
66 result = TType.Double;
67 break;
68 case (byte) 'i':
69 switch (name[1])
70 {
71 case (byte) '8':
72 result = TType.Byte;
73 break;
74 case (byte) '1':
75 result = TType.I16;
76 break;
77 case (byte) '3':
78 result = TType.I32;
79 break;
80 case (byte) '6':
81 result = TType.I64;
82 break;
83 }
84 break;
85 case (byte) 'l':
86 result = TType.List;
87 break;
88 case (byte) 'm':
89 result = TType.Map;
90 break;
91 case (byte) 'r':
92 result = TType.Struct;
93 break;
94 case (byte) 's':
95 if (name[1] == (byte) 't')
96 {
97 result = TType.String;
98 }
99 else if (name[1] == (byte) 'e')
100 {
101 result = TType.Set;
102 }
103 break;
104 case (byte) 't':
105 result = TType.Bool;
106 break;
Jens Geyer62445c12022-06-29 00:00:00 +0200107 case (byte)'u':
108 result = TType.Uuid;
109 break;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100110 }
111 }
112 if (result == TType.Stop)
113 {
114 throw new TProtocolException(TProtocolException.NOT_IMPLEMENTED, "Unrecognized exType");
115 }
116 return result;
117 }
118
119 /// <summary>
120 /// Return true if the given byte could be a valid part of a JSON number.
121 /// </summary>
122 public static bool IsJsonNumeric(byte b)
123 {
124 switch (b)
125 {
126 case (byte)'+':
127 case (byte)'-':
128 case (byte)'.':
129 case (byte)'0':
130 case (byte)'1':
131 case (byte)'2':
132 case (byte)'3':
133 case (byte)'4':
134 case (byte)'5':
135 case (byte)'6':
136 case (byte)'7':
137 case (byte)'8':
138 case (byte)'9':
139 case (byte)'E':
140 case (byte)'e':
141 return true;
142 default:
143 return false;
144 }
145 }
146
147 /// <summary>
148 /// Convert a byte containing a hex char ('0'-'9' or 'a'-'f') into its
149 /// corresponding hex value
150 /// </summary>
151 public static byte ToHexVal(byte ch)
152 {
153 if (ch >= '0' && ch <= '9')
154 {
155 return (byte)((char)ch - '0');
156 }
157
158 if (ch >= 'a' && ch <= 'f')
159 {
160 ch += 10;
161 return (byte)((char)ch - 'a');
162 }
163
164 throw new TProtocolException(TProtocolException.INVALID_DATA, "Expected hex character");
165 }
166
167 /// <summary>
168 /// Convert a byte containing a hex value to its corresponding hex character
169 /// </summary>
170 public static byte ToHexChar(byte val)
171 {
172 val &= 0x0F;
173 if (val < 10)
174 {
175 return (byte)((char)val + '0');
176 }
177 val -= 10;
178 return (byte)((char)val + 'a');
179 }
180 }
Jens Geyer62445c12022-06-29 00:00:00 +0200181}