Exemplo n.º 1
0
 public KVStringValue(string value)
 {
     Require.NotNull(value, nameof(value));
     this.value = value;
 }
Exemplo n.º 2
0
        public KVObject ReadObject()
        {
            Require.NotDisposed(nameof(KVTextReader), disposed);

            var @object = default(KVObject);

            while (stateMachine.IsInObject)
            {
                KVToken token;

                try
                {
                    token = tokenReader.ReadNextToken();
                }
                catch (InvalidDataException ex)
                {
                    throw new KeyValueException(ex.Message, ex);
                }
                catch (EndOfStreamException ex)
                {
                    throw new KeyValueException("Found end of file while trying to read token.", ex);
                }

                switch (token.TokenType)
                {
                case KVTokenType.String:
                    ReadText(token.Value);
                    break;

                case KVTokenType.ObjectStart:
                    BeginNewObject();
                    break;

                case KVTokenType.ObjectEnd:
                    FinalizeCurrentObject();
                    break;

                case KVTokenType.Condition:
                    HandleCondition(token.Value);
                    break;

                case KVTokenType.EndOfFile:
                    try
                    {
                        @object = FinalizeDocument();
                    }
                    catch (InvalidOperationException ex)
                    {
                        throw new KeyValueException("Found end of file when another token type was expected.", ex);
                    }

                    break;

                case KVTokenType.Comment:
                    break;

                case KVTokenType.IncludeAndMerge:
                    HandleIncludeAndMerge(token.Value);
                    break;

                case KVTokenType.IncludeAndAppend:
                    HandleIncludeAndAppend(token.Value);
                    break;

                default:
                    throw new NotImplementedException("The developer forgot to handle a KVTokenType.");
                }
            }

            if (@object == null)
            {
                throw new InvalidOperationException(); // Should be unreachable.
            }

            return(@object);
        }
Exemplo n.º 3
0
        static KVObject FromObjectCore <TObject>(TObject managedObject, string topLevelName, IObjectReflector reflector, HashSet <object> visitedObjects)
        {
            if (managedObject == null)
            {
                throw new ArgumentNullException(nameof(managedObject));
            }

            Require.NotNull(topLevelName, nameof(topLevelName));
            Require.NotNull(reflector, nameof(reflector));
            Require.NotNull(visitedObjects, nameof(visitedObjects));

            if (!typeof(TObject).IsValueType && typeof(TObject) != typeof(string) && !visitedObjects.Add(managedObject))
            {
                throw new KeyValueException("Serialization failed - circular object reference detected.");
            }

            if (typeof(IConvertible).IsAssignableFrom(typeof(TObject)))
            {
                return(new KVObject(topLevelName, (string)Convert.ChangeType(managedObject, typeof(string))));
            }

            var childObjects = new List <KVObject>();

            if (typeof(IDictionary).IsAssignableFrom(typeof(TObject)))
            {
                var dictionary = (IDictionary)managedObject;
                var enumerator = dictionary.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    var entry = enumerator.Entry;
                    childObjects.Add(new KVObject(entry.Key.ToString(), entry.Value.ToString()));
                }
            }
            else if (typeof(TObject).IsArray || typeof(IEnumerable).IsAssignableFrom(typeof(TObject)))
            {
                var counter = 0;
                foreach (var child in (IEnumerable)managedObject)
                {
                    var childKVObject = CopyObject(child, counter.ToString(), reflector, visitedObjects);
                    childObjects.Add(childKVObject);

                    counter++;
                }
            }
            else
            {
                foreach (var member in reflector.GetMembers(managedObject).OrderBy(p => p.Name))
                {
                    if (!member.MemberType.IsValueType && member.Value is null)
                    {
                        continue;
                    }

                    var name = member.Name;
                    if (!member.IsExplicitName && name.Length > 0 && char.IsUpper(name[0]))
                    {
                        name = char.ToLower(name[0]) + name.Substring(1);
                    }

                    if (typeof(IConvertible).IsAssignableFrom(member.MemberType))
                    {
                        childObjects.Add(new KVObject(name, (string)Convert.ChangeType(member.Value, typeof(string))));
                    }
                    else
                    {
                        childObjects.Add(CopyObject(member.Value, member.Name, reflector, visitedObjects));
                    }
                }
            }

            return(new KVObject(topLevelName, childObjects));
        }