1 /* 2 * Copyright (c) Facebook, Inc. and its affiliates. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.facebook.thrift.protocol; 18 19 import com.facebook.thrift.TException; 20 import com.facebook.thrift.transport.TTransport; 21 import java.util.Map; 22 23 /** 24 * JSON protocol implementation for thrift. 25 * 26 * <p>This is a full-featured protocol supporting write and read. 27 * 28 * <p>Please see the C++ class header for a detailed description of the protocol's wire format. 29 */ 30 public class TJSONProtocol extends TJSONProtocolBase { 31 32 /** Factory for JSON protocol objects */ 33 @SuppressWarnings("serial") 34 public static class Factory implements TProtocolFactory { 35 getProtocol(TTransport trans)36 public TProtocol getProtocol(TTransport trans) { 37 return new TJSONProtocol(trans); 38 } 39 } 40 41 /** Constructor */ TJSONProtocol(TTransport trans)42 public TJSONProtocol(TTransport trans) { 43 super(trans); 44 } 45 46 @Override reset()47 public void reset() { 48 super.reset(); 49 } 50 51 @Override writeStructBegin(TStruct struct)52 public void writeStructBegin(TStruct struct) throws TException { 53 writeJSONObjectStart(); 54 } 55 56 @Override writeStructEnd()57 public void writeStructEnd() throws TException { 58 writeJSONObjectEnd(); 59 } 60 61 @Override writeFieldBegin(TField field)62 public void writeFieldBegin(TField field) throws TException { 63 writeJSONInteger(field.id); 64 writeJSONObjectStart(); 65 writeJSONString(getTypeNameForTypeID(field.type)); 66 } 67 68 @Override writeFieldEnd()69 public void writeFieldEnd() throws TException { 70 writeJSONObjectEnd(); 71 } 72 73 @Override writeFieldStop()74 public void writeFieldStop() {} 75 76 @Override writeMapBegin(TMap map)77 public void writeMapBegin(TMap map) throws TException { 78 writeJSONArrayStart(); 79 writeJSONString(getTypeNameForTypeID(map.keyType)); 80 writeJSONString(getTypeNameForTypeID(map.valueType)); 81 writeJSONInteger(map.size); 82 writeJSONObjectStart(); 83 } 84 85 @Override writeMapEnd()86 public void writeMapEnd() throws TException { 87 writeJSONObjectEnd(); 88 writeJSONArrayEnd(); 89 } 90 91 @Override writeListBegin(TList list)92 public void writeListBegin(TList list) throws TException { 93 writeJSONArrayStart(); 94 writeJSONString(getTypeNameForTypeID(list.elemType)); 95 writeJSONInteger(list.size); 96 } 97 98 @Override writeListEnd()99 public void writeListEnd() throws TException { 100 writeJSONArrayEnd(); 101 } 102 103 @Override writeSetBegin(TSet set)104 public void writeSetBegin(TSet set) throws TException { 105 writeJSONArrayStart(); 106 writeJSONString(getTypeNameForTypeID(set.elemType)); 107 writeJSONInteger(set.size); 108 } 109 110 @Override writeSetEnd()111 public void writeSetEnd() throws TException { 112 writeJSONArrayEnd(); 113 } 114 115 /** Reading methods. */ 116 @Override readStructBegin( Map<Integer, com.facebook.thrift.meta_data.FieldMetaData> metaDataMap)117 public TStruct readStructBegin( 118 Map<Integer, com.facebook.thrift.meta_data.FieldMetaData> metaDataMap) throws TException { 119 readJSONObjectStart(); 120 return ANONYMOUS_STRUCT; 121 } 122 123 @Override readStructEnd()124 public void readStructEnd() throws TException { 125 readJSONObjectEnd(); 126 } 127 128 @Override readFieldBegin()129 public TField readFieldBegin() throws TException { 130 byte ch = reader_.peek(); 131 byte type; 132 short id = 0; 133 if (ch == RBRACE[0]) { 134 type = TType.STOP; 135 } else { 136 id = (short) readJSONInteger(); 137 readJSONObjectStart(); 138 type = getTypeIDForTypeName(readJSONString(false).get()); 139 } 140 return new TField("", type, id); 141 } 142 143 @Override readFieldEnd()144 public void readFieldEnd() throws TException { 145 readJSONObjectEnd(); 146 } 147 148 @Override readMapBegin()149 public TMap readMapBegin() throws TException { 150 readJSONArrayStart(); 151 byte keyType = getTypeIDForTypeName(readJSONString(false).get()); 152 byte valueType = getTypeIDForTypeName(readJSONString(false).get()); 153 int size = (int) readJSONInteger(); 154 readJSONObjectStart(); 155 return new TMap(keyType, valueType, size); 156 } 157 158 @Override readMapEnd()159 public void readMapEnd() throws TException { 160 readJSONObjectEnd(); 161 readJSONArrayEnd(); 162 } 163 164 @Override readListBegin()165 public TList readListBegin() throws TException { 166 readJSONArrayStart(); 167 byte elemType = getTypeIDForTypeName(readJSONString(false).get()); 168 int size = (int) readJSONInteger(); 169 return new TList(elemType, size); 170 } 171 172 @Override readListEnd()173 public void readListEnd() throws TException { 174 readJSONArrayEnd(); 175 } 176 177 @Override readSetBegin()178 public TSet readSetBegin() throws TException { 179 readJSONArrayStart(); 180 byte elemType = getTypeIDForTypeName(readJSONString(false).get()); 181 int size = (int) readJSONInteger(); 182 return new TSet(elemType, size); 183 } 184 185 @Override readSetEnd()186 public void readSetEnd() throws TException { 187 readJSONArrayEnd(); 188 } 189 } 190