Example #1
0
		public static bool Deserialize(BufferedTextReader sr, int nextToken)
		{
			if (nextToken == 't')
			{
				//TODO: isNext !?
				if (sr.Read() == 'r' && sr.Read() == 'u' && sr.Read() == 'e')
					return true;
			}
			else if (nextToken == 'f')
			{
				if (sr.Read() == 'a' && sr.Read() == 'l' && sr.Read() == 's' && sr.Read() == 'e')
					return false;
			}
			else if (nextToken == '"')
			{
				nextToken = sr.Read();
				if (nextToken == 't')
				{
					if (sr.Read() == 'r' && sr.Read() == 'u' && sr.Read() == 'e' && sr.Read() == '"')
						return true;
				}
				else if (nextToken == 'f')
				{
					if (sr.Read() == 'a' && sr.Read() == 'l' && sr.Read() == 's' && sr.Read() == 'e' && sr.Read() == '"')
						return false;
				}
			}
			throw new SerializationException("Invalid value found at position " + JsonSerialization.PositionInStream(sr) + " for boolean value. Expecting true or false");
		}
Example #2
0
		public ResultObjectMapping ProcessRow(ResultObjectMapping parent, BufferedTextReader reader, object row)
		{
			var result = new ResultObjectMapping();
			Selects.ForEach(it => result.Add(it.Key, it.Value(row, reader)));
			result.Add(parent);
			return result;
		}
Example #3
0
 public ResultObjectMapping ProcessRow(IDataReader dr, BufferedTextReader reader)
 {
     var result = new ResultObjectMapping();
     foreach (var sel in Query.Selects)
         result.Add(sel.QuerySource, sel.Instancer(result, reader, dr));
     return result;
 }
Example #4
0
		public static List<bool> ParseCollection(BufferedTextReader reader, int context)
		{
			var cur = reader.Read();
			if (cur == ',' || cur == ')')
				return null;
			var espaced = cur != '{';
			if (espaced)
				reader.Read(context);
			var list = new List<bool>();
			cur = reader.Peek();
			if (cur == '}')
				reader.Read();
			while (cur != -1 && cur != '}')
			{
				cur = reader.Read();
				if (cur == 't')
					list.Add(true);
				else if (cur == 'f')
					list.Add(false);
				else
				{
					reader.Read(3);
					list.Add(false);
				}
				cur = reader.Read();
			}
			if (espaced)
				reader.Read(context + 1);
			else
				reader.Read();
			return list;
		}
Example #5
0
 public static decimal DeserializeDecimal(BufferedTextReader sr, ref int nextToken)
 {
     if (nextToken == '"')
     {
         sr.InitBuffer();
         sr.FillUntil('"');
         nextToken = sr.Read(2);
         try
         {
             return sr.BufferToValue(ConvertToDecimal);
         }
         catch (Exception ex)
         {
             throw new SerializationException("Error parsing decimal at " + JsonSerialization.PositionInStream(sr) + ". " + ex.Message, ex);
         }
     }
     var buf = sr.SmallBuffer;
     buf[0] = (char)nextToken;
     var size = sr.ReadNumber(buf, 1) + 1;
     nextToken = sr.Read();
     try
     {
         return ConvertToDecimal(buf, size, sr);
     }
     catch (Exception ex)
     {
         throw new SerializationException("Error parsing decimal at " + JsonSerialization.PositionInStream(sr) + ". " + ex.Message, ex);
     }
 }
Example #6
0
 public static DateTime DeserializeDate(BufferedTextReader sr, int nextToken)
 {
     if (nextToken != '"') throw new SerializationException("Expecting '\"' at position " + JsonSerialization.PositionInStream(sr) + ". Found " + (char)nextToken);
     nextToken = sr.Read();
     int year = 0;
     //TODO: 6!?
     for (int i = 0; i < 6 && (nextToken != '-'); i++, nextToken = sr.Read())
         year = year * 10 + (nextToken - '0');
     nextToken = sr.Read();
     int month = nextToken - 48;
     nextToken = sr.Read();
     if (nextToken != '-')
     {
         month = month * 10 + (nextToken - '0');
         if ((nextToken = sr.Read()) != '-') throw new SerializationException("Expecting '-' at position " + JsonSerialization.PositionInStream(sr) + ". Found " + (char)nextToken);
     }
     nextToken = sr.Read();
     int day = nextToken - 48;
     nextToken = sr.Read();
     if (nextToken != '"' && nextToken != ' ' && nextToken != 'T')
     {
         day = day * 10 + (nextToken - '0');
         nextToken = sr.Read();
     }
     for (int i = 0; i < 24 && nextToken != '"'; i++)
         nextToken = sr.Read();
     if (nextToken != '"') throw new SerializationException("Expecting '\"' at position " + JsonSerialization.PositionInStream(sr) + ". Found " + (char)nextToken);
     return new DateTime(year, month, day);
 }
Example #7
0
		public static List<long> ParseCollection(BufferedTextReader reader, int context)
		{
			var cur = reader.Read();
			if (cur == ',' || cur == ')')
				return null;
			var espaced = cur != '{';
			if (espaced)
				reader.Read(context);
			var list = new List<long>();
			cur = reader.Peek();
			if (cur == '}')
				reader.Read();
			while (cur != -1 && cur != '}')
			{
				cur = reader.Read();
				if (cur == 'N')
				{
					cur = reader.Read(4);
					list.Add(0);
				}
				else
				{
					list.Add(ParseLong(reader, ref cur));
				}
			}
			if (espaced)
				reader.Read(context + 1);
			else
				reader.Read();
			return list;
		}
