Skip to content

Commit 1542fa3

Browse files
WarWithinMeamaitland
authored andcommitted
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.
1 parent 58e7052 commit 1542fa3

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

CefSharp.Core/Internals/Serialization/V8Serialization.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,18 @@ namespace CefSharp
111111
}
112112
list->SetList(index, subList);
113113
}
114+
// Serialize dictionary to CefDictionary (key,value pairs)
115+
else if (System::Collections::IDictionary::typeid->IsAssignableFrom(type))
116+
{
117+
auto subDict = CefDictionaryValue::Create();
118+
auto dict = (System::Collections::IDictionary^) obj;
119+
for each (System::Collections::DictionaryEntry kvp in dict)
120+
{
121+
auto fieldName = StringUtils::ToNative(Convert::ToString(kvp.Key));
122+
SerializeV8SimpleObject(subDict, fieldName, kvp.Value, seen);
123+
}
124+
list->SetDictionary(index, subDict);
125+
}
114126
// Serialize class/structs to CefDictionary (key,value pairs)
115127
else if (!type->IsPrimitive && !type->IsEnum)
116128
{

CefSharp.Example/AsyncBoundObject.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
44

55
using System;
6+
using System.Collections;
67
using System.Collections.Generic;
78
using System.Diagnostics;
89
using System.Text;
@@ -98,5 +99,33 @@ public string DynamiObjectList(IList<dynamic> objects)
9899

99100
return builder.ToString();
100101
}
102+
103+
public Dictionary<string, int> MethodReturnsDictionary1()
104+
{
105+
return new Dictionary<string, int>()
106+
{
107+
{"five", 5},
108+
{"ten", 10}
109+
};
110+
}
111+
112+
public Dictionary<string, object> MethodReturnsDictionary2()
113+
{
114+
return new Dictionary<string, object>()
115+
{
116+
{"onepointfive", 1.5},
117+
{"five", 5},
118+
{"ten", "ten"},
119+
{"twotwo", new Int32[]{2, 2} }
120+
};
121+
}
122+
123+
public Dictionary<string, IDictionary> MethodReturnsDictionary3()
124+
{
125+
return new Dictionary<string, IDictionary>()
126+
{
127+
{"data", MethodReturnsDictionary2()}
128+
};
129+
}
101130
}
102131
}

CefSharp.Example/Resources/BindingTest.html

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,68 @@
327327
});
328328
});
329329

330+
QUnit.test( "Async call (methodReturnsDictionary):", function( assert )
331+
{
332+
function deepEqual(x, y)
333+
{
334+
if ((typeof x == "object" && x !=null) && (typeof y == "object" && y !=null))
335+
{
336+
for (var prop in x)
337+
{
338+
if (prop in y && (Object.keys(x).length === Object.keys(y).length))
339+
{
340+
return deepEqual(x[prop], y[prop]);
341+
}
342+
else
343+
{
344+
return false;
345+
}
346+
}
347+
}
348+
else
349+
{
350+
return x === y;
351+
}
352+
}
353+
354+
const dict1 =
355+
{
356+
"five": 5,
357+
"ten": 10
358+
};
359+
const dict2 =
360+
{
361+
"onepointfive": 1.5,
362+
"five": 5,
363+
"ten": "ten",
364+
"twotwo": [2, 2]
365+
};
366+
const dict3 =
367+
{
368+
"data":
369+
{
370+
"onepointfive": 1.5,
371+
"five": 5,
372+
"ten": "ten",
373+
"twotwo": [2, 2]
374+
}
375+
};
376+
377+
var asyncCallback = assert.async();
378+
Promise.all([
379+
boundAsync.methodReturnsDictionary1(),
380+
boundAsync.methodReturnsDictionary2(),
381+
boundAsync.methodReturnsDictionary3()
382+
]).then(function(results)
383+
{
384+
assert.ok( deepEqual(results[0], dict1), "Call to bound.MethodReturnsDictionary1() resulted in : " + JSON.stringify(results[0]) );
385+
assert.ok( deepEqual(results[1], dict2), "Call to bound.MethodReturnsDictionary2() resulted in : " + JSON.stringify(results[1]) );
386+
assert.ok( deepEqual(results[2], dict3), "Call to bound.MethodReturnsDictionary3() resulted in : " + JSON.stringify(results[2]) );
387+
asyncCallback();
388+
});
389+
390+
});
391+
330392
QUnit.test("Javascript Callback Test using object as parameter", function( assert )
331393
{
332394
var asyncCallback = assert.async();

0 commit comments

Comments
 (0)