Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Json deserializer List<string> #28

Closed
lukencode opened this issue May 28, 2010 · 7 comments
Closed

Json deserializer List<string> #28

lukencode opened this issue May 28, 2010 · 7 comments
Labels

Comments

@lukencode
Copy link

I am trying to deserialize a the json:

"types": [ "locality", "political" ]

Into a List. Attempting to do so throws - System.MissingMethodException: No parameterless constructor defined for this object.

Not sure if this is a bug or I am just doing it wrong.

@johnsheehan
Copy link
Contributor

Darn, I thought this worked. It's nothing you're doing wrong. Handling for this case needs to be added.

@bkaid
Copy link
Contributor

bkaid commented Jun 28, 2010

This bug seems to affect IList of any value types. I am running into the same problem serializing this array into a List: [14877833,13434092,88344083]. The result is a list of 3 longs. Also, if I serialize as a List, I get a null value.

@lukencode
Copy link
Author

If anyone is looking for a quick and dirty fix for this issue I pieced together (read copy/paste) a deserializer based of the existing one - http://gist.github.com/485545.

Testing on it is pretty minimal but it worked for the simple stuff I used it for.

@johnsheehan
Copy link
Contributor

I'm almost done with a fix for this as well. Should be committed in the next few days.

@johnsheehan
Copy link
Contributor

fixed!

@bluejack2000
Copy link

Same problem exists for XML deserializer List<string>.

My hotfix for this is:

private object CreateAndMap(Type t, XElement element)
{
      object item;
      if (t == typeof(System.String))
      {
        item = element.Value;        
      }
      else
      {
        item = Activator.CreateInstance(t);
        Map(item, element);        
      }
      return item;
}

@Subv
Copy link

Subv commented Jul 31, 2011

It still happens for user-created types, i've found a solution:

in JsonDeserializer.cs
under CreateAndMap

replace

instance = Activator.CreateInstance(type);
Map(instance, element);

with

instance = System.Runtime.Serialization.FormatterServices.GetUninitializedObject(type);
Map(instance, element);

and thats all :)

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants