0

In my c# class I have the following class:

public class Product
{
    public int product_id;
    public int quantity;
    public string book_cost;
}

If I serialize it with JavaScriptSerializer then I get back

{"product_id":0,"quantity":0,"book_cost":"hello"}

after setting the values.

Thats c#, but how could I serialize a javascript function to match this?

I am wanting to parse a js function into a c# class

Example.... if I had a class in c# and I wanted to send it to some other webserver, I could serialize the object and make it into JSON using

var serializer = new JavaScriptSerializer();
serializer.Serialize(new Product());

public class Product
{
   public int product_id = 0;
   public int quantity = 0;
   public string book_cost = "hello";
}

then on the other side i could have this:

var serializer = new JavaScriptSerializer();
Product p = serializer.Deserialize<Product>(products);
Console.WriteLine(p.product_id);

public class Product
{
   public int product_id;
   public int quantity;
   public string book_cost;
}

and it would print out 0 because I created a new product class with those values from the other class, however I want one side to be js (the sending side) and the other to be c# (the receiving side)

here is more of an example:

js side...

I have this function, I have set the values to this, but I would change them at runtime.

function Product(){
    this.product_id = 0;
    this.quantity = 0;
    this.book_cost = "hello";   
}

then I would want to generate from this I would want to generate

{"product_id":0,"quantity":0,"book_cost":"hello"}

Then on the other side I would want to populate the Product class that is above. I am only going to be using strings, integers, and date objects. If I serialize a DateTime object in c# it comes out to be

{"date":"\/Date(-62135596800000)\/"}
Ian R. O'Brien
  • 6,682
  • 9
  • 45
  • 73
FabianCook
  • 20,269
  • 16
  • 67
  • 115
  • dupe: http://stackoverflow.com/questions/6487167/deserialize-from-json-to-javascript-object – albertjan Dec 18 '12 at 21:25
  • not duplicate. This is serializing a js function TO JSON not parsing JSON – FabianCook Dec 18 '12 at 21:26
  • Also, I want to parse a js function to c# function – FabianCook Dec 18 '12 at 21:27
  • 4
    Can you give an example? I'm not sure what you are asking here. – jrummell Dec 18 '12 at 21:27
  • 2
    @SmartLemon - I think what you're looking for is the [ASP.NET Ajax Framework](http://www.asp.net/ajax), though you could also use WCF. Both will serialize between JavaScript objects and C# objects, allowing you to work with data between both tiers. – Mike Christensen Dec 18 '12 at 21:30
  • jrummell isnt the op ^ xD. Also example up. – FabianCook Dec 18 '12 at 21:32
  • Are you looking for [JSON.stringify](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/JSON/stringify)? – Snuffleupagus Dec 18 '12 at 21:33
  • 1
    Ok now I'm even more confused.. – Mike Christensen Dec 18 '12 at 21:33
  • 1
    im so confused by what you want? – Stan R. Dec 18 '12 at 21:34
  • Writing more of an example now – FabianCook Dec 18 '12 at 21:41
  • @SmartLemon - If you're trying to generate proxy objects, you can't really do that from a JSON string as it doesn't include full type information. What you might be looking for is WSDL, which can describe an interface contract for a web service. ASP.NET is capable of generating proxies in both C# and Javascript from a WSDL file. – Mike Christensen Dec 18 '12 at 21:41
  • Have a look at my new edit to see what I want xD. – FabianCook Dec 18 '12 at 21:48
  • @SmartLemon - Looks fine to me. As long as the fields all match up, you can deserialize JSON generated from a JavaScript object to a class in C#. Is there something specific that isn't working with what you have? – Mike Christensen Dec 18 '12 at 21:52
  • The question is HOW do I serialize it. – FabianCook Dec 18 '12 at 21:52
  • @SmartLemon - On the JavaScript side? Just use [stringify](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/JSON/stringify) (Note: Many JavaScript frameworks also have JSON helper functions) – Mike Christensen Dec 18 '12 at 21:55
  • That comes back with `{"product_id":0,"quantity":0,"book_cost":"","date":"2012-12-18T21:57:30.479Z"}` I guess this is fine. I will just format the date into the right format and parse it at the other side :). Could you put it in an answer? – FabianCook Dec 18 '12 at 21:59

1 Answers1

2

You can serialize an object in JSON using the JSON.stringify method. Something like:

function Product() {
    this.product_id = 0;
    this.quantity = 0;
    this.book_cost = "hello";   
}

var p = new Product();
p.quantity = 5;
var json = JSON.stringify(p);

Example

Don't worry about date formats. Any good serializer is will be able to recognize various representations of dates. There should be no need to reformat anything by hand.

Mike Christensen
  • 88,082
  • 50
  • 208
  • 326