From 1542fa3cfcbc95a6ce69130b2ee539763bfbcffc Mon Sep 17 00:00:00 2001 From: WarWithinMe Date: Thu, 22 Mar 2018 09:00:52 +0800 Subject: [PATCH] Serialize Dictionary type to V8 object (#2303) * Serialize Dictionary type to V8 object Handle Dictionary type, otherwise it will be treated as class/structs and cause exception. * Make the testcase covert IDictionary interface & ensure consistent bracket style. --- .../Serialization/V8Serialization.cpp | 12 ++++ CefSharp.Example/AsyncBoundObject.cs | 29 +++++++++ CefSharp.Example/Resources/BindingTest.html | 62 +++++++++++++++++++ 3 files changed, 103 insertions(+) diff --git a/CefSharp.Core/Internals/Serialization/V8Serialization.cpp b/CefSharp.Core/Internals/Serialization/V8Serialization.cpp index bc6dcbf37..c95b88e82 100644 --- a/CefSharp.Core/Internals/Serialization/V8Serialization.cpp +++ b/CefSharp.Core/Internals/Serialization/V8Serialization.cpp @@ -111,6 +111,18 @@ namespace CefSharp } list->SetList(index, subList); } + // Serialize dictionary to CefDictionary (key,value pairs) + else if (System::Collections::IDictionary::typeid->IsAssignableFrom(type)) + { + auto subDict = CefDictionaryValue::Create(); + auto dict = (System::Collections::IDictionary^) obj; + for each (System::Collections::DictionaryEntry kvp in dict) + { + auto fieldName = StringUtils::ToNative(Convert::ToString(kvp.Key)); + SerializeV8SimpleObject(subDict, fieldName, kvp.Value, seen); + } + list->SetDictionary(index, subDict); + } // Serialize class/structs to CefDictionary (key,value pairs) else if (!type->IsPrimitive && !type->IsEnum) { diff --git a/CefSharp.Example/AsyncBoundObject.cs b/CefSharp.Example/AsyncBoundObject.cs index 02e87aa17..074828637 100644 --- a/CefSharp.Example/AsyncBoundObject.cs +++ b/CefSharp.Example/AsyncBoundObject.cs @@ -3,6 +3,7 @@ // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. using System; +using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Text; @@ -98,5 +99,33 @@ namespace CefSharp.Example return builder.ToString(); } + + public Dictionary MethodReturnsDictionary1() + { + return new Dictionary() + { + {"five", 5}, + {"ten", 10} + }; + } + + public Dictionary MethodReturnsDictionary2() + { + return new Dictionary() + { + {"onepointfive", 1.5}, + {"five", 5}, + {"ten", "ten"}, + {"twotwo", new Int32[]{2, 2} } + }; + } + + public Dictionary MethodReturnsDictionary3() + { + return new Dictionary() + { + {"data", MethodReturnsDictionary2()} + }; + } } } diff --git a/CefSharp.Example/Resources/BindingTest.html b/CefSharp.Example/Resources/BindingTest.html index 333cd071e..cbe4e4aba 100644 --- a/CefSharp.Example/Resources/BindingTest.html +++ b/CefSharp.Example/Resources/BindingTest.html @@ -327,6 +327,68 @@ }); }); + QUnit.test( "Async call (methodReturnsDictionary):", function( assert ) + { + function deepEqual(x, y) + { + if ((typeof x == "object" && x !=null) && (typeof y == "object" && y !=null)) + { + for (var prop in x) + { + if (prop in y && (Object.keys(x).length === Object.keys(y).length)) + { + return deepEqual(x[prop], y[prop]); + } + else + { + return false; + } + } + } + else + { + return x === y; + } + } + + const dict1 = + { + "five": 5, + "ten": 10 + }; + const dict2 = + { + "onepointfive": 1.5, + "five": 5, + "ten": "ten", + "twotwo": [2, 2] + }; + const dict3 = + { + "data": + { + "onepointfive": 1.5, + "five": 5, + "ten": "ten", + "twotwo": [2, 2] + } + }; + + var asyncCallback = assert.async(); + Promise.all([ + boundAsync.methodReturnsDictionary1(), + boundAsync.methodReturnsDictionary2(), + boundAsync.methodReturnsDictionary3() + ]).then(function(results) + { + assert.ok( deepEqual(results[0], dict1), "Call to bound.MethodReturnsDictionary1() resulted in : " + JSON.stringify(results[0]) ); + assert.ok( deepEqual(results[1], dict2), "Call to bound.MethodReturnsDictionary2() resulted in : " + JSON.stringify(results[1]) ); + assert.ok( deepEqual(results[2], dict3), "Call to bound.MethodReturnsDictionary3() resulted in : " + JSON.stringify(results[2]) ); + asyncCallback(); + }); + + }); + QUnit.test("Javascript Callback Test using object as parameter", function( assert ) { var asyncCallback = assert.async();