package demo;
import codec.ReqWrapper;
import com.google.protobuf.util.JsonFormat;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
* @author NickLiu on 2017/1/5 0005 16:17.
*/
public class TestProtocol {
public static void main(String[] args) throws Exception {
String json = "{\"id\":1,\"name\":\"NickLiu\"}";
JsonFormat.Parser parser = JsonFormat.parser();
ReqWrapper.ReqEntity.Builder builder = ReqWrapper.ReqEntity.newBuilder();
parser.merge(json,builder);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ReqWrapper.ReqEntity reqEntity = builder.build();
reqEntity.writeTo(bos);
byte[] bytes = bos.toByteArray();
System.out.println("length="+bytes.length);
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
ReqWrapper.ReqEntity deReqEntity = ReqWrapper.ReqEntity.parseFrom(bis);
System.out.println("id="+deReqEntity.getId()+",name="+deReqEntity.getName());
JsonFormat.Printer printer = JsonFormat.printer();
System.out.println("msg="+printer.print(deReqEntity));
ByteArrayOutputStream bos_jdk = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos_jdk);
oos.writeObject(deReqEntity);
oos.flush();
oos.close();
System.out.println("length="+bos_jdk.toByteArray().length);
}
}