Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support serialization of CLR Classes #2025

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CefSharp.BrowserSubprocess.Core/TypeUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ namespace CefSharp

return cefArray;
}
if (type->IsValueType && !type->IsPrimitive && !type->IsEnum)
if (!type->IsPrimitive && !type->IsEnum)
{
cli::array<System::Reflection::FieldInfo^>^ fields = type->GetFields();
CefRefPtr<CefV8Value> cefArray = CefV8Value::CreateArray(fields->Length);
Expand Down
2 changes: 1 addition & 1 deletion CefSharp.Core/Internals/Serialization/V8Serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ namespace CefSharp
}
list->SetList(index, subList);
}
else if (type->IsValueType && !type->IsPrimitive && !type->IsEnum)
else if (!type->IsPrimitive && !type->IsEnum)
{
auto fields = type->GetFields();
auto subDict = CefDictionaryValue::Create();
Expand Down
23 changes: 14 additions & 9 deletions CefSharp.Example/AsyncBoundObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@

namespace CefSharp.Example
{
public struct JsObject
{
public string Value;
}

public class AsyncBoundObject
{
Expand Down Expand Up @@ -43,20 +39,29 @@ public string DoSomething()
return "Waited for 1000ms before returning";
}

public JsObject ReturnObject(string name)
public JsSerializableClass ReturnObject(string name)
{
return new JsObject
return new JsSerializableClass
{
Value = name
};
}

public JsObject[] ObjectArray(string name)
public JsSerializableStruct[] StructsArray(JsSerializableStruct obj)
{
return new[]
{
new JsObject() { Value = "Item1" },
new JsObject() { Value = "Item2" }
new JsSerializableStruct() { Value = obj.Value + "1" },
new JsSerializableStruct() { Value = obj.Value + "2" }
};
}

public JsSerializableClass[] ClassesArray(string name)
{
return new[]
{
new JsSerializableClass() { Value = name + "1" },
new JsSerializableClass() { Value = name + "2" }
};
}

Expand Down
2 changes: 2 additions & 0 deletions CefSharp.Example/CefSharp.Example.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@
<Compile Include="Filters\PassThruResponseFilter.cs" />
<Compile Include="Handlers\BrowserProcessHandler.cs" />
<Compile Include="InMemorySchemeAndResourceHandlerFactory.cs" />
<Compile Include="JsSerializableStruct.cs" />
<Compile Include="JsSerializableClass.cs" />
<Compile Include="ModelBinding\MethodInterceptorLogger.cs" />
<Compile Include="RequestEventHandler\EventArgs\BaseRequestEventArgs.cs" />
<Compile Include="RequestEventHandler\EventArgs\GetAuthCredentialsEventArgs.cs" />
Expand Down
7 changes: 7 additions & 0 deletions CefSharp.Example/JsSerializableClass.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace CefSharp.Example
{
public class JsSerializableClass
{
public string Value;
}
}
7 changes: 7 additions & 0 deletions CefSharp.Example/JsSerializableStruct.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace CefSharp.Example
{
public struct JsSerializableStruct
{
public string Value;
}
}
30 changes: 21 additions & 9 deletions CefSharp.Example/Resources/BindingTest.html
Original file line number Diff line number Diff line change
Expand Up @@ -93,29 +93,41 @@
writeAsyncResult(call, end);
}

async function asyncObjectArray()
{
let call = "Async call (ObjectArray): " + Date();
let res = await boundAsync.objectArray('CefSharp');
let end = "Result: [ " + res.map(function (item) { return item.Value }) + " ] (" + Date() + ")";
writeAsyncResult(call, end);
}

async function asyncDictionaryPassedAsParam()
{
let call = [{ Name : "Chrome", Engine : {Name : "WebKit"} }, { Name : "Chromium", Engine : {Name : "WebKit"} }, { Name : "Opera", Engine : {Name : "WebKit"} }];
let res = await boundAsync.dynamiObjectList(call);
writeAsyncResult(call, res);
}

function asyncStructsArray()
{
var call = "Async call (StructsArray): " + Date();
boundAsync.structsArray({ Value: 'CefSharp' }).then(function (res)
{
var end = "Result: [ " + res.map(function (item) { return item.Value }) + " ] (" + Date() + ")";
writeAsyncResult(call, end);
});
}

function asyncClassesArray()
{
var call = "Async call (ClassesArray): " + Date();
boundAsync.classesArray('CefSharp').then(function (res) {
var end = "Result: [ " + res.map(function (item) { return item.Value }) + " ] (" + Date() + ")";
writeAsyncResult(call, end);
});
}

asyncError();
asyncDivOk();
asyncDivFail();
asyncDoSomething();
asyncHello();
asyncObject();
asyncObjectArray();
asyncDictionaryPassedAsParam();
asyncStructsArray();
asyncClassesArray();
</script>
</p>
<p>
Expand Down
2 changes: 1 addition & 1 deletion CefSharp/ModelBinding/ModelBindingExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2010-2017 The CefSharp Authors. All rights reserved.
// Copyright © 2010-2017 The CefSharp Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

Expand Down