In VS2012 it is possible to paste JSON as c# classes via context menu -> paste special.
Is there a solution to do it the other way around? Pasting a c# class as JSON?
I think many developers first code the server code for example with EF DBContext and then the client side javascript code. It is complicated always to write the json by hand.
An example:
// a model class
public class SampleTodoItem
{
public int Id { get; set; }
public string Description { get; set; }
public bool IsDone { get; set; }
}
then i want to have the same model as json in my javascript client code. Of course there has to be dummy data in the generated JSON.
var model = {
id: 0,
description: 'dummy',
isDone: false
};
I always have to do it manually what sucks.
As far as i understand i need these "JSON-models" to bind javascript data to html view with knockout or angularjs. Also possible that i don't understand the whole thing..
What it does. Neutralises oxidised sebum and proteins to revive greyish, greenish and dull skin undertones. Scavenges free radicals, provides natural sunscreen against UVA and UVB and regulates melanin production. Helps Collagen production and protects Elastin.
Ethyl Ascorbic Acid is a water-soluble, stable vitamin C derivative and highly effective antioxidant, improves collagen synthesis and reduces wrinkles.
Apply 3 to 4 drops to a freshly cleansed face, neck and décolletage each morning. Allow the serum to absorb into the skin before applying your favourite Alpha-H broad-spectrum moisturising SPF.
There are various approaches you can take. If you are planning to convert the c# classes to json representation, the easiest way is to serialize the data representation using something like a Javascript serializer or JSON.Net. Here is an example that converts a given IEnumerable to its JSON representation
public static string ToJSONString<T>(this IEnumerable<T> items)
{
var jsonString = JsonConvert.SerializeObject(items, Formatting.Indented, new JsonSerializerSettings
{
ContractResolver = new LowerCaseContractResolver(),
NullValueHandling = NullValueHandling.Ignore
});
jsonString = jsonString.Replace("\r\n", string.Empty);
return jsonString;
}
Another approach (this is for knockout viewmodels, is using T4 Templates to generate it. You can use the same concept to generate your representation if you plan to generate JavaScript files with your model. You can find it here http://www.codeproject.com/Articles/578827/Generate-Knockout-Viewmodels-using-T4-templates
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With