Example #8
0
 public static Dictionary<string, string> Parse(BufferedTextReader reader, int context)
 {
     var cur = reader.Read();
     if (cur == ',' || cur == ')')
         return null;
     return ParseDictionary(reader, context, context > 0 ? context << 1 : 1, ref cur, ')');
 }
Example #9
0
		public static float Parse(BufferedTextReader reader)
		{
			var cur = reader.Read();
			if (cur == ',' || cur == ')')
				return 0;
			return ParseFloat(reader, ref  cur, ')');
		}
Example #10
0
		public static void Skip(BufferedTextReader reader, int context)
		{
			var cur = reader.Read();
			if (cur == ',' || cur == ')')
				return;
			if (cur != '"' && cur != '\\')
			{
				reader.InitBuffer();
				reader.FillUntil(',', ')');
				reader.Read();
			}
			else
			{
				cur = reader.Read(context);
				while (cur != -1)
				{
					if (cur == '\\' || cur == '"')
					{
						cur = reader.Read(context);
						if (cur == ',' || cur == ')')
							return;
						cur = reader.Read(context);
					}
					else cur = reader.Read();
				}
				throw new FrameworkException("Unable to find end of string");
			}
		}
Example #11
0
		public static Dictionary<string, string> Deserialize(BufferedTextReader sr, int nextToken)
		{
			if (nextToken != '{') throw new SerializationException("Expecting '{' at position " + JsonSerialization.PositionInStream(sr) + ". Found " + (char)nextToken);
			var res = new Dictionary<string, string>();
			nextToken = JsonSerialization.GetNextToken(sr);
			if (nextToken == '}') return res;
			var key = StringConverter.Deserialize(sr, nextToken);
			nextToken = JsonSerialization.GetNextToken(sr);
			if (nextToken != ':') throw new SerializationException("Expecting ':' at position " + JsonSerialization.PositionInStream(sr) + ". Found " + (char)nextToken);
			nextToken = JsonSerialization.GetNextToken(sr);
			var value = StringConverter.DeserializeNullable(sr, nextToken);
			res.Add(key, value);
			while ((nextToken = JsonSerialization.GetNextToken(sr)) == ',')
			{
				nextToken = JsonSerialization.GetNextToken(sr);
				key = StringConverter.Deserialize(sr, nextToken);
				nextToken = JsonSerialization.GetNextToken(sr);
				if (nextToken != ':') throw new SerializationException("Expecting ':' at position " + JsonSerialization.PositionInStream(sr) + ". Found " + (char)nextToken);
				nextToken = JsonSerialization.GetNextToken(sr);
				value = StringConverter.DeserializeNullable(sr, nextToken);
				res.Add(key, value);
			}
			if (nextToken != '}') throw new SerializationException("Expecting '}' at position " + JsonSerialization.PositionInStream(sr) + ". Found " + (char)nextToken);
			return res;
		}
Example #12
0
		public static int? ParseNullable(BufferedTextReader reader)
		{
			var cur = reader.Read();
			if (cur == ',' || cur == ')')
				return null;
			return ParseInt(reader, ref cur, ')');
		}
Example #13
0
		public static List<DateTime?> ParseNullableCollection(BufferedTextReader reader, int context)
		{
			var cur = reader.Read();
			if (cur == ',' || cur == ')')
				return null;
			var escaped = cur == '"' || cur == '\\';
			if (escaped)
				reader.Read(context);
			var list = new List<DateTime?>();
			cur = reader.Peek();
			if (cur == '}')
				reader.Read();
			while (cur != -1 && cur != '}')
			{
				cur = reader.Read();
				if (cur == 'N')
				{
					cur = reader.Read(4);
					list.Add(null);
				}
				else
				{
					list.Add(ParseDate(reader, cur));
					cur = reader.Read();
				}
			}
			if (escaped)
				reader.Read(context + 1);
			else
				reader.Read();
			return list;
		}
Example #14
0
		public static Image Parse(BufferedTextReader reader, int context)
		{
			var stream = ByteaConverter.ParseStream(reader, context);
			if (stream == null)
				return null;
			return Image.FromStream(stream);
		}
Example #15
0
		public static long? ParseNullable(BufferedTextReader reader)
		{
			var cur = reader.Read();
			if (cur == ',' || cur == ')')
				return null;
			return ParseLong(reader, ref cur);
		}
Example #16
0
		public static double Parse(BufferedTextReader reader)
		{
			var cur = reader.Read();
			if (cur == ',' || cur == ')')
				return 0;
			return ParseDouble(reader, ref cur, ')');
		}
Example #17
0
 public static List<Dictionary<string, string>> ParseCollection(BufferedTextReader reader, int context, bool allowNulls)
 {
     var cur = reader.Read();
     if (cur == ',' || cur == ')')
         return null;
     var espaced = cur != '{';
     if (espaced)
         reader.Read(context);
     var innerContext = context << 1;
     var list = new List<Dictionary<string, string>>();
     cur = reader.Peek();
     if (cur == '}')
         reader.Read();
     while (cur != -1 && cur != '}')
     {
         cur = reader.Read();
         if (cur == 'N')
         {
             cur = reader.Read(4);
             list.Add(allowNulls ? null : new Dictionary<string, string>());
         }
         else
         {
             list.Add(ParseDictionary(reader, innerContext, innerContext << 1, ref cur, '}'));
         }
     }
     if (espaced)
         reader.Read(context + 1);
     else
         reader.Read();
     return list;
 }
