예제 #1
0
        public override object Deserialize(ContextDeserialization context)
        {
            // Result
            var res = new List <object>();

            // Burn bracket open, "["
            context.Burn(1);
            // Go to first element
            context.BurnWhitespaces();
            // Loop until we hit brackend end
            var isFirst = true;

            while (context.ReadChar() != ']')
            {
                // If not first element, need to burn the ","
                if (!isFirst)
                {
                    context.Burn(1);
                    context.BurnWhitespaces();
                }
                isFirst = false;
                // Read the generic element
                res.Add(context.ReadGeneric());
                // Go to next stop
                context.BurnWhitespaces();
            }
            // Burn bracket end, "]"
            context.Burn(1);
            // Done, return list
            return(res);
        }
예제 #2
0
        public override object Deserialize(ContextDeserialization context)
        {
            // Parsing flags
            var isDecimal    = false;
            var bigValue     = (double)0;
            var decimalPoint = (double)1;
            var multiplier   = 1;

            // While we have numbers chars
            while (true)
            {
                // Keep reading
                var curr = context.ReadChar();
                // If it is a digit
                var digit = (double)0;
                if (digits.TryGetValue(curr, out digit))
                {
                    if (isDecimal)
                    {
                        decimalPoint *= 10;
                    }
                    bigValue *= 10;
                    bigValue += digit;
                }
                // If its something else
                else
                {
                    // If special char, set some flags
                    if (specials.Contains(curr))
                    {
                        if (curr == '.')
                        {
                            isDecimal = true;
                        }
                        if (curr == '-')
                        {
                            multiplier *= -1;
                        }
                    }
                    // If no known, it means we just hit the end of the number
                    else
                    {
                        break;
                    }
                }
                // Burn parsed digit
                context.Burn(1);
            }
            // If result is decimal
            if (isDecimal)
            {
                return((double)(multiplier * (bigValue / decimalPoint)));
            }
            // If result is integer
            else
            {
                return((long)(multiplier * bigValue));
            }
        }
예제 #3
0
        public T DeserializeString <T>(string str)
        {
            var context = new ContextDeserialization(str);

            context.deserializers = deserializers;
            var result = (T)context.ReadGeneric();

            context.Close();
            return(result);
        }
예제 #4
0
        public override object Deserialize(ContextDeserialization context)
        {
            var value = context.ReadString(4);

            if (value == "null")
            {
                context.Burn(4);
                return(null);
            }
            throw new Exception("Json unknown token: " + value);
        }
예제 #5
0
        public override object Deserialize(ContextDeserialization context)
        {
            var isTrue = context.ReadString(4) == "true";

            if (isTrue)
            {
                context.Burn(4);
                return(true);
            }
            var isFalse = context.ReadString(5) == "false";

            if (isFalse)
            {
                context.Burn(5);
                return(false);
            }
            throw new Exception("Json unknown token: " + context.ReadString(1));
        }
        public override object Deserialize(ContextDeserialization context)
        {
            // Result
            var res = new Dictionary <string, object>();

            // Burn bracket open, "{"
            context.Burn(1);
            // Go to first element
            context.BurnWhitespaces();
            // Loop until we hit brackend end, "}"
            var isFirst = true;

            while (context.ReadChar() != '}')
            {
                // If not first element, need to burn the ","
                if (!isFirst)
                {
                    context.Burn(1);
                    context.BurnWhitespaces();
                }
                isFirst = false;
                // Read the generic key
                var key = (string)context.ReadGeneric();
                // Go to key delimiter
                context.BurnWhitespaces();
                // Burn key delimiter, ":"
                context.Burn(1);
                // go to value
                context.BurnWhitespaces();
                // Read the generic element
                var value = context.ReadGeneric();
                // Save result
                res[key] = value;
                // Go to next stop
                context.BurnWhitespaces();
            }
            // Burn bracket end, "}"
            context.Burn(1);
            // Done, return list
            return(res);
        }
예제 #7
0
        public override object Deserialize(ContextDeserialization context)
        {
            // Start building the string
            var res = new StringBuilder();
            // Read opening char, " or '
            var open = context.ReadChar();

            // Burn opening char
            context.Burn(1);
            // Read first string char
            var curr = context.ReadChar();
            // Until we find the opening char again
            var lastIsBackslash = false;

            while (curr != open || lastIsBackslash)
            {
                if (lastIsBackslash)
                {
                    if (curr == '\"')
                    {
                        res.Append('\\');
                        res.Append('\"');
                    }
                    if (curr == '\'')
                    {
                        res.Append('\\');
                        res.Append('\'');
                    }
                    if (curr == 't')
                    {
                        res.Append('\\');
                        res.Append('\t');
                    }
                    if (curr == 'r')
                    {
                        res.Append('\\');
                        res.Append('\r');
                    }
                    if (curr == 'n')
                    {
                        res.Append('\\');
                        res.Append('\n');
                    }
                    lastIsBackslash = false;
                }
                else
                {
                    if (curr == '\\')
                    {
                        lastIsBackslash = true;
                    }
                    else
                    {
                        // Append char to string
                        res.Append(curr);
                    }
                }
                // Burn read char
                context.Burn(1);
                // Read next to be tested
                curr = context.ReadChar();
            }
            // Burn closing char
            context.Burn(1);
            // Done
            return(res.ToString());
        }
예제 #8
0
 public abstract object Deserialize(ContextDeserialization context);