Exemplo n.º 1
0
        private void WriteValue(object value)
        {
            if (value == null)
            {
                return;
            }

            if (value is byte)
            {
                output.Write((byte)value);
            }
            else if (value is short)
            {
                output.Write((short)value);
            }
            else if (value is int)
            {
                output.Write((int)value);
            }
            else if (value is long)
            {
                output.Write((long)value);
            }
            else if (value is float)
            {
                output.Write((float)value);
            }
            else if (value is double)
            {
                output.Write((double)value);
            }
            else if (value is DateTime)
            {
                output.Write(UnixDateTime.ToUnixTimestamp((DateTime)value));
            }
            else if (value is String)
            {
                string s  = (string)value;
                int    sz = s.Length;
                output.Write(sz);
                for (int i = 0; i < sz; i++)
                {
                    output.Write(s[i]);
                }
            }
            else if (value is Array)
            {
                //TODO:
            }
            else if (value is PathStruct)
            {
                PathStruct pathStruct  = (PathStruct)value;
                string[]   memberNames = pathStruct.MemberNames;
                int        sz          = pathStruct.MemberCount;

                output.Write(sz);
                for (int i = 0; i < sz; i++)
                {
                    string memberName = memberNames[i];
                    WriteValue(memberName);
                    Write(pathStruct.GetValue(memberName));
                }
            }
            else
            {
                throw new ArgumentException("The value is not supported.");
            }
        }
Exemplo n.º 2
0
        private PathValue ReadValue(PathValueType type)
        {
            if (type == PathValueType.Null)
            {
                return(PathValue.Null);
            }

            if (type == PathValueType.Boolean)
            {
                return(new PathValue(input.ReadBoolean()));
            }

            if (type == PathValueType.Byte)
            {
                return(new PathValue(input.ReadByte()));
            }
            if (type == PathValueType.Int16)
            {
                return(new PathValue(input.ReadInt16()));
            }
            if (type == PathValueType.Int32)
            {
                return(new PathValue(input.ReadInt32()));
            }
            if (type == PathValueType.Int64)
            {
                return(new PathValue(input.ReadInt64()));
            }
            if (type == PathValueType.Single)
            {
                return(new PathValue(input.ReadSingle()));
            }
            if (type == PathValueType.Double)
            {
                return(new PathValue(input.ReadDouble()));
            }

            if (type == PathValueType.String)
            {
                int           length = input.ReadInt32();
                StringBuilder sb     = new StringBuilder(length);
                for (int i = 0; i < length; i++)
                {
                    sb.Append(input.ReadChar());
                }

                return(new PathValue(sb.ToString()));
            }

            if (type == PathValueType.DateTime)
            {
                return(new PathValue(UnixDateTime.ToDateTime(input.ReadInt64())));
            }

            if (type == PathValueType.Struct)
            {
                int        memberCount = input.ReadInt32();
                PathStruct pathStruct  = new PathStruct();
                for (int i = 0; i < memberCount; i++)
                {
                    string    memberName = ReadValue(PathValueType.String);
                    PathValue value      = ReadValue();
                    pathStruct.SetValue(memberName, value);
                }

                return(new PathValue(pathStruct));
            }

            if (type == PathValueType.Array)
            {
                PathValueType elementType = (PathValueType)input.ReadByte();
                int           length      = input.ReadInt32();
                PathValue     array       = PathValue.CreateArray(elementType, length);

                for (int i = 0; i < length; i++)
                {
                    PathValue value = ReadValue(elementType);
                    array.SetValue(i, value.Value);
                }

                return(array);
            }

            throw new NotSupportedException();
        }