Example #18
0
 public static void DeserializeNullableCollection(BufferedTextReader sr, int nextToken, ICollection<TreePath?> res)
 {
     if (nextToken == 'n')
     {
         if (sr.Read() == 'u' && sr.Read() == 'l' && sr.Read() == 'l')
             res.Add(null);
         else throw new SerializationException("Invalid value found at position " + JsonSerialization.PositionInStream(sr) + " for string value. Expecting '\"' or null");
     }
     else res.Add(Deserialize(sr, nextToken));
     while ((nextToken = JsonSerialization.GetNextToken(sr)) == ',')
     {
         nextToken = JsonSerialization.GetNextToken(sr);
         if (nextToken == 'n')
         {
             if (sr.Read() == 'u' && sr.Read() == 'l' && sr.Read() == 'l')
                 res.Add(null);
             else throw new SerializationException("Invalid value found at position " + JsonSerialization.PositionInStream(sr) + " for string value. Expecting '\"' or null");
         }
         else res.Add(Deserialize(sr, nextToken));
     }
     if (nextToken != ']')
     {
         if (nextToken == -1) throw new SerializationException("Unexpected end of json in collection.");
         else throw new SerializationException("Expecting ']' at position " + JsonSerialization.PositionInStream(sr) + ". Found " + (char)nextToken);
     }
 }
Example #19
0
		public static List<decimal?> ParseNullableCollection(BufferedTextReader reader, int context)
		{
			var cur = reader.Read();
			if (cur == ',' || cur == ')')
				return null;
			var espaced = cur != '{';
			if (espaced)
				reader.Read(context);
			cur = reader.Peek();
			if (cur == '}')
			{
				if (espaced)
					reader.Read(context + 2);
				else
					reader.Read(2);
				return new List<decimal?>(0);
			}
			var list = new List<decimal?>();
			do
			{
				cur = reader.Read();
				if (cur == 'N')
				{
					list.Add(null);
					cur = reader.Read(4);
				}
				else list.Add(ParseDecimal(reader, ref cur, '}'));
			} while (cur == ',');
			if (espaced)
				reader.Read(context + 1);
			else
				reader.Read();
			return list;
		}
Example #20
0
		private static decimal ParseDecimal(BufferedTextReader reader, ref int cur, char matchEnd)
		{
			var neg = cur == '-';
			if (neg)
				cur = reader.Read();
			var buf = reader.SmallBuffer;
			buf[0] = (char)cur;
			var size = reader.ReadUntil(buf, 1, ',', matchEnd) + 1;
			cur = reader.Read();
			if (cur >= '0' && cur <= '9' || cur == '.')
				throw new FrameworkException("Too long decimal number: " + new string(buf, 0, size));
			if (size > 18)
			{
				if (neg)
					return -decimal.Parse(new string(buf, 0, size), Invariant);
				return decimal.Parse(new string(buf, 0, size), Invariant);
			}
			long value = 0;
			int scale = 0;
			char ch;
			for (int i = 0; i < size && i < buf.Length; i++)
			{
				ch = buf[i];
				if (ch == '.')
					scale = size - i - 1;
				else
					value = (value << 3) + (value << 1) + ch - 48;
			}
			return new decimal((int)value, (int)(value >> 32), 0, neg, (byte)scale);
		}
Example #21
0
		private static double ParseDouble(BufferedTextReader reader, ref int cur, char matchEnd)
		{
			reader.InitBuffer((char)cur);
			reader.FillUntil(',', matchEnd);
			cur = reader.Read();
			//TODO: optimize
			return double.Parse(reader.BufferToString(), NumberStyles.Float, CultureInfo.InvariantCulture);
		}
Example #22
0
		public static bool? ParseNullable(BufferedTextReader reader)
		{
			var cur = reader.Read();
			if (cur == ',' || cur == ')')
				return null;
			reader.Read();
			return cur == 't';
		}
Example #23
0
		public static XElement Parse(BufferedTextReader reader, int context)
		{
			var stream = StringConverter.ParseStream(reader, context);
			if (stream == null)
				return null;
			try { return XElement.Load(stream); }
			finally { stream.Dispose(); }
		}
Example #24
0
 public static TreePath Deserialize(BufferedTextReader sr, int nextToken)
 {
     if (nextToken != '"') throw new SerializationException("Expecting '\"' at position " + JsonSerialization.PositionInStream(sr) + ". Found " + (char)nextToken);
     sr.InitBuffer();
     nextToken = sr.FillUntil('"');
     sr.Read();
     return TreePath.Create(sr.BufferToString());
 }
Example #25
0
		public static bool Parse(BufferedTextReader reader)
		{
			var cur = reader.Read();
			if (cur == ',' || cur == ')')
				return false;
			reader.Read();
			return cur == 't';
		}
Example #26
0
		public static DateTime Parse(BufferedTextReader reader)
		{
			var cur = reader.Read();
			if (cur == ',' || cur == ')')
				return DateTime.MinValue;
			var res = ParseDate(reader, cur);
			reader.Read();
			return res;
		}
Example #27
0
		public static DateTime? ParseNullable(BufferedTextReader reader)
		{
			var cur = reader.Read();
			if (cur == ',' || cur == ')')
				return null;
			var res = ParseDate(reader, cur);
			reader.Read();
			return res;
		}
