I’m looking for a way to render a C# class object to javascript. For instance this class definition:
public class Foo
{
public string Bar1 { get; set; }
public string Bar2 { get; set; }
}
should render (an object of class Foo with values filled) as:
foo:
{
bar1: 'bar1value',
bar2: 'bar2value'
}
I know my way around reflection, but before reinventing the wheel all over again I was wondering if there are any libraries already doing these kind of things.
This might do what you want: http://json.codeplex.com/
There’s an example as well: http://james.newtonking.com/projects/json/help/
If I recall, JSON demands quotes for key names (though probably tolerates a lack thereof), but I wasn’t sure about Javascript, so I tested it with the following HTML, which seems to be usable in Chrome, IE, and Firefox.
Hopefully, you’ll be able to use such syntax directly (RFC 4627 says that JSON is a subset of Javascript).