Example #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);
            }
        }
Example #2
0
        public override string SerializeObject(object value, FileStyle _)
        {
            if (value is T item)
            {
                return(SerializeItem(item));
            }

            throw new ArgumentException($"Wrong type!!!!");
        }
Example #3
0
        private static string SerializeEnum(object value, FileStyle style)
        {
            switch (style.EnumStyle)
            {
            case EnumStyle.name:
            default:
                return(value.ToString());

            case EnumStyle.number:
                return(((Enum)value).ToString("d"));
            }
        }
Example #4
0
        internal static string SerializeBaseType(object thing, Type type, FileStyle style)
        {
            if (BaseSerializeMethods.TryGetValue(type, out var method))
            {
                return(method(thing));
            }

            if (BaseStyledSerializeMethods.TryGetValue(type, out var stylemethod))
            {
                return(stylemethod(thing, style));
            }

            if (type.IsEnum)
            {
                return(SerializeEnum(thing, style));
            }

            throw new Exception($"Cannot serialize base type {type} - are you sure it is a base type?");
        }
Example #5
0
        private static string SerializeBool(object value, FileStyle style)
        {
            bool b = (bool)value;

            switch (style.BoolStyle)
            {
            case BoolStyle.true_false:
            default:
                return(b ? "true" : "false");

            case BoolStyle.on_off:
                return(b ? "on" : "off");

            case BoolStyle.yes_no:
                return(b ? "yes" : "no");

            case BoolStyle.y_n:
                return(b ? "y" : "n");
            }
        }
Example #6
0
        private static string SerializeString(object value, FileStyle style)
        {
            string text = (string)value;

            if (String.IsNullOrEmpty(text))
            {
                return(String.Empty);
            }

            if (
                style.AlwaysQuoteStrings ||
                text[0] == ' ' || text[text.Length - 1] == ' ' ||
                text.IsQuoted()
                )
            {
                text = text.Quote();
            }

            return(text);
        }
Example #7
0
        private static string SerializeString(object value, FileStyle style)
        {
            string text = (string)value;

            if (String.IsNullOrEmpty(text))
            {
                return(String.Empty);
            }

            text = text.Replace("\t", "    "); // SUCC files cannot contain tabs. Prevent saving strings with tabs in them.

            if (
                style.AlwaysQuoteStrings ||
                text[0] == ' ' || text[text.Length - 1] == ' ' ||
                text.IsQuoted()
                )
            {
                text = text.Quote();
            }

            return(text);
        }
Example #8
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);
            }
        }
Example #9
0
        /// <summary>
        /// Creates a new DataFile object corresponding to a SUCC file in system storage, with the option to have a custom FileStyle.
        /// </summary>
        /// <param name="path"> the path of the file. Can be either absolute or relative to the default path. </param>
        /// <param name="style"> the rules for how this file styles newly saved data </param>
        /// <param name="defaultFileText"> optionally, if there isn't a file at the path, one can be created from the text supplied here. </param>
        /// <param name="autoSave"> if true, the DataFile will automatically save changes to disk with each Get or Set. Otherwise, you must call SaveAllData() manually. </param>
        /// <param name="autoReload"> if true, the DataFile will automatically reload when the file changes on disk. </param>
        public DataFile(string path, FileStyle style, string defaultFileText = null, bool autoSave = true, bool autoReload = false) : base(autoSave, style)
        {
            path          = Utilities.AbsolutePath(path);
            path          = Path.ChangeExtension(path, Utilities.FileExtension);
            this.FilePath = path;

            if (!Utilities.SuccFileExists(path))
            {
                if (defaultFileText == null)
                {
                    Directory.CreateDirectory(new FileInfo(path).Directory.FullName);
                    File.Create(path).Close(); // create empty file on disk
                }
                else
                {
                    File.WriteAllText(path, defaultFileText);
                }
            }

            this.ReloadAllData();

            SetupWatcher(); // setup watcher AFTER file has been created
            this.AutoReload = autoReload;
        }
Example #10
0
 /// <summary>
 /// Creates a new DataFile object corresponding to a SUCC file in system storage, with the option to have a custom FileStyle.
 /// </summary>
 /// <param name="path"> the path of the file. Can be either absolute or relative to the default path. </param>
 /// <param name="style"> the rules for how this file styles newly saved data </param>
 /// <param name="defaultFileText"> optionally, if there isn't a file at the path, one can be created from the text supplied here. </param>
 /// <param name="autoSave"> if true, the DataFile will automatically save changes to disk with each Get or Set. Otherwise, you must call SaveAllData() manually. </param>
 /// <param name="autoReload"> if true, the DataFile will automatically reload when the file changes on disk. </param>
 public DataFile(string path, FileStyle style, string defaultFileText = null, bool autoSave = true, bool autoReload = false) : base(path, defaultFileText, autoReload)
 {
     AutoSave = autoSave;
     Style    = style;
 }
Example #11
0
 internal static void SetBaseTypeNode(Node node, object thing, Type type, FileStyle style)
 {
     node.CapChildCount(0);
     node.ChildNodeType = NodeChildrenType.none;
     node.Value         = SerializeBaseType(thing, type, style);
 }
Example #12
0
 internal static string SerializeBaseType <T>(T thing, FileStyle style) => SerializeBaseType(thing, typeof(T), style);
Example #13
0
 public abstract string SerializeItem(T value, FileStyle style);
Example #14
0
 internal static void SetNodeData <T>(Node node, T data, FileStyle style) => SetNodeData(node, data, typeof(T), style);