rickyah / ini-parser

Read/Write an INI file the easy way!
MIT License
972 stars 241 forks source link

Redis conf parse failed #207

Open naughtyGitCat opened 4 years ago

naughtyGitCat commented 4 years ago
                this.logger.LogDebug($"redis config string: \n{raw.ConfigString}");
                var parser = new IniDataParser(new IniParser.Model.Configuration.IniParserConfiguration { AllowKeysWithoutSection = true });
                parser.Configuration.CommentString = "#";
                var data = parser.Parse(raw.ConfigString);
                this.logger.LogDebug($"parsed conf: {data}");

turns:

IniParser.Exceptions.ParsingException: Unknown file format. Couldn't parse the line: 'bind 10.160.246.207'. while parsing line number 0 with value '' - IniParser version: 2.5.2.0 while parsing line number 1 with value 'bind 10.160.246.207' - IniParser version: 2.5.2.0
 ---> IniParser.Exceptions.ParsingException: Unknown file format. Couldn't parse the line: 'bind 100.100.206.207'. while parsing line number 0 with value '' - IniParser version: 2.5.2.0
   at IniParser.Parser.IniDataParser.ProcessLine(String currentLine, IniData currentIniData)
   at IniParser.Parser.IniDataParser.Parse(String iniDataString)
   --- End of inner exception stack trace ---
   at IniParser.Parser.IniDataParser.Parse(String iniDataString)

the raw.String is below:

bind 100.100.206.207
protected-mode yes
port 6579
tcp-backlog 1000
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
loglevel notice
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
maxclients 300
maxmemory 2GB

# volatile-lru -> remove the key with an expire set using an LRU algorithm
# allkeys-lru -> remove any key according to the LRU algorithm
# volatile-random -> remove a random key with an expire set
# allkeys-random -> remove a random key, any key
# volatile-ttl -> remove the key with the nearest expire time (minor TTL)
naughtyGitCat commented 4 years ago

fixed with

                parser.Configuration.KeyValueAssigmentChar = ' ';

but the

save 900 1
save 300 10
save 60 10000

how to parse? the parser.Configuration.AllowDuplicateKeys = true only leaves one save

naughtyGitCat commented 4 years ago

and unable to use System.Text.Json to serialize

System.InvalidCastException: Unable to cast object of type 'System.Collections.Generic.KeyValuePair`2[System.String,IniParser.Model.KeyData]' to type 'IniParser.Model.KeyData'.
   at get_Comments(Object )
   at System.Text.Json.JsonPropertyInfoCommon`4.GetValueAsObject(Object obj)
   at System.Text.Json.JsonSerializer.HandleEnumerable(JsonClassInfo elementClassInfo, JsonSerializerOptions options, Utf8JsonWriter writer, WriteStack& state)
   at System.Text.Json.JsonSerializer.Write(Utf8JsonWriter writer, Int32 originalWriterDepth, Int32 flushThreshold, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.JsonSerializer.WriteCore(Utf8JsonWriter writer, Object value, Type type, JsonSerializerOptions options)
   at System.Text.Json.JsonSerializer.WriteCore(PooledByteBufferWriter output, Object value, Type type, JsonSerializerOptions options)
   at System.Text.Json.JsonSerializer.WriteCoreString(Object value, Type type, JsonSerializerOptions options)
   at System.Text.Json.JsonSerializer.Serialize[TValue](TValue value, JsonSerializerOptions options)
naughtyGitCat commented 4 years ago

serialize problem fixed with

                var dict = new Dictionary<string, object>();
                foreach (var i in data.Global)
                {
                    dict.Add(i.KeyName, i.Value);
                }

hope there is a ToJson Method, or __json__ magic method