mirror of
https://github.com/cefsharp/CefSharp.git
synced 2026-12-05 03:32:37 +00:00
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.
This commit is contained in:
@@ -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)
|
||||
{
|
||||
|
||||
@@ -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<string, int> MethodReturnsDictionary1()
|
||||
{
|
||||
return new Dictionary<string, int>()
|
||||
{
|
||||
{"five", 5},
|
||||
{"ten", 10}
|
||||
};
|
||||
}
|
||||
|
||||
public Dictionary<string, object> MethodReturnsDictionary2()
|
||||
{
|
||||
return new Dictionary<string, object>()
|
||||
{
|
||||
{"onepointfive", 1.5},
|
||||
{"five", 5},
|
||||
{"ten", "ten"},
|
||||
{"twotwo", new Int32[]{2, 2} }
|
||||
};
|
||||
}
|
||||
|
||||
public Dictionary<string, IDictionary> MethodReturnsDictionary3()
|
||||
{
|
||||
return new Dictionary<string, IDictionary>()
|
||||
{
|
||||
{"data", MethodReturnsDictionary2()}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user