Best solution to merge ByteStrings

31 views
Skip to first unread message

Joan Balagueró

unread,
Apr 12, 2024, 11:27:35 AMApr 12
to Protocol Buffers
Hello,

I have a protobuf like this in a ByteString variable 'cp':

1: {
1: 30
2: {
2: 100
}
}
   
   
And I also have another ByteString 'hotel' with this content:

     1: 87354
     2: {
(...) a lot of content here
     }

 
Now I need:

1. Put the 'hotel' ByteString under a "Hotels" tag with key = 2:

2: {
 1: 87354
 2: {
(...) a lot of content here
 }
}
 
 
2. And finally "concat" these two ByetString 'cp' and 'hotels' and put them under another tag "Success" with key = "1":

1: {
1: {
1: 30
2: {
2: 100
}
}
2: {
1: 87354
2: {
(...) a lot of content here
}
}
}

I have solved this and it's working. What I did:

// Add hotel under '2' (hotels) and "concat" cp + hotels
UnknownFieldSet.Builder b2 = UnknownFieldSet.newBuilder().mergeFrom(cp).mergeLengthDelimitedField(2, hotel);

// Add 'b2' to '1' ('Success')
UnknownFieldSet.Builder b1 = UnknownFieldSet.newBuilder().mergeLengthDelimitedField(1, b2.build().toByteString());


But since I'm a newbie in protobuf, I would like to know if my solution is the optimal or there is a better way to solve this.

Any suggestion would be really appreciated.

Thanks,

Joan.
Reply all
Reply to author
Forward
0 new messages