mapbox / pbf

A low-level, lightweight protocol buffers implementation in JavaScript.
BSD 3-Clause "New" or "Revised" License
796 stars 106 forks source link

It is not parsing nested objects #93

Closed amitguptagwl closed 6 years ago

amitguptagwl commented 6 years ago

Proto file

message Person {
    required string user_name        = 1;
    optional int64  favourite_number = 2;
    message Data {
        optional double length = 6;
        optional double dbl = 7;
        repeated string text = 8;
    }
    repeated string interests        = 3; 
    repeated bool male        = 4; 
    optional int64  dt = 5;
}

And this the object I'm parsing

var obj = {
    "user_name": "Martin ",
    "favourite_number": 1337,
    "data" : {
        "length" : 2,
        "text" : ["str1", "str2"]
    },
    "interests": ["daydreaming", "hacking"],
    "male" : true,
    "dt" : (new Date("Mon Feb 26 2018 17:42:17 GMT+0530 (IST)")).getTime()
}

But when I parse and parse back

var pbf = new Pbf();
Person.write(obj, pbf);
var buffer = pbf.finish();

// read
var pbf_reader = new Pbf(buffer);
var outputObj = Person.read(pbf_reader);

But the output object is

{
    "user_name": "Martin ",
    "favourite_number": 1337,
    "interests": [
        "daydreaming",
        "hacking"
    ],
    "male": [],
    "dt": 1519647137000
}

Can you please tell me if I'm doing something wrong.

amitguptagwl commented 6 years ago

Never mind. I found the reason. It was because of wrong proto file structure generated by some online tool.

message Data {
    optional double length = 1;
    optional double dbl = 1;
    repeated string text = 3;
}

message Person {
    required string user_name        = 1;
    optional int64  favourite_number = 2;
    required Data data = 3;
    repeated string interests        = 4; 
    required bool male        = 5; 
    optional int64  dt = 6;
}