Skip to content

Fix unquoted key error during serialization. #223

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/Hocon.Configuration.Test/SerializationSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,19 @@ public void ShouldResolveEmptyToEmpty()
HoconConfigurationFactory.ParseString("{}").IsEmpty.Should().BeTrue();
Config.Empty.IsEmpty.Should().BeTrue();
}

[Fact]
public void QuotedKeyWithInvalidCharactersShouldSerializeProperly()
{
var hoconString = @"this.""should[]"".work = true";
var config = HoconConfigurationFactory.ParseString(hoconString);
var serialized = JsonConvert.SerializeObject(config);
var deserialized = JsonConvert.DeserializeObject<Config>(serialized);

Assert.True(config.GetBoolean("this.\"should[]\".work"));
Assert.True(deserialized.GetBoolean("this.\"should[]\".work"));
Assert.Equal(config, deserialized);
}

}
}
18 changes: 12 additions & 6 deletions src/Hocon.Configuration/HoconConfigurationFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,19 @@ public static Config Default()
return FromFile(path);

// if we made it this far: no default HOCON files found. Check app.config
var def = Load("hocon"); // new default
if (!def.IsNullOrEmpty())
return def;
try
{
var def = Load("hocon"); // new default
if (!def.IsNullOrEmpty())
return def;

def = Load("akka"); // old Akka.NET-specific default
if (!def.IsNullOrEmpty())
return def;
def = Load("akka"); // old Akka.NET-specific default
if (!def.IsNullOrEmpty())
return def;
}
catch
{
}

return Config.Empty;
}
Expand Down
8 changes: 8 additions & 0 deletions src/Hocon.Tests/PathTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,13 @@ public void PathToStringQuoteKeysContainingDot()
Assert.NotEqual(path1.Value, path2.Value);
Assert.Equal("i am.\"kong.fu\".panda", path1.Value);
}

[Fact]
public void QuotedKeyShouldHandleInvalidCharacters()
{
var hoconString = @"this.""should[]"".work = true";
var config = HoconParser.Parse(hoconString);
Assert.True(config.GetBoolean("this.\"should[]\".work"));
}
}
}
2 changes: 1 addition & 1 deletion src/Hocon/Impl/HoconObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public string ToString(int indent, int indentSize)
var i = new string(' ', indent * indentSize);
var sb = new StringBuilder();
foreach (var field in this)
sb.Append($"{i}{field.Key} : {field.Value.ToString(indent + 1, indentSize)},{Environment.NewLine}");
sb.Append($"{i}{(field.Key.NeedQuotes() ? field.Key.AddQuotes() : field.Key)} : {field.Value.ToString(indent + 1, indentSize)},{Environment.NewLine}");
return sb.Length > 2 ? sb.ToString(0, sb.Length - Environment.NewLine.Length - 1) : sb.ToString();
}

Expand Down