0

I have the following class structure. If I use the same assembly of contracts (assembly with all data classes n other stuff) used by api, I am unable to deserialize it in my application.

But I need to remove the dependency of that assembly. So I created another assembly with same namespace and contracts.

Example: I have many parts, and I can have a car out of those parts or a motorcycle.

public class Car
{
 public string Name {get; set;}
 public Dictionary<string,Part> SpareParts {get; set;}
}

public class Part
{
public string PartName {get; set;}
public object Value {get;set;}
public string Type {get;set}
}

Now, I have a Generic Query request and response

public class QueryRequest
{
  //Stuff.. Request works fine.. i get a response
}

public class QueryResponse
{
   public QueryResult[] Result;
}


public classs QueryResult
{
//Other details like entity name n stuff for car or motorcycle etc.
 public Part[] Parts;
}

Remember that Part Class with Value field, that contains List It goes like this

public class abstract Spoke
{
}

public class Spoke1 : Spoke
{
}

public class Spoke2 : Spoke
{
}

In my response I get this weird serialized string

"Value": {
            "$type": "System.Collections.Generic.List`1[[namespace.Spoke, namespace]], mscorlib",
            "$values": [
              {
                "$type": "namespace.spoke2, namespace",
               //Other Properties of spoke2
              }]
        }

EDIT: I get the data in format of QueryResponse which contains QueryResult n Parts

I m using Newtonsoft.Json, Any ideas how to deserialize that into my namespace2.Spoke I have the whole contract copied in another assembly. I have tried changing the namespace same as in response but doesn't work.

Any suggestions of help please.

Akanksha Gaur
  • 2,636
  • 3
  • 26
  • 50
  • 1
    The serialized string contains the type information - i.e. `TypeNameHandling = TypeNameHandling.Objects` in the [serializer settings](http://www.newtonsoft.com/json/help/html/serializetypenamehandling.htm). – stuartd Aug 02 '16 at 12:27
  • 1
    I've found that serializing C# dictionaries to Json with serializers (mainly JsonConvert) sometimes gives a heap of problems - which looks like what you're experiencing. It is still possible to convert a Dictionary to Json - see this question, here: http://stackoverflow.com/questions/5597349/how-do-i-convert-a-dictionary-to-a-json-string-in-c – Geoff James Aug 02 '16 at 12:38

0 Answers0