-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update the jint options to support System.text.json (#15449)
- Loading branch information
Showing
3 changed files
with
182 additions
and
1 deletion.
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
56 changes: 56 additions & 0 deletions
56
src/OrchardCore/OrchardCore.Scripting.JavaScript/JsonValueConverter.cs
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,56 @@ | ||
using System.Text.Json; | ||
using System.Text.Json.Nodes; | ||
using Jint; | ||
using Jint.Native; | ||
using Jint.Runtime.Interop; | ||
|
||
namespace OrchardCore.Scripting.JavaScript; | ||
public class JsonValueConverter : IObjectConverter | ||
{ | ||
public bool TryConvert(Engine engine, object value, out JsValue result) | ||
{ | ||
if (value is JsonValue jsonValue) | ||
{ | ||
var valueKind = jsonValue.GetValueKind(); | ||
switch (valueKind) | ||
{ | ||
case JsonValueKind.Object: | ||
case JsonValueKind.Array: | ||
result = JsValue.FromObject(engine, jsonValue); | ||
break; | ||
case JsonValueKind.String: | ||
result = jsonValue.ToString(); | ||
break; | ||
case JsonValueKind.Number: | ||
if (jsonValue.TryGetValue<double>(out var doubleValue)) | ||
{ | ||
result = JsNumber.Create(doubleValue); | ||
} | ||
else | ||
{ | ||
result = JsValue.Undefined; | ||
} | ||
break; | ||
case JsonValueKind.True: | ||
result = JsBoolean.True; | ||
break; | ||
case JsonValueKind.False: | ||
result = JsBoolean.False; | ||
break; | ||
case JsonValueKind.Undefined: | ||
result = JsValue.Undefined; | ||
break; | ||
case JsonValueKind.Null: | ||
result = JsValue.Null; | ||
break; | ||
default: | ||
result = JsValue.Undefined; | ||
break; | ||
} | ||
return true; | ||
} | ||
result = JsValue.Undefined; | ||
return false; | ||
|
||
} | ||
} |
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,88 @@ | ||
using System.Text.Json.Nodes; | ||
using OrchardCore.Scripting; | ||
using OrchardCore.Tests.Apis.Context; | ||
|
||
namespace OrchardCore.Tests.Scripting; | ||
public class ScriptFunctionsTest | ||
{ | ||
[Fact] | ||
public async Task TheScriptingEngineShouldBeAbleToHandleJsonObject() | ||
{ | ||
using var context = new SiteContext(); | ||
await context.InitializeAsync(); | ||
await context.UsingTenantScopeAsync(scope => | ||
{ | ||
var findUser = new GlobalMethod | ||
{ | ||
Name = "tryAccessJsonObject", | ||
Method = sp => () => | ||
{ | ||
const string jsonData = """ | ||
{ | ||
"age":33, | ||
"falseValue":false, | ||
"trueValue":true, | ||
"stringValue":"stringTest", | ||
"employees": { | ||
"type": "array", | ||
"value": [ | ||
{ | ||
"firstName": "John", | ||
"lastName": "Doe" | ||
}, | ||
{ | ||
"firstName": "Jane", | ||
"lastName": "Doe" | ||
} | ||
] | ||
} | ||
} | ||
"""; | ||
return JObject.Parse(jsonData); | ||
} | ||
}; | ||
var scriptingEngine = scope.ServiceProvider.GetRequiredService<IScriptingEngine>(); | ||
var scriptingScope = scriptingEngine.CreateScope([findUser], scope.ServiceProvider, null, null); | ||
var result = (bool)scriptingEngine.Evaluate(scriptingScope, | ||
@"var jobj = tryAccessJsonObject(); | ||
return jobj.age == 33; | ||
"); | ||
Assert.True(result); | ||
result = (bool)scriptingEngine.Evaluate(scriptingScope, | ||
@"var jobj = tryAccessJsonObject(); | ||
return jobj.stringValue == ""stringTest""; | ||
"); | ||
Assert.True(result); | ||
result = (bool)scriptingEngine.Evaluate(scriptingScope, | ||
@"var jobj = tryAccessJsonObject(); | ||
return jobj.employees.type == ""array"" && | ||
jobj.employees.value[0].firstName == ""John""; | ||
"); | ||
Assert.True(result); | ||
var result1 = scriptingEngine.Evaluate(scriptingScope, | ||
@"var jobj = tryAccessJsonObject(); | ||
var steps = []; | ||
if(!jobj.falseValue) steps.push(1); | ||
if(jobj.trueValue) steps.push(2); | ||
// falseValue should be false | ||
if(jobj.falseValue == false) steps.push(3); | ||
if(jobj.trueValue == true) steps.push(4); | ||
if(!!jobj.trueValue) steps.push(5); | ||
steps.push(jobj.falseValue); | ||
steps.push(jobj.falseValue.toString()); | ||
return steps.join(',') | ||
"); | ||
Assert.Equal("1,2,3,4,5,false,false", result1); | ||
return Task.CompletedTask; | ||
}); | ||
} | ||
} |