Skip to content

Commit

Permalink
Create a sample for System.Text.Json interop (#1663)
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma authored Oct 28, 2023
1 parent 2ae0dc2 commit e125c62
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
1 change: 1 addition & 0 deletions Jint.Tests/Jint.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="MongoDB.Bson.signed" Version="2.19.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="System.Text.Json" Version="6.0.8" />
<PackageReference Include="xunit" Version="2.5.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0">
<PrivateAssets>all</PrivateAssets>
Expand Down
83 changes: 83 additions & 0 deletions Jint.Tests/Runtime/InteropTests.SystemTextJson.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using System.Reflection;
using System.Text.Json.Nodes;
using Jint.Runtime.Interop;

namespace Jint.Tests.Runtime;

public partial class InteropTests
{
[Fact]
public void AccessingJsonNodeShouldWork()
{
const string Json = """
{
"employees": {
"type": "array",
"value": [
{
"firstName": "John",
"lastName": "Doe"
},
{
"firstName": "Jane",
"lastName": "Doe"
}
]
}
}
""";

var variables = JsonNode.Parse(Json);

var engine = new Engine(options =>
{
// JsonArray behave like JS array
options.Interop.WrapObjectHandler = static (e, target, type) =>
{
var wrapped = new ObjectWrapper(e, target);
if (target is JsonArray)
{
wrapped.SetPrototypeOf(e.Realm.Intrinsics.Array.PrototypeObject);
}
return wrapped;
};
// we cannot access this[string] with anything else than JsonObject, otherwise itw will throw
options.Interop.TypeResolver = new TypeResolver
{
MemberFilter = static info =>
{
if (info.DeclaringType != typeof(JsonObject) && info.Name == "Item" && info is PropertyInfo p)
{
var parameters = p.GetIndexParameters();
return parameters.Length != 1 || parameters[0].ParameterType != typeof(string);
}
return true;
}
};
});

engine
.SetValue("variables", variables)
.Execute("""
function populateFullName() {
return variables['employees'].value.map(item => {
var newItem =
{
"firstName": item.firstName,
"lastName": item.lastName,
"fullName": item.firstName + ' ' + item.lastName
};

return newItem;
});
}
""");

var result = engine.Evaluate("populateFullName()").AsArray();
Assert.Equal((uint) 2, result.Length);
Assert.Equal("John Doe", result[0].AsObject()["fullName"]);
Assert.Equal("Jane Doe", result[1].AsObject()["fullName"]);
}
}

0 comments on commit e125c62

Please sign in to comment.