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 package com.facebook.thrift.protocol; 21 22 import com.facebook.thrift.TException; 23 import com.facebook.thrift.meta_data.FieldMetaData; 24 import com.facebook.thrift.transport.TTransport; 25 import java.util.Map; 26 27 /** 28 * JSON protocol implementation for thrift. 29 * 30 * <p>This should not be confused with the TJSONProtocol. This protocol store data in JSON-styled 31 * "field name" -> "field value", but it doesn't preserve field id nor field type (which doesn't 32 * give the user the same backward/forward compatibility guarantees). 33 * 34 * <p>It is not recommended to use this protocol for RPC that needs backward/forward compatibility 35 * guarantees. 36 */ 37 public class TSimpleJSONProtocol extends TProtocol { 38 private final AbstractTSimpleJSONProtocol delegate; 39 40 /** Factory */ 41 public static class Factory implements TProtocolFactory { 42 private final boolean useBase64; 43 Factory()44 public Factory() { 45 this(false); 46 } 47 Factory(boolean useBase64)48 public Factory(boolean useBase64) { 49 this.useBase64 = useBase64; 50 } 51 getProtocol(TTransport trans)52 public TProtocol getProtocol(TTransport trans) { 53 return new TSimpleJSONProtocol(trans, this.useBase64); 54 } 55 getProtocol(TTransport trans, boolean useBase64)56 public TProtocol getProtocol(TTransport trans, boolean useBase64) { 57 return new TSimpleJSONProtocol(trans, this.useBase64 || useBase64); 58 } 59 } 60 61 /** Constructor */ TSimpleJSONProtocol(TTransport trans)62 public TSimpleJSONProtocol(TTransport trans) { 63 this(trans, false); 64 } 65 66 /** 67 * Creates new TSimpleJSONProtocol with the option to enabled base64 encoding. 68 * 69 * @param trans transport encoded too 70 * @param useBase64 if true will encode binary using base 64 71 */ TSimpleJSONProtocol(TTransport trans, boolean useBase64)72 public TSimpleJSONProtocol(TTransport trans, boolean useBase64) { 73 super(trans); 74 this.delegate = 75 useBase64 ? new Base64TSimpleJSONProtocol(trans) : new DefaultTSimpleJSONProtocol(trans); 76 } 77 78 @Override writeMessageBegin(TMessage message)79 public void writeMessageBegin(TMessage message) throws TException { 80 delegate.writeMessageBegin(message); 81 } 82 83 @Override writeMessageEnd()84 public void writeMessageEnd() throws TException { 85 delegate.writeMessageEnd(); 86 } 87 88 @Override writeStructBegin(TStruct struct)89 public void writeStructBegin(TStruct struct) throws TException { 90 delegate.writeStructBegin(struct); 91 } 92 93 @Override writeStructEnd()94 public void writeStructEnd() throws TException { 95 delegate.writeStructEnd(); 96 } 97 98 @Override writeFieldBegin(TField field)99 public void writeFieldBegin(TField field) throws TException { 100 delegate.writeFieldBegin(field); 101 } 102 103 @Override writeFieldEnd()104 public void writeFieldEnd() { 105 delegate.writeFieldEnd(); 106 } 107 108 @Override writeFieldStop()109 public void writeFieldStop() { 110 delegate.writeFieldStop(); 111 } 112 113 @Override writeMapBegin(TMap map)114 public void writeMapBegin(TMap map) throws TException { 115 delegate.writeMapBegin(map); 116 } 117 118 @Override writeMapEnd()119 public void writeMapEnd() throws TException { 120 delegate.writeMapEnd(); 121 } 122 123 @Override writeListBegin(TList list)124 public void writeListBegin(TList list) throws TException { 125 delegate.writeListBegin(list); 126 } 127 128 @Override writeListEnd()129 public void writeListEnd() throws TException { 130 delegate.writeListEnd(); 131 } 132 133 @Override writeSetBegin(TSet set)134 public void writeSetBegin(TSet set) throws TException { 135 delegate.writeSetBegin(set); 136 } 137 138 @Override writeSetEnd()139 public void writeSetEnd() throws TException { 140 delegate.writeSetEnd(); 141 } 142 143 @Override writeBool(boolean b)144 public void writeBool(boolean b) throws TException { 145 delegate.writeBool(b); 146 } 147 148 @Override writeByte(byte b)149 public void writeByte(byte b) throws TException { 150 delegate.writeByte(b); 151 } 152 153 @Override writeI16(short i16)154 public void writeI16(short i16) throws TException { 155 delegate.writeI16(i16); 156 } 157 158 @Override writeI32(int i32)159 public void writeI32(int i32) throws TException { 160 delegate.writeI32(i32); 161 } 162 _writeStringData(String s)163 public void _writeStringData(String s) throws TException { 164 delegate._writeStringData(s); 165 } 166 167 @Override writeI64(long i64)168 public void writeI64(long i64) throws TException { 169 delegate.writeI64(i64); 170 } 171 172 @Override writeFloat(float flt)173 public void writeFloat(float flt) throws TException { 174 delegate.writeFloat(flt); 175 } 176 177 @Override writeDouble(double dub)178 public void writeDouble(double dub) throws TException { 179 delegate.writeDouble(dub); 180 } 181 182 @Override writeString(String str)183 public void writeString(String str) throws TException { 184 delegate.writeString(str); 185 } 186 187 @Override writeBinary(byte[] bin)188 public void writeBinary(byte[] bin) throws TException { 189 delegate.writeBinary(bin); 190 } 191 readJSONSyntaxChar(byte[] b)192 public void readJSONSyntaxChar(byte[] b) throws TException { 193 delegate.readJSONSyntaxChar(b); 194 } 195 readJSONSyntaxString(byte[] expected)196 public void readJSONSyntaxString(byte[] expected) throws TException { 197 delegate.readJSONSyntaxString(expected); 198 } 199 readJSONFloat()200 public float readJSONFloat() throws TException { 201 return delegate.readJSONFloat(); 202 } 203 204 @Override readMessageBegin()205 public TMessage readMessageBegin() throws TException { 206 return delegate.readMessageBegin(); 207 } 208 209 @Override readMessageEnd()210 public void readMessageEnd() throws TException { 211 delegate.readMessageEnd(); 212 } 213 214 @Override readStructBegin(Map<Integer, FieldMetaData> fieldMetadata)215 public TStruct readStructBegin(Map<Integer, FieldMetaData> fieldMetadata) throws TException { 216 return delegate.readStructBegin(fieldMetadata); 217 } 218 219 @Override readStructBegin()220 public TStruct readStructBegin() { 221 return delegate.readStructBegin(); 222 } 223 224 @Override readStructEnd()225 public void readStructEnd() throws TException { 226 delegate.readStructEnd(); 227 } 228 229 @Override readFieldBegin()230 public TField readFieldBegin() throws TException { 231 return delegate.readFieldBegin(); 232 } 233 234 @Override readFieldEnd()235 public void readFieldEnd() throws TException { 236 delegate.readFieldEnd(); 237 } 238 239 @Override readMapBegin()240 public TMap readMapBegin() throws TException { 241 return delegate.readMapBegin(); 242 } 243 244 @Override peekMap()245 public boolean peekMap() throws TException { 246 return delegate.peekMap(); 247 } 248 249 @Override readMapEnd()250 public void readMapEnd() throws TException { 251 delegate.readMapEnd(); 252 } 253 254 @Override readListBegin()255 public TList readListBegin() throws TException { 256 return delegate.readListBegin(); 257 } 258 259 @Override peekList()260 public boolean peekList() throws TException { 261 return delegate.peekList(); 262 } 263 264 @Override readListEnd()265 public void readListEnd() throws TException { 266 delegate.readListEnd(); 267 } 268 269 @Override readSetBegin()270 public TSet readSetBegin() throws TException { 271 return delegate.readSetBegin(); 272 } 273 274 @Override peekSet()275 public boolean peekSet() throws TException { 276 return delegate.peekSet(); 277 } 278 279 @Override readSetEnd()280 public void readSetEnd() throws TException { 281 delegate.readSetEnd(); 282 } 283 284 @Override readBool()285 public boolean readBool() throws TException { 286 return delegate.readBool(); 287 } 288 289 @Override readByte()290 public byte readByte() throws TException { 291 return delegate.readByte(); 292 } 293 294 @Override readI16()295 public short readI16() throws TException { 296 return delegate.readI16(); 297 } 298 299 @Override readI32()300 public int readI32() throws TException { 301 return delegate.readI32(); 302 } 303 304 @Override readI64()305 public long readI64() throws TException { 306 return delegate.readI64(); 307 } 308 309 @Override readDouble()310 public double readDouble() throws TException { 311 return delegate.readDouble(); 312 } 313 314 @Override readFloat()315 public float readFloat() throws TException { 316 return delegate.readFloat(); 317 } 318 319 @Override readString()320 public String readString() throws TException { 321 return delegate.readString(); 322 } 323 324 @Override readBinary()325 public byte[] readBinary() throws TException { 326 return delegate.readBinary(); 327 } 328 329 @Override skipBinary()330 public void skipBinary() throws TException { 331 delegate.skipBinary(); 332 } 333 getTypeIDForPeekedByte(byte peekedByte)334 public static byte getTypeIDForPeekedByte(byte peekedByte) throws TException { 335 return AbstractTSimpleJSONProtocol.getTypeIDForPeekedByte(peekedByte); 336 } 337 pushWriteContext(AbstractTSimpleJSONProtocol.Context c)338 public void pushWriteContext(AbstractTSimpleJSONProtocol.Context c) { 339 delegate.pushWriteContext(c); 340 } 341 popWriteContext()342 public void popWriteContext() { 343 delegate.popWriteContext(); 344 } 345 assertContextIsNotMapKey(String invalidKeyType)346 public void assertContextIsNotMapKey(String invalidKeyType) 347 throws AbstractTSimpleJSONProtocol.CollectionMapKeyException { 348 delegate.assertContextIsNotMapKey(invalidKeyType); 349 } 350 } 351