5

I'm using fastJson library to deserialize a json string to a "Person" object. The Person class is defined below:

class Person
{
    public string type;
    public string id;
    public string name;
}

The Json string is:

[{
"type": "/basketball/basketball_player", 
"id": "/en/rasheed_wallace", 
"name": "Rasheed Wallace"
},
{
"type": "/basketball/basketball_player", 
"id": "/en/tayshaun_prince", 
"name": "Tayshaun Prince"
}]

When I use the code:

var obj = fastJSON.JSON.Instance.ToObject<List<Person>>(str);

It shows an unhandled exception that

Failed to fast create instance for type
'System.Collections.Generic.List`1[[JsonSample.Person, JsonSample, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null]]' from assemebly  
System.Collections.Generic.List`1[[JsonSample.Person, JsonSample, Version=1.0.0.0, 
Culture=neutral, PublicKeyToken=null]], mscorlib, Version=4.0.0.0, Culture=neutral, 
PublicKeyToken=b77a5c561934e089'

But everything works fine in Newtonsoft.Json library if I use the code:

var obj = JsonConvert.DeserializeObject<List<Person>>(str);

So is this a bug of fastJson or am I not using fastJson in a correct way?

ChandlerQ
  • 1,428
  • 2
  • 21
  • 29
  • @CuongLe Because the Json files I need to tackle are really large...Json.net is a little slow and according to [fastJson](http://www.codeproject.com/Articles/159450/fastJSON), it's fast~ – ChandlerQ Oct 15 '12 at 11:11

1 Answers1

5

It is because Person is not public. Change your class definition to

public class Person
{
    public string type;
    public string id;
    public string name;
}

I tried running your code as is and got the same exception. I modified Person to be public and the exception went away.

Mike Two
  • 44,935
  • 9
  • 80
  • 96
  • Yes, it works! But do you know why I should make Person Public? Thanks! – ChandlerQ Oct 15 '12 at 12:02
  • 3
    I had the same error message, but in my case the problem was lack of a default constructor. To fix my error I could add a default constructor, or use JSONParameters ParametricConstructorOverride as follows: `var newobj = fastJSON.JSON.ToObject(jsonText, new fastJSON.JSONParameters { ParametricConstructorOverride = true });` – Erik Sep 15 '16 at 21:37