Example #28
0
		public static DateTime? ParseNullable(BufferedTextReader reader, int context)
		{
			var cur = reader.Read();
			if (cur == ',' || cur == ')')
				return null;
			var dt = ParseTimestampUtc(reader, context);
			reader.Read();
			return dt.ToLocalTime();
		}
Example #29
0
		public static S3 Parse(BufferedTextReader reader, int context)
		{
			var cur = reader.Read();
			if (cur == ',' || cur == ')')
				return null;
			var s3 = ParseS3(reader, context, context == 0 ? 1 : context << 1, null);
			reader.Read();
			return s3;
		}
 public static DateTime Parse(BufferedTextReader reader, int context)
 {
     var cur = reader.Read();
     if (cur == ',' || cur == ')')
         return DateTime.MinValue;
     var dt = ParseTimestampUtc(reader, context);
     reader.Read();
     return dt.ToLocalTime();
 }
Example #31
0
        public static DateTime Parse(BufferedTextReader reader, int context)
        {
            var cur = reader.Read();

            if (cur == ',' || cur == ')')
            {
                return(DateTime.MinValue);
            }
            var dt = ParseTimestampUtc(reader, context);

            reader.Read();
            return(dt.ToLocalTime());
        }
Example #32
0
        public static DateTime Parse(BufferedTextReader reader)
        {
            var cur = reader.Read();

            if (cur == ',' || cur == ')')
            {
                return(DateTime.MinValue);
            }
            var res = ParseDate(reader, cur);

            reader.Read();
            return(res);
        }
Example #33
0
        public static DateTime?ParseNullableUtc(BufferedTextReader reader, int context)
        {
            var cur = reader.Read();

            if (cur == ',' || cur == ')')
            {
                return(null);
            }
            var dt = ParseTimestampUtc(reader, context);

            reader.Read();
            return(dt);
        }
Example #34
0
        public static DateTime?ParseNullable(BufferedTextReader reader)
        {
            var cur = reader.Read();

            if (cur == ',' || cur == ')')
            {
                return(null);
            }
            var res = ParseDate(reader, cur);

            reader.Read();
            return(res);
        }
Example #35
0
 public static Color DeserializeColor(BufferedTextReader sr, ref int nextToken)
 {
     if (nextToken == '"')
     {
         var val = StringConverter.Deserialize(sr, nextToken);
         nextToken = JsonSerialization.GetNextToken(sr);
         return(Color.FromName(val));
     }
     else
     {
         var val = NumberConverter.DeserializeInt(sr, ref nextToken);
         return(Color.FromArgb(val));
     }
 }
Example #36
0
 public static void DeserializeColorNullableCollection(BufferedTextReader sr, int nextToken, ICollection <Color?> res)
 {
     if (nextToken == 'n')
     {
         if (sr.Read() == 'u' && sr.Read() == 'l' && sr.Read() == 'l')
         {
             res.Add(null);
         }
         else
         {
             throw new SerializationException("Invalid value found at position " + JsonSerialization.PositionInStream(sr) + " for Color value. Expecting number, string or null");
         }
         nextToken = sr.Read();
     }
     else
     {
         res.Add(DeserializeColor(sr, ref nextToken));
     }
     while ((nextToken = JsonSerialization.MoveToNextToken(sr, nextToken)) == ',')
     {
         nextToken = JsonSerialization.GetNextToken(sr);
         if (nextToken == 'n')
         {
             if (sr.Read() == 'u' && sr.Read() == 'l' && sr.Read() == 'l')
             {
                 res.Add(null);
             }
             else
             {
                 throw new SerializationException("Invalid value found at position " + JsonSerialization.PositionInStream(sr) + " for Color value. Expecting number, string or null");
             }
             nextToken = sr.Read();
         }
         else
         {
             res.Add(DeserializeColor(sr, ref nextToken));
         }
     }
     if (nextToken != ']')
     {
         if (nextToken == -1)
         {
             throw new SerializationException("Unexpected end of json in collection.");
         }
         else
         {
             throw new SerializationException("Expecting ']' at position " + JsonSerialization.PositionInStream(sr) + ". Found " + (char)nextToken);
         }
     }
 }
Example #37
0
        public static List <bool> ParseCollection(BufferedTextReader reader, int context)
        {
            var cur = reader.Read();

            if (cur == ',' || cur == ')')
            {
                return(null);
            }
            var espaced = cur != '{';

            if (espaced)
            {
                reader.Read(context);
            }
            var list = new List <bool>();

            cur = reader.Peek();
            if (cur == '}')
            {
                reader.Read();
            }
            while (cur != -1 && cur != '}')
            {
                cur = reader.Read();
                if (cur == 't')
                {
                    list.Add(true);
                }
                else if (cur == 'f')
                {
                    list.Add(false);
                }
                else
                {
                    reader.Read(3);
                    list.Add(false);
                }
                cur = reader.Read();
            }
            if (espaced)
            {
                reader.Read(context + 1);
            }
            else
            {
                reader.Read();
            }
            return(list);
        }
Example #38
0
 public static void DeserializeNullableStructCollection <T>(BufferedTextReader sr, int nextToken, Func <int, T> factory, ICollection <T?> res)
     where T : struct
 {
     if (nextToken == 'n')
     {
         if (sr.Read() == 'u' && sr.Read() == 'l' && sr.Read() == 'l')
         {
             res.Add(null);
         }
         else
         {
             throw new SerializationException("Invalid value found at position " + PositionInStream(sr) + " for string value. Expecting '\"' or null");
         }
     }
     else
     {
         res.Add(factory(nextToken));
     }
     while ((nextToken = GetNextToken(sr)) == ',')
     {
         nextToken = GetNextToken(sr);
         if (nextToken == 'n')
         {
             if (sr.Read() == 'u' && sr.Read() == 'l' && sr.Read() == 'l')
             {
                 res.Add(null);
             }
             else
             {
                 throw new SerializationException("Invalid value found at position " + PositionInStream(sr) + " for string value. Expecting '\"' or null");
             }
         }
         else
         {
             res.Add(factory(nextToken));
         }
     }
     if (nextToken != ']')
     {
         if (nextToken == -1)
         {
             throw new SerializationException("Unexpected end of json in collection.");
         }
         else
         {
             throw new SerializationException("Expecting ']' at position " + PositionInStream(sr) + ". Found " + (char)nextToken);
         }
     }
 }
Example #39
0
        public static string Deserialize(BufferedTextReader sr, int nextToken)
        {
            if (nextToken != '"')
            {
                throw new SerializationException("Expecting '\"' at position " + JsonSerialization.PositionInStream(sr) + ". Found " + (char)nextToken);
            }
            sr.InitBuffer();
            nextToken = sr.FillUntil('"', '\\');
            if (nextToken == '"')
            {
                sr.Read();
                return(sr.BufferToString());
            }
            var tmp = sr.SmallBuffer;

            do
            {
                nextToken = sr.Read(2);
                switch (nextToken)
                {
                case 'b': sr.AddToBuffer('\b'); break;

                case 't': sr.AddToBuffer('\t'); break;

                case 'r': sr.AddToBuffer('\r'); break;

                case 'n': sr.AddToBuffer('\n'); break;

                case 'f': sr.AddToBuffer('\f'); break;

                case 'u':
                    var len = sr.Read(tmp, 0, 4);
                    if (len < 4)
                    {
                        sr.Read(tmp, len, 4 - len);                                     //the second one must succeed
                    }
                    sr.AddToBuffer((char)Convert.ToInt32(new string(tmp, 0, 4), 16));   //TODO: optimize to no allocation
                    break;

                default:
                    sr.AddToBuffer((char)nextToken);
                    break;
                }
                nextToken = sr.FillUntil('"', '\\');
            } while (nextToken == '\\');
            sr.Read();
            return(sr.BufferToString());
        }
Example #40
0
        private static decimal ConvertToDecimal(char[] buf, int len, BufferedTextReader sr)
        {
            if (len > 18)
            {
                return(ConvertToDecimalGeneric(buf, len));
            }
            var  ch  = buf[0];
            int  i   = 0;
            bool neg = false;

            switch (ch)
            {
            case '+':
                i = 1;
                break;

            case '-':
                i   = 1;
                neg = true;
                break;
            }
            long value = 0;
            int  scale = 0;
            int  num;

            for (; i < len; i++)
            {
                ch = buf[i];
                if (ch == '.')
                {
                    if (scale != 0)
                    {
                        throw new SerializationException("Multiple '.' found in decimal value: " + new string(buf, 0, len) + ". At position" + JsonSerialization.PositionInStream(sr));
                    }
                    scale = len - i - 1;
                }
                else
                {
                    num = ch - 48;
                    if (num < 0 || num > 9)
                    {
                        return(ConvertToDecimalGeneric(buf, len));
                    }
                    value = (value << 3) + (value << 1) + num;
                }
            }
            return(new decimal((int)value, (int)(value >> 32), 0, neg, (byte)scale));
        }
Example #41
0
 public static void DeserializeOffsetNullableCollection(BufferedTextReader sr, int nextToken, ICollection <DateTimeOffset?> res)
 {
     if (nextToken == 'n')
     {
         if (sr.Read() == 'u' && sr.Read() == 'l' && sr.Read() == 'l')
         {
             res.Add(null);
         }
         else
         {
             throw new SerializationException("Invalid value found at position " + JsonSerialization.PositionInStream(sr) + " for DateTime value. Expecting timestamp or null");
         }
     }
     else
     {
         res.Add(DeserializeOffset(sr, nextToken));
     }
     while ((nextToken = JsonSerialization.GetNextToken(sr)) == ',')
     {
         nextToken = JsonSerialization.GetNextToken(sr);
         if (nextToken == 'n')
         {
             if (sr.Read() == 'u' && sr.Read() == 'l' && sr.Read() == 'l')
             {
                 res.Add(null);
             }
             else
             {
                 throw new SerializationException("Invalid value found at position " + JsonSerialization.PositionInStream(sr) + " for DateTime value. Expecting timestamp or null");
             }
         }
         else
         {
             res.Add(DeserializeOffset(sr, nextToken));
         }
     }
     if (nextToken != ']')
     {
         if (nextToken == -1)
         {
             throw new SerializationException("Unexpected end of json in collection.");
         }
         else
         {
             throw new SerializationException("Expecting ']' at position " + JsonSerialization.PositionInStream(sr) + ". Found " + (char)nextToken);
         }
     }
 }
Example #42
0
        public static List <Image> ParseCollection(BufferedTextReader reader, int context)
        {
            var list = ByteaConverter.ParseStreamCollection(reader, context, true);

            if (list == null)
            {
                return(null);
            }
            var result = new List <Image>(list.Count);

            foreach (var stream in list)
            {
                result.Add(stream != null ? Image.FromStream(stream) : null);
            }
            return(result);
        }
Example #43
0
        public static string Parse(BufferedTextReader reader, int context)
        {
            var cur = reader.Read();

            if (cur == ',' || cur == ')')
            {
                return(null);
            }
            if (cur != '"' && cur != '\\')
            {
                reader.InitBuffer((char)cur);
                reader.FillUntil(',', ')');
                reader.Read();
                return(reader.BufferToString());
            }
            return(ParseEscapedString(reader, context, ref cur, ')'));
        }
Example #44
0
        public static List <DateTime?> ParseNullableCollection(BufferedTextReader reader, int context)
        {
            var cur = reader.Read();

            if (cur == ',' || cur == ')')
            {
                return(null);
            }
            var escaped = cur == '"' || cur == '\\';

            if (escaped)
            {
                reader.Read(context);
            }
            var list = new List <DateTime?>();

            cur = reader.Peek();
            if (cur == '}')
            {
                reader.Read();
            }
            while (cur != -1 && cur != '}')
            {
                cur = reader.Read();
                if (cur == 'N')
                {
                    cur = reader.Read(4);
                    list.Add(null);
                }
                else
                {
                    list.Add(ParseDate(reader, cur));
                    cur = reader.Read();
                }
            }
            if (escaped)
            {
                reader.Read(context + 1);
            }
            else
            {
                reader.Read();
            }
            return(list);
        }
Example #45
0
        public static DateTime DeserializeDate(BufferedTextReader sr, int nextToken)
        {
            if (nextToken != '"')
            {
                throw new SerializationException("Expecting '\"' at position " + JsonSerialization.PositionInStream(sr) + ". Found " + (char)nextToken);
            }
            nextToken = sr.Read();
            int year = 0;

            //TODO: 6!?
            for (int i = 0; i < 6 && (nextToken != '-'); i++, nextToken = sr.Read())
            {
                year = year * 10 + (nextToken - '0');
            }
            nextToken = sr.Read();
            int month = nextToken - 48;

            nextToken = sr.Read();
            if (nextToken != '-')
            {
                month = month * 10 + (nextToken - '0');
                if ((nextToken = sr.Read()) != '-')
                {
                    throw new SerializationException("Expecting '-' at position " + JsonSerialization.PositionInStream(sr) + ". Found " + (char)nextToken);
                }
            }
            nextToken = sr.Read();
            int day = nextToken - 48;

            nextToken = sr.Read();
            if (nextToken != '"' && nextToken != ' ' && nextToken != 'T')
            {
                day       = day * 10 + (nextToken - '0');
                nextToken = sr.Read();
            }
            for (int i = 0; i < 24 && nextToken != '"'; i++)
            {
                nextToken = sr.Read();
            }
            if (nextToken != '"')
            {
                throw new SerializationException("Expecting '\"' at position " + JsonSerialization.PositionInStream(sr) + ". Found " + (char)nextToken);
            }
            return(new DateTime(year, month, day));
        }
Example #46
0
        public static List <Dictionary <string, string> > ParseCollection(BufferedTextReader reader, int context, bool allowNulls)
        {
            var cur = reader.Read();

            if (cur == ',' || cur == ')')
            {
                return(null);
            }
            var escaped = cur != '{';

            if (escaped)
            {
                reader.Read(context);
            }
            int innerContext = context == 0 ? 1 : context << 1;
            var list         = new List <Dictionary <string, string> >();

            cur = reader.Peek();
            if (cur == '}')
            {
                reader.Read();
            }
            while (cur != -1 && cur != '}')
            {
                cur = reader.Read();
                if (cur == 'N')
                {
                    cur = reader.Read(4);
                    list.Add(allowNulls ? null : new Dictionary <string, string>());
                }
                else
                {
                    list.Add(ParseDictionary(reader, innerContext, innerContext << 1, ref cur, '}'));
                }
            }
            if (escaped)
            {
                reader.Read(context + 1);
            }
            else
            {
                reader.Read();
            }
            return(list);
        }
Example #47
0
 public static XElement Deserialize(BufferedTextReader sr, ref int nextToken)
 {
     if (nextToken == '"')
     {
         var value = StringConverter.Deserialize(sr, nextToken);
         nextToken = sr.Read();
         try
         {
             return(XElement.Parse(value));
         }
         catch (Exception ex)
         {
             throw new SerializationException("Error parsing XML at " + JsonSerialization.PositionInStream(sr) + ". " + ex.Message, ex);
         }
     }
     using (var cms = JsonSerialization.Memorize(sr, ref nextToken))
         return((XElement)JsonNet.Deserialize(cms.GetReader(), typeof(XElement)));
 }
Example #48
0
        private static int MemoizeSkipString(BufferedTextReader sr, TextWriter sw)
        {
            var c = sr.Read();

            sw.Write((char)c);
            while (c != '"' && c != -1)
            {
                c = sr.Read();
                sw.Write((char)c);
                if (c == '\\')
                {
                    c = sr.Read();
                    sw.Write((char)c);
                    c = (char)0;
                }
            }
            return(MemoizeGetNextToken(sr, sw));
        }
Example #49
0
        public static SecureString Deserialize(BufferedTextReader sr, int nextToken)
        {
            var ss    = new SecureString();
            var bytes = BinaryConverter.Deserialize(sr, nextToken);

            if (bytes == null)
            {
                return(ss);
            }
            //TODO use tmp buffer
            var utf8string = Encoding.UTF8.GetString(RsaProvider.Decrypt(bytes, false));

            foreach (var c in utf8string)
            {
                ss.AppendChar(c);
            }
            return(ss);
        }
Example #50
0
        public static SecureString ParseNullable(BufferedTextReader reader, int context)
        {
            var bytes = ByteaConverter.Parse(reader, context);

            if (bytes == null)
            {
                return(null);
            }
            var ss      = new SecureString();
            var decrypt = RsaProvider.Decrypt(bytes, false);
            var chars   = Encoding.Unicode.GetChars(decrypt);

            for (int i = 0; i < chars.Length; i++)
            {
                ss.AppendChar(chars[i]);
            }
            return(ss);
        }
Example #51
0
        public static Stream ParseStream(BufferedTextReader reader, int context)
        {
            var cur = reader.Read();

            if (cur == ',' || cur == ')')
            {
                return(null);
            }
            var cms = ChunkedMemoryStream.Create();
            var sw  = cms.GetWriter();

            if (cur != '"' && cur != '\\')
            {
                sw.Write((char)cur);
                reader.FillUntil(sw, ',', ')');
            }
            else
            {
                cur = reader.Read(context);
                while (cur != -1)
                {
                    if (cur == '\\' || cur == '"')
                    {
                        cur = reader.Read(context);
                        if (cur == ',' || cur == ')')
                        {
                            sw.Flush();
                            cms.Position = 0;
                            return(cms);
                        }
                        for (int i = 0; i < context - 1; i++)
                        {
                            cur = reader.Read();
                        }
                    }
                    sw.Write((char)cur);
                    cur = reader.Read();
                }
                reader.Read(context);
            }
            sw.Flush();
            cms.Position = 0;
            return(cms);
        }
Example #52
0
        private static decimal ParseDecimal(BufferedTextReader reader, ref int cur, char matchEnd)
        {
            var neg = cur == '-';

            if (neg)
            {
                cur = reader.Read();
            }
            var buf = reader.SmallBuffer;

            buf[0] = (char)cur;
            var size = reader.ReadUntil(buf, 1, ',', matchEnd) + 1;

            cur = reader.Read();
            if (cur >= '0' && cur <= '9' || cur == '.')
            {
                throw new FrameworkException("Too long decimal number: " + new string(buf, 0, size));
            }
            if (size > 18)
            {
                if (neg)
                {
                    return(-decimal.Parse(new string(buf, 0, size), Invariant));
                }
                return(decimal.Parse(new string(buf, 0, size), Invariant));
            }
            long value = 0;
            int  scale = 0;
            char ch;

            for (int i = 0; i < size && i < buf.Length; i++)
            {
                ch = buf[i];
                if (ch == '.')
                {
                    scale = size - i - 1;
                }
                else
                {
                    value = (value << 3) + (value << 1) + ch - 48;
                }
            }
            return(new decimal((int)value, (int)(value >> 32), 0, neg, (byte)scale));
        }
Example #53
0
        public static List <short> ParseCollection(BufferedTextReader reader, int context)
        {
            var cur = reader.Read();

            if (cur == ',' || cur == ')')
            {
                return(null);
            }
            var espaced = cur != '{';

            if (espaced)
            {
                reader.Read(context);
            }
            var list = new List <short>();

            cur = reader.Peek();
            if (cur == '}')
            {
                reader.Read();
            }
            while (cur != -1 && cur != '}')
            {
                cur = reader.Read();
                if (cur == 'N')
                {
                    cur = reader.Read(4);
                    list.Add(0);
                }
                else
                {
                    list.Add(ParseShort(reader, ref cur, '}'));
                }
            }
            if (espaced)
            {
                reader.Read(context + 1);
            }
            else
            {
                reader.Read();
            }
            return(list);
        }
Example #54
0
        public static Dictionary <string, string> Deserialize(BufferedTextReader sr, int nextToken)
        {
            if (nextToken != '{')
            {
                throw new SerializationException("Expecting '{' at position " + JsonSerialization.PositionInStream(sr) + ". Found " + (char)nextToken);
            }
            var res = new Dictionary <string, string>();

            nextToken = JsonSerialization.GetNextToken(sr);
            if (nextToken == '}')
            {
                return(res);
            }
            var key = StringConverter.Deserialize(sr, nextToken);

            nextToken = JsonSerialization.GetNextToken(sr);
            if (nextToken != ':')
            {
                throw new SerializationException("Expecting ':' at position " + JsonSerialization.PositionInStream(sr) + ". Found " + (char)nextToken);
            }
            nextToken = JsonSerialization.GetNextToken(sr);
            var value = StringConverter.DeserializeNullable(sr, nextToken);

            res.Add(key, value);
            while ((nextToken = JsonSerialization.GetNextToken(sr)) == ',')
            {
                nextToken = JsonSerialization.GetNextToken(sr);
                key       = StringConverter.Deserialize(sr, nextToken);
                nextToken = JsonSerialization.GetNextToken(sr);
                if (nextToken != ':')
                {
                    throw new SerializationException("Expecting ':' at position " + JsonSerialization.PositionInStream(sr) + ". Found " + (char)nextToken);
                }
                nextToken = JsonSerialization.GetNextToken(sr);
                value     = StringConverter.DeserializeNullable(sr, nextToken);
                res.Add(key, value);
            }
            if (nextToken != '}')
            {
                throw new SerializationException("Expecting '}' at position " + JsonSerialization.PositionInStream(sr) + ". Found " + (char)nextToken);
            }
            return(res);
        }
