c# - Json.NET deserialise to interface implementation -


i'm trying use json.net serialise class transfer via http request. client server test program shares common class files. files interface (itestcase) , implementations of interface (testpicture, testvideo).

serialising , deserialising testcase below within same application works fine, presumably because json.net code contained within 1 assembly. when serialise testcase, send server, try deserialise, error

"error resolving type specified in json 'com.test.testcases.testpicture, graphics tester'. path '$type', line 2, position 61"

with inner exception of type jsonserializationexception message

"could not load assembly 'graphics tester'."

in json.net documentation, when json generated $type value "$type": "newtonsoft.json.samples.stockholder, newtonsoft.json.tests". second parameter seems reference relevant classes namespace, rather project name (namely graphics tester) happening in instance.

given both projects share requisite classes , files in same namespace, why json.net looking assembly rather class itself?

below skeleton of code; details ommited don't assist problem. shown interface, , 2 implementations of interface. shown serialisation , deserialisation operations, , resulting json.

itestcase testcase = new testpicture("test.jpg")  string json = jsonconvert.serializeobject(testcase, formatting.indented, new jsonserializersettings {     typenamehandling = typenamehandling.objects, });  itestcase instance = jsonconvert.deserializeobject<itestcase>(json, new jsonserializersettings {     typenamehandling = typenamehandling.objects, });   //value in variable json after serialisation //{ //  "$type": "com.test.testcases.testpicture, graphics tester", //  "filename": "test.jpg", //  "testpoints": null //} 

individual class files:

namespace com.test.testcases {     interface itestcase     {         void run();         bool verify();     } }  namespace com.test.testcases {     class testpicture : itestcase {} }  namespace com.test.testcases {     class testvideo : itestcase {} } 

my solution:

a simpler solution existed expected. wasn't optimal, works. i'd class more workaround or hack. altering project properties , setting assembly name same in both projects, find classes included in project, , create objects specified json string.

the $type encodes fully-qualified type name, differ between client , server if they're not using same project define these types (i.e. if each defines type rather referencing common assembly).

in case, in client fully-qualified name com.test.testcases.testpicture, graphics tester, when gets server server can't find assembly called graphics tester.

you can solve in 1 of 2 ways:

  1. define common assembly serializable types, , reference both client , server.
  2. define types separately both in client , server, , customize json.net's type resolving providing custom serializationbinder jsonserializersettings.binder, implement own logic.

Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -