1

A simple 'Person' object is parsed as

            Person person = new Person
            {
                Id = 1,
                Name = "Foo",
                Email = "foo@bar",
                Phones = { new Person.Types.PhoneNumber { Number = "555-1212" } }
            };
            using (MemoryStream stream = new MemoryStream())
            {
                // Save the person to a stream
                person.WriteTo(stream);
                bytes = stream.ToArray();
            }
            Person copy = Person.Parser.ParseFrom(bytes);

How is a RepeatedField<> parsed?

EDIT: The question is if RepeatedFields can be sent through the wire or do they have to be bundled in a message to be passed around?

  • I'm not sure I understand your question. `Person` is a message, but `repeated Person` is a field _in_ a message, not a message. Are you asking if you can write / read a single entry of a repeated field at a time? If so I am pretty sure the answer is no, you must read/write an entire message at a time. If you're asking about the internals of parsing... no idea, but you could always dig into the source code if you were really interested! – user812786 Jan 26 '16 at 21:33
  • The former - I was beginning to wrongly think that a repeated Person could be sent as a message. If you add that as an answer, I will accept it. – Hari Hara Chandan Jan 27 '16 at 06:29
  • 1
    added, plus some more info I discovered since I was curious how this worked under the hood :) – user812786 Jan 27 '16 at 14:52

1 Answers1

0

Person is a message, so you can read and write a single instance of it, as in your example. repeated Person is a field in a message, not a message itself. You cannot read/write a repeated field, you have to read/write an entire message at a time. (Looking at the Python implementation, it appears that the encoder needs to know how long the message is in order to encode it properly, so this makes sense.)

However, there are a couple alternatives to the scenario you described:

  1. You could send a bunch of single Person messages, and gather them together at the receiving end in whatever way you need.

  2. You could define a message, let's call it People, containing a single field repeated Person and write that message. In the documentation for encoding, they note that both concatenating the strings of two messages or calling the Message::MergeFrom method will concatenate repeated fields in messages. So, you could send any number of People messages and concatenate or merge them on the receiving end. This would get you a single People message containing every Person that was sent, without having to know up front how many Person messages will be sent.

user812786
  • 4,302
  • 5
  • 38
  • 50