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

Array of Custom Objects fails #5

Open
GoogleCodeExporter opened this issue Jun 30, 2015 · 1 comment
Open

Array of Custom Objects fails #5

GoogleCodeExporter opened this issue Jun 30, 2015 · 1 comment

Comments

@GoogleCodeExporter
Copy link

I have a custom object - and I want to return an array of the object as a
JSON string:

public class custom_obj
{
  private int _age;
  public int age
  {
    get { return this._age; }
    set { this._age = value; }
  }
}

custom_obj co;

List<custom_obj> co_list = new List<custom_obj>();

for (int i = 0; i < 5; ++i)
{
  co = new custom_obj();
  co.age = i;
  co_list.Add(co);
}
JSONReflector json = new JSONReflector(co_list);
Console.WriteLine(json.ToString());

Here's what I would expect:
[ {"age":0},{"age":1},{"age":2},{"age":3},{"age":4}]

Instead, I get an Exception:
"System.Reflection.TargetParameterCountException: Parameter count mismatch."
[TargetParameterCountException: Parameter count mismatch.]
   System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags
invokeAttr, Binder binder, Object[] parameters, CultureInfo culture,
Boolean skipVisibilityChecks) +2754022
   System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags
invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +29
   System.Reflection.RuntimePropertyInfo.GetValue(Object obj, BindingFlags
invokeAttr, Binder binder, Object[] index, CultureInfo culture) +55
   System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[]
index) +18
   JSONSharp.JSONReflector..ctor(Object objValue) +167


If I create the JSONReflector on a *single instance* of custom_obj - it
works just fine.  It just can't seem to handle an array of custom_obj objects.

Is this a limitation of JSONSharp or am I doing it wrong?

Original issue reported on code.google.com by jtre...@gmail.com on 6 Oct 2009 at 4:43

@GoogleCodeExporter
Copy link
Author

You neeed a wrapper for your list, then you can call it like this:

Wrapper wrapper = new Wrapper();
wrapper.CustomObjects = co_list.ToArray();

JSONReflector json = new JSONReflector(wrapper);
Console.WriteLine(json.ToString());

*****

public class Wrapper
{
  private custom_obj[] _customObjects;

  public custom_obj[] CustomObjects
{
get
{
   return _customObjects;
}
set
{
   _customObjects = value;
}

}
}


Original comment by len...@gmail.com on 5 Nov 2009 at 2:11

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

No branches or pull requests

1 participant