Example #1
0
        private object ReadArray(System.IO.TextReader reader)
        {
#pragma warning disable CS8604 // Possible null reference argument.
            IList?list = Activator.CreateInstance(DefaultArrayType) as IList;
#pragma warning restore CS8604 // Possible null reference argument.
            reader.FastSeek('[');
            reader.Read();     // consume the '['
            reader.EatWhiteSpace();
            bool done = false;
            char ch   = (char)reader.Peek();
            if (ch == ']')
            {
                done = true;
                reader.Read(); // consume the ']'
            }
            else
            {
                if (ch != 't' && ch != 'f' && ch != 'n' && Char.IsLetter(ch))
                {
                    throw new InvalidDataException($"LoadArray char {ch} is not allowed after [");
                }
            }

            while (!done)
            {
                reader.EatWhiteSpace();
                list?.Add(Read(reader));
                reader.EatWhiteSpace();
                ch = (char)reader.Peek();
                if (ch == ',')
                {
                    reader.Read(); // consume ','
                }

                reader.EatWhiteSpace();
                ch = (char)reader.Peek();
                if (ch == ']')
                {
                    reader.Read(); // consume ']'
                    done = true;
                }
            }
#pragma warning disable CS8604 // Possible null reference argument.
            return(list.Simplify());

#pragma warning restore CS8604 // Possible null reference argument.
        }
Example #2
0
        private object ReadObject(System.IO.TextReader reader)
        {
            const char objectOpenCharacter  = '{';
            const char objectCloseCharacter = '}';
            const char comma = ',';

#pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type.
            IDictionary dictionary = null;
#pragma warning restore CS8600 // Converting null literal or possible null value to non-nullable type.
            if (ObjectCount == 0)
            {
                if (DefaultDocumentType == null)
                {
                    throw new InvalidOperationException("DefaultDocumentType is null");
                }

#pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type.
                dictionary = Activator.CreateInstance(DefaultDocumentType) as IDictionary;
#pragma warning restore CS8600 // Converting null literal or possible null value to non-nullable type.
                if (dictionary == null)
                {
                    throw new InvalidOperationException($"Unable to create instance of {DefaultDocumentType.FullName}");
                }
            }
            else
            {
                if (CreateDefaultObject != null)
                {
                    dictionary = CreateDefaultObject();
                    if (dictionary == null)
                    {
                        throw new InvalidOperationException("CreateDefaultObject returned null");
                    }
                }
                else
                {
                    if (DefaultObjectType == null)
                    {
                        throw new InvalidOperationException("DefaultObjectType is null");
                    }

#pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type.
                    dictionary = Activator.CreateInstance(DefaultObjectType) as IDictionary;
#pragma warning restore CS8600 // Converting null literal or possible null value to non-nullable type.
                    if (dictionary == null)
                    {
                        throw new InvalidOperationException($"Unable to create isntance of {DefaultObjectType.FullName}");
                    }
                }
            }

            ObjectCount++;
            reader.FastSeek(objectOpenCharacter);
            reader.Read(); // consume the '{'
            reader.EatWhiteSpace();
            bool done = false;
            if ((char)(reader.Peek()) == objectCloseCharacter)// '}')
            {
                done = true;
                reader.Read(); // consume the '}'
            }
            while (!done)
            {
                reader.EatWhiteSpace();
                string?key = ReadString(reader) as string;

                /*
                 #if DEBUG
                 * if (key == "string_symbol")
                 * {
                 *  int x = 0;
                 * }
                 #endif*/
                reader.EatWhiteSpace();
                reader.Read(); //consume ':'
#pragma warning disable CS8604 // Possible null reference argument.
                dictionary[key] = Read(reader);
#pragma warning restore CS8604 // Possible null reference argument.
                reader.EatWhiteSpace();
                char ch = (char)reader.Peek();
                if (ch == comma)
                {
                    reader.Read(); // consume ','
                }

                reader.EatWhiteSpace();
                ch = (char)reader.Peek();
                if (ch == objectCloseCharacter)//'}')
                {
                    reader.Read();
                    done = true;
                }
            }

            string?type = dictionary.Get <string>("Type", "");
            if (type.Length > 0 && ConversionTypeNames.ContainsKey(type) && !ConversionTypeNames[type].IsInstanceOfType(dictionary))
            {
                if (!(Activator.CreateInstance(ConversionTypeNames[type]) is IDictionary converted))
                {
                    throw new InvalidOperationException($"Unable to create instance of {ConversionTypeNames[type].FullName}");
                }
                foreach (object?key in dictionary.Keys)
                {
#pragma warning disable CS8604 // Possible null reference argument.
                    if (!converted.Contains(key))
#pragma warning restore CS8604 // Possible null reference argument.
                    {
#pragma warning disable CS8604 // Possible null reference argument.
#pragma warning disable CS8604 // Possible null reference argument.
                        converted.Add(key, dictionary[key]);
#pragma warning restore CS8604 // Possible null reference argument.
#pragma warning restore CS8604 // Possible null reference argument.
                    }
                }
                return(converted);
            }
            return(dictionary);
        }