Beispiel #1
0
        internal static void SetNodeData(Node node, object data, Type type, FileStyle style)
        {
            if (data == null)
            {
                throw new Exception("you can't serialize null");
            }

            string dataAsString = data as string;

            if (type == typeof(string) && (dataAsString.ContainsNewLine() || node.ChildNodes.Count > 0))
            {
                BaseTypes.SerializeSpecialStringCase(dataAsString, node, style);
            }

            else if (BaseTypes.IsBaseType(type))
            {
                node.Value = BaseTypes.SerializeBaseType(data, type, style);
            }

            else if (CollectionTypes.TrySetCollection(node, data, type, style))
            {
                return;
            }

            else
            {
                ComplexTypes.SetComplexNode(node, data, type, style);
            }
        }
Beispiel #2
0
        internal static object GetNodeData(Node node, Type type)
        {
            try
            {
                if (type == typeof(string) && node.Value == MultiLineStringNode.Terminator && node.ChildLines.Count > 0)
                {
                    return(BaseTypes.ParseSpecialStringCase(node));
                }

                if (BaseTypes.IsBaseType(type))
                {
                    return(BaseTypes.ParseBaseType(node.Value, type));
                }

                var collection = CollectionTypes.TryGetCollection(node, type);
                if (collection != null)
                {
                    return(collection);
                }

                if (!String.IsNullOrEmpty(node.Value))
                {
                    return(ComplexTypeShortcuts.GetFromShortcut(node.Value, type));
                }

                return(ComplexTypes.RetrieveComplexType(node, type));
            }
            catch (Exception e)
            {
                throw new Exception($"Error getting data of type {type} from node: {e.Message}");
            }
        }
Beispiel #3
0
        /// <summary> Save this file as a dictionary, using the dictionary's keys as top-level keys in the file. </summary>
        /// <remarks> TKey must be a Base Type </remarks>
        public void SaveAsDictionary <TKey, TValue>(IDictionary <TKey, TValue> dictionary)
        {
            if (!BaseTypes.IsBaseType(typeof(TKey)))
            {
                throw new Exception("When using GetAsDictionary, TKey must be a base type");
            }

            bool _autosave = AutoSave;

            AutoSave = false; // don't write to disk when we don't have to

            try
            {
                var CurrentKeys = new List <string>(capacity: dictionary.Count);
                foreach (var key in dictionary.Keys)
                {
                    var keyText = BaseTypes.SerializeBaseType(key, Style);
                    if (!Utilities.IsValidKey(keyText, out string whyNot))
                    {
                        throw new Exception($"can't save file as this dictionary. A key ({keyText}) is not valid: {whyNot}");
                    }

                    CurrentKeys.Add(keyText);
                    Set(keyText, dictionary[key]);
                }

                // make sure that old data in the file is deleted when a new dictionary is saved.
                foreach (var key in this.GetTopLevelKeys())
                {
                    if (!CurrentKeys.Contains(key))
                    {
                        this.TopLevelNodes.Remove(key);
                    }
                }
            }
            finally
            {
                AutoSave = _autosave;
            }

            if (AutoSave)
            {
                SaveAllData();
            }
        }
Beispiel #4
0
        /// <summary> Interpret this file as a dictionary. Top-level keys in the file are interpreted as keys in the dictionary. </summary>
        /// <remarks> TKey must be a Base Type </remarks>
        public Dictionary <TKey, TValue> GetAsDictionary <TKey, TValue>()
        {
            if (!BaseTypes.IsBaseType(typeof(TKey)))
            {
                throw new Exception("When using GetAsDictionary, TKey must be a base type");
            }

            var keys       = GetTopLevelKeys();
            var dictionary = new Dictionary <TKey, TValue>(capacity: keys.Length);

            foreach (var keyText in keys)
            {
                TKey   key   = BaseTypes.ParseBaseType <TKey>(keyText);
                TValue value = NodeManager.GetNodeData <TValue>(TopLevelNodes[keyText]);
                dictionary.Add(key, value);
            }

            return(dictionary);
        }
Beispiel #5
0
        // support for multi-line strings
        internal static void SetStringSpecialCase(Node node, string value, FileStyle style)
        {
            if (value != null && value.ContainsNewLine())
            {
                node.Value = MultiLineStringNode.Terminator;
                var lines = value.SplitIntoLines();

                node.CapChildCount(lines.Length + 1);

                for (int i = 0; i < lines.Length; i++)
                {
                    var newnode = node.GetChildAddresedByStringLineNumber(i);
                    newnode.Value = BaseTypes.SerializeString(lines[i], style);
                }

                node.GetChildAddresedByStringLineNumber(lines.Length).MakeTerminator();
                return;
            }
            else
            {
                node.ClearChildren();
                node.Value = BaseTypes.SerializeString(value, style);
            }
        }