Example #55
0
 public static void DeserializeCollection(BufferedTextReader sr, int nextToken, ICollection <TreePath> res)
 {
     res.Add(Deserialize(sr, nextToken));
     while ((nextToken = JsonSerialization.GetNextToken(sr)) == ',')
     {
         nextToken = JsonSerialization.GetNextToken(sr);
         res.Add(Deserialize(sr, nextToken));
     }
     if (nextToken != ']')
     {
         if (nextToken == -1)
         {
             throw new SerializationException("Unexpected end of json in collection.");
         }
         else
         {
             throw new SerializationException("Expecting ']' at position " + JsonSerialization.PositionInStream(sr) + ". Found " + (char)nextToken);
         }
     }
 }
Example #56
0
        public static Uri DeserializeUri(BufferedTextReader sr, int nextToken)
        {
            if (nextToken != '"')
            {
                throw new SerializationException("Expecting '\"' at position " + JsonSerialization.PositionInStream(sr) + ". Found " + (char)nextToken);
            }
            nextToken = sr.Read();
            var buffer = sr.CharBuffer;
            int i      = 0;

            for (; nextToken != '"' && i < buffer.Length; i++, nextToken = sr.Read())
            {
                buffer[i] = (char)nextToken;
            }
            if (nextToken != '"')
            {
                throw new SerializationException("Invalid value found at position " + JsonSerialization.PositionInStream(sr) + " for Uri value. Expecting \"");
            }
            return(new Uri(new string(buffer, 0, i)));
        }
Example #57
0
        public static Stream DeserializeStream(BufferedTextReader sr, int nextToken)
        {
            if (nextToken != '"')
            {
                throw new SerializationException("Expecting '\"' at position " + JsonSerialization.PositionInStream(sr) + ". Found " + (char)nextToken);
            }
            nextToken = sr.Read();
            if (nextToken == '"')
            {
                return(new MemoryStream());
            }
            //TODO: lazy init stream (more lightweight!?)
            var res    = new ChunkedMemoryStream();
            var base64 = sr.LargeTempBuffer;
            int i      = 1;

            base64[0] = (char)nextToken;
            int len;

            while ((len = sr.ReadUntil(base64, i, '"')) > 0)
            {
                i += len;
                if (i == base64.Length)
                {
                    var bytes = Convert.FromBase64CharArray(base64, 0, base64.Length);
                    res.Write(bytes, 0, bytes.Length);
                    i = 0;
                }
            }
            nextToken = sr.Read();
            if (i > 0)
            {
                var bytes = Convert.FromBase64CharArray(base64, 0, i);
                res.Write(bytes, 0, bytes.Length);
            }
            if (nextToken != '"')
            {
                throw new SerializationException("Expecting '\"' at position " + JsonSerialization.PositionInStream(sr) + ". Found end of stream.");
            }
            return(res);
        }
Example #58
0
        private static S3 ParseS3(BufferedTextReader reader, int context, int innerContext, IServiceProvider locator)
        {
            for (int i = 0; i < context; i++)
            {
                reader.Read();
            }
            var bucket   = StringConverter.Parse(reader, innerContext);
            var key      = StringConverter.Parse(reader, innerContext);
            var length   = LongConverter.Parse(reader);
            var name     = StringConverter.Parse(reader, innerContext);
            var mimeType = StringConverter.Parse(reader, innerContext);
            var metadata = HstoreConverter.Parse(reader, innerContext);

            for (int i = 0; i < context; i++)
            {
                reader.Read();
            }
            return(new S3 {
                Bucket = bucket, Key = key, Length = length, Name = name, MimeType = mimeType, Metadata = metadata
            });
        }
Example #59
0
        /// <summary>
        ///
        /// </summary>
        /// <remarks>
        /// This function is called by the infrastructure when starting to read
        /// the file by the FileDataReader
        /// </remarks>
        protected override void OpenForRead()
        {
            if (inputReader == null)
            {
                base.OpenForRead();

                // Open text reader
                // Wrap underlying stream, so it doesn't get disposed automatically
                if (base.Encoding == null)
                {
                    inputReader = new StreamReader(new DetachedStream(base.BaseStream));
                }
                else
                {
                    inputReader = new StreamReader(new DetachedStream(base.BaseStream), base.Encoding);
                }

                this.ownsInputReader = true;
            }

            this.bufferedReader = new BufferedTextReader(inputReader);
        }
Example #60
0
        private static DateTime ParseDate(BufferedTextReader reader, int cur)
        {
            //TODO: BC after date for year < 0 ... not supported by .NET
            if (cur == '\\' || cur == '"')
            {
                throw new NotSupportedException("Negative dates are not supported by .NET.");
            }
            var buf = reader.SmallBuffer;

            buf[0] = (char)cur;
            var read = reader.Read(buf, 1, 9);

            for (int i = read + 1; i < 10; i++)
            {
                buf[i] = (char)reader.Read();
            }
            if (buf[4] != '-')
            {
                return(ParseDateSlow(buf, reader));
            }
            return(new DateTime(NumberConverter.Read4(buf, 0), NumberConverter.Read2(buf, 5), NumberConverter.Read2(buf, 8)));
        }