Skip to content

Commit c9be909

Browse files
committed
Add support for unquoting JSV keys to make it more JSON compatible
1 parent 32e78ef commit c9be909

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

ServiceStack.Text/src/ServiceStack.Text/Jsv/JsvTypeSerializer.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,10 @@ public ReadOnlySpan<char> EatMapKey(ReadOnlySpan<char> value, ref int i)
360360
if (!isLiteralQuote)
361361
break;
362362
}
363-
return value.Slice(tokenStartPos, i - tokenStartPos);
363+
var key = value.Slice(tokenStartPos, i - tokenStartPos);
364+
if (key.Length > 2 && key[0] == JsWriter.QuoteChar) // Don't unquote empty keys
365+
key = key.Slice(1, key.Length - 2);
366+
return key;
364367

365368
//Is Type/Map, i.e. {...}
366369
case JsWriter.MapStartChar:

ServiceStack.Text/tests/ServiceStack.Text.Tests/JsvTests/JsvCollectionTests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,15 @@ public void Can_serialize_empty_collections()
5858

5959
collection.PrintDump();
6060
}
61+
62+
[Test]
63+
public void Can_deserialize_quoted_keys()
64+
{
65+
var itemArray = "[{\"key\":\"KEY\",\"value\":\"VAL\"}]";
66+
var items = TypeSerializer.DeserializeFromString<Item[]>(itemArray);
67+
var item = items[0];
68+
Assert.That(item.Key, Is.EqualTo("KEY"));
69+
Assert.That(item.Value, Is.EqualTo("VAL"));
70+
}
6171
}
6272
}

0 commit comments

Comments
 (0)