Esempio n. 1
0
        public byte[] Serialize(ODocument document)
        {
            var       buffer = new BinaryBuffer();
            ODocument schema = null; // Until Implementing schema this is null class

            // Version
            buffer.Add((byte)SERIALIZER_VERSION);

            // Class Name
            if (!String.IsNullOrEmpty(document.OClassName))
            {
                buffer.WriteVariant(document.OClassName.Length);
                var className = BinarySerializer.ToArray(document.OClassName);
                buffer.AddRange(className);
            }
            else
            {
                var length = BinarySerializer.ToArray((int)0);
                buffer.AddRange(length);
            }

            // Header
            var propNames = document.Keys.Where(k => !k.StartsWith("@")).ToArray();
            var pos       = new int[propNames.Length];
            var val       = new KeyValuePair <string, Object> [propNames.Length];

            for (var i = 0; i < propNames.Length; i++)
            {
                var prop = propNames[i];
                if (schema != null)
                {
                }
                else
                {
                    buffer.WriteVariant(prop.Length);
                    var propName = BinarySerializer.ToArray(prop);
                    buffer.AddRange(propName);
                    pos[i] = buffer.Allocate(sizeof(int) + 1);
                }
                val[i] = new KeyValuePair <string, object>(prop, document.GetField <object>(prop));
            }

            buffer.WriteVariant(0); // Header stop byte

            // Data
            for (int i = 0; i < val.Length; i++)
            {
                int pointer = 0;
                var value   = val[i].Value;
                if (value != null)
                {
                    var valueType = TypeConverter.TypeToDbName(value.GetType());
                    pointer = writeOType(buffer, value, valueType, getLinkedType(valueType, val[i].Key));
                    buffer.Write(pos[i], pointer);
                    if (schema == null)
                    {
                        buffer.Write((pos[i] + sizeof(int)), (byte)valueType);
                    }
                }
            }
            return(buffer.ToArray());
        }
Esempio n. 2
0
        public ODocument Deserialize(byte[] rawRecord, ODocument document)
        {
            string recordString = BinarySerializer.ToString(rawRecord).Trim();

            return(Deserialize(recordString, document));
        }