-
-
Notifications
You must be signed in to change notification settings - Fork 560
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a sample for System.Text.Json interop (#1663)
- Loading branch information
Showing
2 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]); | ||
} | ||
} |