Class SF
- Namespace
- SunamoSerializer
- Assembly
- SunamoSerializer.dll
Provides serialization and deserialization utilities for text-based data files with delimited content.
public static class SF
- Inheritance
-
SF
- Inherited Members
Fields
ReplaceForSeparatorChar
Replacement character used when separator character is found in content.
public static readonly char ReplaceForSeparatorChar
Field Value
ReplaceForSeparatorString
Replacement string used when separator character is found in content.
public const string ReplaceForSeparatorString = "_"
Field Value
Properties
DefaultDelimiter
Gets or sets the default delimiter used for serialization.
public static string DefaultDelimiter { get; set; }
Property Value
KeyCodeSeparator
Gets the key code of the separator character.
public static int KeyCodeSeparator { get; }
Property Value
SeparatorChar
Gets the separator character. Must be property to avoid inconsistency when changing value.
public static char SeparatorChar { get; }
Property Value
SeparatorString
Gets or sets the separator string used for serialization.
public static string SeparatorString { get; set; }
Property Value
Methods
AppendAllText(string, string)
Appends a line to a file and returns all parsed elements.
public static Task<List<List<string>>> AppendAllText(string filePath, string line)
Parameters
Returns
DictionaryAppend(string, Dictionary<int, string>)
Appends a dictionary to an existing file in serialized format.
public static Task DictionaryAppend(string filePath, Dictionary<int, string> dictionary)
Parameters
filePathstringPath to the file to append to.
dictionaryDictionary<int, string>The dictionary to serialize and append.
Returns
Dictionary<TKey, TValue>(string, Dictionary<TKey, TValue>)
Writes a dictionary to a file in serialized format.
public static Task Dictionary<TKey, TValue>(string filePath, Dictionary<TKey, TValue> dictionary) where TKey : notnull
Parameters
filePathstringPath to the output file.
dictionaryDictionary<TKey, TValue>The dictionary to serialize and write.
Returns
Type Parameters
TKeyThe type of dictionary keys.
TValueThe type of dictionary values.
GetAllElementsFile(string)
Gets all elements from all lines of a file, with header included as first row.
public static List<List<string>> GetAllElementsFile(string filePath)
Parameters
filePathstringPath to the file to read.
Returns
GetAllElementsFile(string, string)
Gets all elements from all lines of a file using the specified separator.
public static List<List<string>> GetAllElementsFile(string filePath, string separator = "|")
Parameters
filePathstringPath to the file to read.
separatorstringThe separator string used between elements.
Returns
GetAllElementsFileAdvanced(string, string)
Gets all elements from a file, returning header and data rows separately.
public static (List<string> header, List<List<string>> rows) GetAllElementsFileAdvanced(string filePath, string separator = "|")
Parameters
filePathstringPath to the file to read.
separatorstringThe separator string used between elements. Defaults to "|".
Returns
- (List<string> header, List<List<string>> rows)
A tuple containing the header elements and a list of row element lists.
GetAllElementsLine(string, string?)
Gets all elements from a single line using the specified separator.
public static List<string> GetAllElementsLine(string text, string? separator = null)
Parameters
textstringThe line to parse.
separatorstringThe separator string used between elements. Defaults to "|".
Returns
GetElementAtIndexFile(string, int, int)
Gets the element at specified indices from a file. Returns null if not found.
public static string? GetElementAtIndexFile(string filePath, int elementIndex, int lineIndex)
Parameters
filePathstringPath to the file to read.
elementIndexintThe element index within a line.
lineIndexintThe line index in the file.
Returns
- string
The element at the specified position, or null if not found.
GetFirstWhereIsFirstElement(string, string)
Gets the first line where the first element matches the specified value. Returns null if not found.
public static List<string>? GetFirstWhereIsFirstElement(string filePath, string firstElement)
Parameters
filePathstringPath to the file to read.
firstElementstringThe value to match against the first element of each line.
Returns
GetLastWhereIsFirstElement(string, string)
Gets the last line where the first element matches the specified value. Returns null if not found.
public static List<string>? GetLastWhereIsFirstElement(string filePath, string firstElement)
Parameters
filePathstringPath to the file to read.
firstElementstringThe value to match against the first element of each line.
Returns
ListFromDictionary<TKey, TValue>(Dictionary<TKey, TValue>)
Converts a dictionary into a list of string lists for serialization.
public static List<List<string>> ListFromDictionary<TKey, TValue>(Dictionary<TKey, TValue> dictionary) where TKey : notnull
Parameters
dictionaryDictionary<TKey, TValue>The dictionary to convert.
Returns
Type Parameters
TKeyThe type of dictionary keys.
TValueThe type of dictionary values.
ParseUpToRequiredElementsLine(string, int)
Parses a line and ensures it contains exactly the required number of elements, padding with empty strings if needed.
public static List<string> ParseUpToRequiredElementsLine(string text, int requiredCount)
Parameters
Returns
PrepareToSerialization(List<string>, string)
Serializes a list of strings into a single delimited line.
public static string PrepareToSerialization(List<string> list, string separator = "|")
Parameters
listList<string>The list of strings to serialize.
separatorstringThe separator string to use between elements.
Returns
- string
A delimited string containing all elements.
PrepareToSerialization(params string[])
Serializes string elements into a single delimited line using the default delimiter.
public static string PrepareToSerialization(params string[] elements)
Parameters
elementsstring[]The elements to serialize.
Returns
- string
A delimited string containing all elements.
ReadFileOfSettingsOther(string, Func<string, string>)
Reads a settings file and extracts the separator from its first line.
public static void ReadFileOfSettingsOther(string filePath, Func<string, string> readFileFunc)
Parameters
filePathstringPath or name of the settings file.
readFileFuncFunc<string, string>Function that resolves the file path and reads its content.
RemoveComments(List<string>)
Removes comment lines (starting with #) and whitespace-only lines from the input.
public static List<string> RemoveComments(List<string> list)
Parameters
Returns
ToDictionary<TKey, TValue>(List<List<string>>)
Converts a list of string element lists into a dictionary by parsing each pair.
public static Dictionary<TKey, TValue> ToDictionary<TKey, TValue>(List<List<string>> list) where TKey : notnull
Parameters
listList<List<string>>The list of string element lists where each inner list should have exactly 2 elements.
Returns
- Dictionary<TKey, TValue>
Dictionary populated from the parsed elements.
Type Parameters
TKeyThe type of dictionary keys.
TValueThe type of dictionary values.
WriteAllElementsToFile(string, List<List<string>>)
Writes all element lists to a file in serialized format.
public static Task WriteAllElementsToFile(string filePath, List<List<string>> list)
Parameters
filePathstringPath to the output file.
listList<List<string>>The list of element lists to serialize and write.
Returns
WriteAllElementsToFile(string, List<string>[])
Writes all element arrays to a file in serialized format.
public static Task WriteAllElementsToFile(string filePath, List<string>[] array)
Parameters
filePathstringPath to the output file.
arrayList<string>[]Array of element lists to serialize and write.
Returns
WriteAllElementsToFile<TKey, TValue>(string, Dictionary<TKey, TValue>)
Writes all elements from a dictionary to a file in serialized format.
public static Task WriteAllElementsToFile<TKey, TValue>(string filePath, Dictionary<TKey, TValue> dictionary) where TKey : notnull
Parameters
filePathstringPath to the output file.
dictionaryDictionary<TKey, TValue>The dictionary to serialize and write.
Returns
Type Parameters
TKeyThe type of dictionary keys.
TValueThe type of dictionary values.