Exemplo n.º 1
0
        /// <summary>
        /// Sserializes the provided object into a StrinBuilder object
        /// </summary>
        /// <param name="value">Object to be serialized</param>
        /// <param name="sb">The StringBuilder object where the serialized data will be written</param>
        /// <returns>true if value was serialized successfully; otherwise, false</returns>
        protected bool Serialize(HumanFace value, StringBuilder sb)
        {
            if (value == null)
            {
                sb.Append("null");
                return(true);
            }

            // 1. Write open brace '{'
            // 2. Write white space
            sb.Append("{ ");

            // 3. Write quoted name followed by one space
            sb.Append("\\\"");
            sb.Append(value.Name);
            sb.Append("\\\" ");

            // 4. Write Pan
            sb.Append(value.Pan.ToString("G8", System.Globalization.CultureInfo.InvariantCulture.NumberFormat));

            // 5. Write space
            sb.Append(' ');

            // 6. Write Tilt
            sb.Append(value.Tilt.ToString("G8", System.Globalization.CultureInfo.InvariantCulture.NumberFormat));

            // 7. Write space
            sb.Append(' ');

            // 8. Write confidence
            sb.Append(value.Confidence.ToString("G8", System.Globalization.CultureInfo.InvariantCulture.NumberFormat));

            // 9. Write white space followed by closing brace '}'
            sb.Append(" }");

            return(true);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Deserializes the provided object from a string
        /// </summary>
        /// <param name="serializedData">String containing the serialized object</param>
        /// <param name="cc">Read header for the serializedData string</param>
        /// <param name="value">When this method returns contains the value stored in serializedData the deserialization succeeded, or zero if the deserialization failed. The deserialization fails if the serializedData parameter is a null reference (Nothing in Visual Basic) or its content could not be parsed. This parameter is passed uninitialized</param>
        /// <returns>true if serializedData was deserialized successfully; otherwise, false</returns>
        protected bool Deserialize(string serializedData, ref int cc, out HumanFace value)
        {
            int    start;
            int    end;
            string name;
            double pan;
            double tilt;
            double confidence;

            if (String.IsNullOrEmpty(serializedData) || (String.Compare("null", serializedData.Substring(cc, 4), true) == 0))
            {
                value = null;
                cc   += 4;
                return(false);
            }

            value = null;

            if (serializedData.Length < 15)
            {
                return(false);
            }
            // 1. Read open brace '{'
            if (serializedData[cc++] != '{')
            {
                return(false);
            }
            // 2. Read white space
            if (serializedData[cc++] != ' ')
            {
                return(false);
            }

            // 3. Read name
            // 3.1. Read escaped double quotes
            if (!Scanner.ReadChar('\\', serializedData, ref cc) || !Scanner.ReadChar('"', serializedData, ref cc))
            {
                return(false);
            }
            // 3.2. Read Name
            start = cc;
            while (cc < serializedData.Length)
            {
                // 3.3. Read escaped double quotes
                if (Scanner.ReadChar('\\', serializedData, ref cc) && Scanner.ReadChar('"', serializedData, ref cc))
                {
                    break;
                }
            }

            // 3.4. Extract person name
            end = cc - 2;
            if ((end - start) < 1)
            {
                return(false);
            }
            name = serializedData.Substring(start, end - start);

            // 4. Read white space
            if (!Scanner.ReadChar(' ', serializedData, ref cc))
            {
                return(false);
            }

            // 5. Read Pan
            if (!Scanner.XtractDouble(serializedData, ref cc, out pan) ||
                (pan < HumanFace.MinimumPan) || (pan > HumanFace.MaximumPan))
            {
                return(false);
            }

            // 6. Read white space
            if (!Scanner.ReadChar(' ', serializedData, ref cc))
            {
                return(false);
            }

            // 7. Read Tilt
            if (!Scanner.XtractDouble(serializedData, ref cc, out tilt) ||
                (tilt < HumanFace.MinimumTilt) || (tilt > HumanFace.MaximumTilt))
            {
                return(false);
            }

            // 8. Read white space
            if (!Scanner.ReadChar(' ', serializedData, ref cc))
            {
                return(false);
            }

            // 9. Read confidence
            if (!Scanner.XtractDouble(serializedData, ref cc, out confidence) ||
                (confidence < 0) || (confidence > 1))
            {
                return(false);
            }

            // A. Read white space
            if (!Scanner.ReadChar(' ', serializedData, ref cc))
            {
                return(false);
            }

            // B. Read closing brace '}'
            if (!Scanner.ReadChar('}', serializedData, ref cc))
            {
                return(false);
            }

            try
            {
                value = new HumanFace(name, pan, tilt, confidence);
            }
            catch { value = null; return(false); }
            return(true);
        }