raspasov / protobuf-java-format

Automatically exported from code.google.com/p/protobuf-java-format
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

Error while parsing Json string when a non-primitive field is null #50

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1.Create a POJO class with an user defined class's object as instance member
2.Instantiate this pojo object with the member object's value as null
3.Convert this POJO to json string and invoke JsonFormat.merge()

What is the expected output? What do you see instead?
Json string should be properly mapped to its corresponding ProtoBuf object. But 
Getting exception
           Exception in thread "main" com.googlecode.protobuf.format.JsonFormat$ParseException: 1:16: Expected "{".
    at com.googlecode.protobuf.format.JsonFormat$Tokenizer.parseException(JsonFormat.java:765)
    at com.googlecode.protobuf.format.JsonFormat$Tokenizer.consume(JsonFormat.java:530)
    at com.googlecode.protobuf.format.JsonFormat.handleObject(JsonFormat.java:1128)
    at com.googlecode.protobuf.format.JsonFormat.handleValue(JsonFormat.java:1004)
    at com.googlecode.protobuf.format.JsonFormat.mergeField(JsonFormat.java:952)
    at com.googlecode.protobuf.format.JsonFormat.merge(JsonFormat.java:870)
    at com.googlecode.protobuf.format.JsonFormat.merge(JsonFormat.java:818)
    at com.ms.JSONToPB.main(JSONToPB.java:48)

What version of the product are you using? On what operating system?
 protobuf-java-format - 1.2. OS - Windows 7

Please provide any additional information below.

main function I used
--------------------
public static void main(String[] args)(){
        Source sObj = new Source();
    sObj.setMemberClass(null);
    SourceProto.Source.Builder builder = SourceProto.Source.newBuilder();
    ObjectMapper objMapper = new ObjectMapper();        
    String jsonString = objMapper.writeValueAsString(sObj);
    JsonFormat.merge(jsonString, builder);
    SourceProto.Source obj = builder.build();
}

Source.class
------------
public class Source{

    private MemberClass memberClass;    

    public MemberClass getMemberClass() {
        return memberClass;
    }

    public void setMemberClass(MemberClass memberClass) {
        this.memberClass = memberClass;
    }
}
MemberClass.class
------------------
public class MemberClass {
    private int num;

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }   
}

If we keep null value for primitive type, it is parsed properly. For 
non-primitive types, error is thrown

I went through the code and modified the JsonFormat.java at line number : 1123. 
I added the following code

        if("null".equals(tokenizer.currentToken)){
            tokenizer.consume("null");
            return null;
        }

With this null check, issue is resolved. Please check whether this is the 
proper resolution

Original issue reported on code.google.com by baluli...@gmail.com on 14 Jan 2015 at 5:25