Skip to content

Commit 6d14466

Browse files
IgorFedchenkoAaronontheweb
authored andcommitted
Fix parsing of quoted keys (#140)
* Added reproducing test * Fixed double-quoted paths in object
1 parent 77c7c0e commit 6d14466

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

src/Hocon.Tests/HoconTests.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//-----------------------------------------------------------------------
1+
//-----------------------------------------------------------------------
22
// <copyright file="HoconTests.cs" company="Hocon Project">
33
// Copyright (C) 2009-2018 Lightbend Inc. <http://www.lightbend.com>
44
// Copyright (C) 2013-2018 .NET Foundation <https://github.com/akkadotnet/hocon>
@@ -230,6 +230,19 @@ public void CanParseObject()
230230
Assert.Equal("1", Parser.Parse(hocon).GetString("a.b"));
231231
}
232232

233+
[Fact]
234+
public void CanParseQuotedElements()
235+
{
236+
var hocon = @"
237+
A.B = 1
238+
A {
239+
""X.Y"" = 1
240+
}
241+
";
242+
var ex = Record.Exception(() => Parser.Parse(hocon).GetObject("A"));
243+
Assert.Null(ex);
244+
}
245+
233246
[Fact]
234247
public void CanTrimValue()
235248
{

src/Hocon/Impl/HoconObject.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,15 @@ public HoconField GetField(HoconPath path)
171171

172172
if (path.Count == 0)
173173
throw new ArgumentException("Path is empty.", nameof(path));
174-
175-
var pathIndex = 0;
174+
176175
var currentObject = this;
176+
177+
// Sometimes path may be a double-quoted string like "a.b.c" with quotes ommited,
178+
// so check if there is such key first
179+
if (currentObject.TryGetValue(path.ToString(), out var rootField))
180+
return rootField;
181+
182+
var pathIndex = 0;
177183
while (true)
178184
{
179185
var key = path[pathIndex];

0 commit comments

Comments
 (0)