Esempio n. 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);
            }
        }
Esempio n. 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}");
            }
        }
Esempio n. 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();
            }
        }
Esempio n. 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);
        }