Skip to content

Commit

Permalink
Serialization fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
ackava committed Aug 15, 2024
1 parent 8bfa124 commit a868854
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 13 deletions.
16 changes: 15 additions & 1 deletion NativeShell/Controls/NativeWebView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,22 @@ public NativeWebView()
this.Clr = new GlobalClr();
Context["clr"] = Context.Marshal(Clr);

Context["serialize"] = Context.CreateFunction(1, (c, s) => {
try
{
var arg0 = s[0];
var serialized = Clr.Serialize(arg0);
return Context.CreateString(serialized);
} catch (Exception error)
{
System.Diagnostics.Debug.WriteLine(error.ToString());
return Context.CreateString("null");
}
}, "serialize");

Context["evalInPage"] = Context.CreateFunction(1, (c, s) => {
this.Eval(s.ToString());
var script = s[0].ToString();
this.Eval(script);
return Context.Undefined;
}, "sendToBrowser");

Expand Down
44 changes: 44 additions & 0 deletions NativeShell/Core/GlobalClr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,50 @@ public GlobalClr()
return Type.GetType(typeName);
}

public string Serialize(IJSValue value)
{
if (value.IsValueNull || value.IsUndefined)
{
return NullJson;
}
if (value.IsString)
{
return Serialize(value.ToString()!);
}
if (value.IsNumber)
{
return Serialize(value.DoubleValue!);
}
if (value.IsDate)
{
return Serialize(value.DateValue!);
}
if (value.IsBoolean)
{
return Serialize(value.BooleanValue!);
}
if (value.IsArray)
{
return Serialize(value.ToArray().Select((x) => SerializeAsync(x)).ToList());
}
if (value.IsObject)
{
var list = new List<string>();
foreach (var item in value.Entries)
{
list.Add($"\"{item.Key}\": {SerializeAsync(item.Value)}");
}
return "{" + string.Join(",", list) + "}";
}
if (value.IsWrapped)
{
var v = value.Unwrap<object>();
throw new NotSupportedException($"You cannot transfer clr object to JavaScript");

}
return Serialize(value.ToString());
}

public async Task<string> SerializeAsync(IJSValue value)
{
if (value.IsValueNull || value.IsUndefined)
Expand Down
11 changes: 9 additions & 2 deletions NativeShell/Engine/JSContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -460,10 +460,17 @@ public static IJSValue Marshal(this IJSContext context, object valueToCopy, Seri
{
if (valueToCopy == null)
{
return null;
return context.Null;
}
if (valueToCopy is IJSValue jv) return jv;
var type = valueToCopy.GetType();

if (valueToCopy is Type type)
{
return context.CreateClass(type);
}

type = valueToCopy.GetType();

type = Nullable.GetUnderlyingType(type) ?? type;
if (type.IsEnum)
{
Expand Down
4 changes: 2 additions & 2 deletions NativeShell/NativeShell.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="YantraJS.ExpressionCompiler" Version="1.2.209" ReferenceOutputAssembly="false" OutputItemType="Analyzer" />
<PackageReference Include="YantraJS.JSClassGenerator" Version="1.2.209" />
<PackageReference Include="CommunityToolkit.Maui" Version="9.0.2" />
<PackageReference Include="Nerdbank.GitVersioning" Version="3.6.139">
<PrivateAssets>all</PrivateAssets>
Expand All @@ -44,6 +42,8 @@
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net8.0-android'">
<PackageReference Include="YantraJS.ExpressionCompiler" Version="1.2.209" ReferenceOutputAssembly="false" OutputItemType="Analyzer" />
<PackageReference Include="YantraJS.JSClassGenerator" Version="1.2.209" />
<PackageReference Include="Plugin.Firebase" Version="3.0.0" />
<PackageReference Include="Xamarin.Kotlin.StdLib.Jdk7" Version="2.0.0.1" ExcludeAssets="build;buildTransitive" />
<PackageReference Include="Xamarin.Kotlin.StdLib.Jdk8" Version="2.0.0.1" ExcludeAssets="build;buildTransitive" />
Expand Down
8 changes: 4 additions & 4 deletions NativeShell/Resources/NativeShell.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@
let result = ($code$).apply({ clr, evalInPage }, a);
if (result && result.then) {
result.then((r) => {
evalInPage(`window.nativeShell.on($rid$, ${JSON.stringify(r) || 1})`);
evalInPage(`window.nativeShell.on($rid$, ${serialize(r) || 1})`);
}, (e) => {
evalInPage(`window.nativeShell.on($rid$, void 0, ${JSON.stringify(e.stack || e)})`);
evalInPage(`window.nativeShell.on($rid$, void 0, ${serialize(e.stack || e)})`);
});
} else {
setTimeout(() =>
evalInPage(`window.nativeShell.on($rid$, ${JSON.stringify(r) || 1})`),
evalInPage(`window.nativeShell.on($rid$, ${serialize(result) || 1})`),
1);
}
} catch (error) {
setTimeout(() =>
evalInPage(`window.nativeShell.on($rid$, void 0, ${JSON.stringify(error.stack || error)})`),
evalInPage(`window.nativeShell.on($rid$, void 0, ${serialize(error.stack || error)})`),
1);
}
}
Expand Down
2 changes: 1 addition & 1 deletion NativeShellApp/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public App()
var mp = new NativeShellMainPage() {
Url = "https://m.800casting.com/ProfileEditor/Agency"
};
mp.WebView.UserAgent = "800Casting-Hybrid-Mobile-App/1.0";
mp.WebView.UserAgent = "Hybrid-Mobile-App/1.0 Android/1.1";


MainPage = mp;
Expand Down
9 changes: 7 additions & 2 deletions NativeShellApp/NativeShellApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
<PackageReference Include="Xamarin.AndroidX.Collection.Ktx" Version="1.3.0.1" />
<PackageReference Include="Xamarin.Kotlin.StdLib.Jdk7" Version="2.0.0.1" ExcludeAssets="build;buildTransitive" />
<PackageReference Include="Xamarin.Kotlin.StdLib.Jdk8" Version="2.0.0.1" ExcludeAssets="build;buildTransitive" />
<GoogleServicesJson Include="config\google-services.json" />
</ItemGroup>

<ItemGroup>
Expand All @@ -74,8 +75,12 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\NativeShell\NativeShell.csproj" />
<PackageReference Include="Microsoft.Maui.Controls" Version="8.0.71" />
<ProjectReference Include="..\NativeShell\NativeShell.csproj" />
<PackageReference Include="Microsoft.Maui.Controls" Version="8.0.80" />
<PackageReference Update="Microsoft.Maui.Controls.Compatibility" Version="8.0.80" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />

</ItemGroup>

</Project>

0 comments on commit a868854

Please sign in to comment.