Skip to content

Commit

Permalink
Code literal generation for JsonElement
Browse files Browse the repository at this point in the history
Fixes #1101
  • Loading branch information
roji committed Nov 7, 2019
1 parent 199292b commit 5d53171
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ public override Expression GenerateCodeLiteral(object value)
=> value switch
{
JsonDocument document => Expression.Call(ParseMethod, Expression.Constant(document.RootElement.ToString()), DefaultJsonDocumentOptions),
JsonElement _ => throw new NotSupportedException("Cannot currently generate code literals for JsonElement"),
JsonElement element => Expression.Property(
Expression.Call(ParseMethod, Expression.Constant(element.ToString()), DefaultJsonDocumentOptions),
nameof(JsonDocument.RootElement)),
string s => Expression.Constant(s),
_ => throw new NotSupportedException("Cannot generate code literals for JSON POCOs")
};
Expand Down
6 changes: 6 additions & 0 deletions test/EFCore.PG.Tests/Storage/NpgsqlTypeMappingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,12 @@ public void GenerateCodeLiteral_returns_json_document_literal()
@"System.Text.Json.JsonDocument.Parse(""{\""Name\"":\""Joe\"",\""Age\"":25}"", new System.Text.Json.JsonDocumentOptions())",
CodeLiteral(JsonDocument.Parse(@"{""Name"":""Joe"",""Age"":25}")));

[Fact]
public void GenerateCodeLiteral_returns_json_element_literal()
=> Assert.Equal(
@"System.Text.Json.JsonDocument.Parse(""{\""Name\"":\""Joe\"",\""Age\"":25}"", new System.Text.Json.JsonDocumentOptions()).RootElement",
CodeLiteral(JsonDocument.Parse(@"{""Name"":""Joe"",""Age"":25}").RootElement));

static readonly Customer SampleCustomer = new Customer
{
Name = "Joe",
Expand Down

0 comments on commit 5d53171

Please sign in to comment.