aarondav / 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

JSON parser does not accept camel-case fieldnames. #49

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
BJF could be fixed to allow JSON field names in camelCase or sname_case. 

What steps will reproduce the problem?

1. Make a .proto file with a snake_case field_name.
2. Generate JSON from protobuf using https://github.com/chrisdew/protobuf/
3. The JSON will have a camelCase fieldName
4. PJF will fail to parse the JSON.

e.g.

1. download pjf_empty_object.tgz
2. extract it (tar -xzvf pjf_empty_object.tgz)
3. cd pjf_empty_object
4. make clean && make && make run

What is the expected output? What do you see instead?

expected:

textJson: {"text":"foo"}
textPdu: {"text": "foo"}
authAckJson: {"authAck":{}}
authAckPdu: {"authAck": {}}

actual:

textJson: {"text":"foo"}
textPdu: {"text": "foo"}
authAckJson: {"authAck":{}}
com.googlecode.protobuf.format.JsonFormat$ParseException: 1:13: Expected 
identifier. -}
    at com.googlecode.protobuf.format.JsonFormat$Tokenizer.parseException(JsonFormat.java:765)
    at com.googlecode.protobuf.format.JsonFormat$Tokenizer.consumeIdentifier(JsonFormat.java:575)
    at com.googlecode.protobuf.format.JsonFormat.handleMissingField(JsonFormat.java:970)
    at com.googlecode.protobuf.format.JsonFormat.mergeField(JsonFormat.java:936)
    at com.googlecode.protobuf.format.JsonFormat.merge(JsonFormat.java:870)
    at com.googlecode.protobuf.format.JsonFormat.merge(JsonFormat.java:818)
    at com.chrisdew.Main.main(Main.java:26)
authAckPdu: {}

What version of the product are you using? On what operating system?

$ ls libs/protobuf*
libs/protobuf-2.5.0.jar  libs/protobuf-java-format-1.2.jar

$ java -version
java version "1.6.0_45"
Java(TM) SE Runtime Environment (build 1.6.0_45-b06)
Java HotSpot(TM) 64-Bit Server VM (build 20.45-b01, mixed mode)

$ cat /etc/lsb-release 
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=12.04
DISTRIB_CODENAME=precise
DISTRIB_DESCRIPTION="Ubuntu 12.04.2 LTS"

$ uname -a
Linux chris-work 3.2.0-45-generic #70-Ubuntu SMP Wed May 29 20:12:06 UTC 2013 
x86_64 x86_64 x86_64 GNU/Linux

Please provide any additional information below.

pjfeoi.proto:

package com.chrisdew.pjfeoi;

message Pdu {
  optional string  text     = 4;
  optional AuthAck auth_ack = 5;
}
message AuthAck {
}

Main.java:

package com.chrisdew;

import com.google.protobuf.UninitializedMessageException;
import com.googlecode.protobuf.format.JsonFormat;
import com.chrisdew.pjfeoi.Pjfeoi;

public class Main {
        public static void main(String[] args) {
                String textJson = "{\"text\":\"foo\"}";
                System.out.println("textJson: " + textJson);

                Pjfeoi.Pdu.Builder builderForText = Pjfeoi.Pdu.newBuilder();
                try {
                        JsonFormat.merge(textJson, builderForText);
                } catch (JsonFormat.ParseException e) {
                        e.printStackTrace();
                }
                Pjfeoi.Pdu textPdu = builderForText.build();
                System.out.println("textPdu: " + JsonFormat.printToString(textPdu));

                String authAckJson = "{\"authAck\":{}}";
                System.out.println("authAckJson: " + authAckJson);

                Pjfeoi.Pdu.Builder builderForAuthAck = Pjfeoi.Pdu.newBuilder();
                try {
                        JsonFormat.merge(authAckJson, builderForAuthAck);
                } catch (JsonFormat.ParseException e) {
                        e.printStackTrace();
                }
                Pjfeoi.Pdu authAckPdu = builderForAuthAck.build();
                System.out.println("authAckPdu: " + JsonFormat.printToString(authAckPdu));
        }
}

P.S. this was previously 
https://code.google.com/p/protobuf-java-format/issues/detail?id=48

Original issue reported on code.google.com by cms...@gmail.com on 7 Aug 2013 at 10:46

Attachments: