public static void SerializePersonTest(Person person, ObjectReaderWriterFactory factory)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                SerializePerson(person, factory.CreateWriter(stream));

                byte[] serialized = stream.ToArray();

#if DEBUG
                string writeDump = serialized.ToAsciiDumpString();
                Debug.WriteLine(writeDump);
#endif

                Test.Output("{0} writer wrote {1} bytes", factory.Name, serialized.Length);

                // Deserializer 1

                stream.Position = 0;
                DeserializePerson1(factory.CreateReader(stream));

                // Deserializer 2

                stream.Position = 0;
                DeserializePerson2(factory.CreateReader(stream));
            }
        }
        public void SerializePersonTest()
        {
            Person person = new Person()
            {
                Id = 1,
                Name = "Foo",
                Scores = new int[] { 1, 2, 3 },
            };

            SerializePersonTest(person, new PortableBinaryObjectReaderWriterFactory());
            SerializePersonTest(person, new BencodingObjectReaderWriterFactory());
            SerializePersonTest(person, new JsonObjectReaderWriterFactory());
            SerializePersonTest(person, new XmlObjectReaderWriterFactory());
        }
        public static void SerializePerson(Person person, IObjectWriter writer)
        {
            if (person == null)
            {
                writer.WriteNullValue();
            }
            else
            {
                writer.WriteStartObject();

                writer.WriteMember(1, person.Id);
                writer.WriteMember(2, person.Name);
                writer.WriteMember(3, person.Scores);

                writer.WriteEndObject();
            }
        }
        public static Person DeserializePerson1(IObjectReader reader)
        {
            if (!reader.ReadStartObject())
            {
                return null;
            }

            Person person = new Person();

            while (reader.ReadNextMemberKey())
            {
                if (reader.MemberKey == 1)
                {
                    person.Id = reader.ReadValueAsInt32();
                }
                else if (reader.MemberKey == 2)
                {
                    person.Name = reader.ReadValueAsString(250);
                }
                else if (reader.MemberKey == 3)
                {
                    person.Scores = reader.ReadValueAsInt32Array();
                }
            }

            reader.ReadEndObject();

            return person;
        }
        public static Person DeserializePerson2(IObjectReader reader)
        {
            var obj = reader.GetObject();
            if (obj == null)
            {
                return null;
            }

            Person person = new Person();

            foreach (var member in obj.Members)
            {
                if (member.MemberKey == 1)
                {
                    person.Id = member.ReadValueAsInt32();
                }
                else if (member.MemberKey == 2)
                {
                    person.Name = member.ReadValueAsString(250);
                }
                else if (member.MemberKey == 3)
                {
                    person.Scores = member.ReadValueAsInt32Array();
                }
            }

            return person;
        }