2016-07-20 2 views
0
CreateOrderRequest req = new CreateOrderRequest(); 

req.pid=1111; 
req.name="xxx"; 

TMemoryBuffer mem_buf = new TMemoryBuffer(512); 
TJSONProtocol bin_proto = new TJSONProtocol(mem_buf,true); 

try 
{ 
    req.write(bin_proto); 
    System.out.println("====begin serial json================="); 
    String jsonStr=mem_buf.toString("utf-8"); 
    System.out.println("after jasonprocol="+jsonStr); 

    System.out.println("====begin deserial json================="); 
    CreateOrderRequest req2 = new CreateOrderRequest(); 
    TMemoryBuffer mem_buf2 = new TMemoryBuffer(512); 
    byte[] data = jsonStr.getBytes(); 
    mem_buf2.write(data); 

    TJSONProtocol bin_proto2 = new TJSONProtocol(mem_buf2,true); 
    req2.read(bin_proto2); 
    System.out.println("after de jasonprocol pid="+req.pid); 
    System.out.println("after de jasonprocol name="+req.name); 
} 
catch(Exception e) 
{ 
    System.out.println(e); 
} 

출력 :쓰기 후 다시 개체를 읽을 수 없습니다

====begin serial json================= 

after jasonprocol={"pid":{"i32":1111},"uid":{"i32":0},"corpId": {"i32":0},"sharePriceE6":{"i64":0},"shareCount":{"i32":0},"totalPriceE6":{"i64":0},"bankcardId":{"i32":0},"name":{"str":"xxx"},"isAnonymous":{"tf":0},"expectIncomeE6":{"i64":0},"clientId":{"i32":0},"totalAmtE6":{"i64":0},"subSrc":{"str":""}} 
====begin deserial json================= 

org.apache.thrift.protocol.TProtocolException: Unexpected character:p 

질문 :

fieldNamesAsString_ 후, true로 설정되어 있지 않은 경우 jasonStr{"1": {"i32":1111},"2":{"i32":0},"3":{"i32":0},...이되며 CreateOrderRequest에 다시 기록 할 수 있습니다. 합리적이라고 생각하지 않습니다.

개체를 json str에 쓸 수는 있지만 다시 읽을 수는 없습니까?

답변

0

fieldNamesAsString 매개 변수는 쓰기 전용 JSON 형식과 동일한 효과를냅니다. Thrift를 사용하여 JSON을 다시 읽을 수는 없습니다.

따라서 본질적으로 이것은 정확하게 동일한 문제는 아니지만 this issue과 다소 비슷합니다.

개체를 json str에 쓸 수는 있지만 다시 읽을 수 없습니까?

네, 여기가 그 의도입니다. 분명히 유스 케이스가있는 사람들이 있습니다.

추가 읽기 : 당신의 대답에 대한

+0

감사합니다,하지만 난 직렬화 메소드를 구현하기가 매우 복잡 할 수 없습니다 생각, 그리고 왜 그것을 할? 왜냐하면 TField는 thriftId와 fiedName, – robin

+0

@robin을 모두 저장했기 때문입니다. "* 구현하기가 너무 복잡하지 않을 것 같아요."- 이것은 오픈 소스이기 때문에 그냥 발사하십시오! [끌어 오기 요청을 환영합니다.] (https://thrift.apache.org/docs/HowToContribute). – JensG

관련 문제