views:

16

answers:

1

I've got a handful of public static WebMethods that I'm accessing with Javascript (directly), usually through jQuery. That's working, and I'm happy with the results (and with not having to deal with the AJAX framework).

What I'd like to be able to do is to define my own mock-JSON object to pass between JS and ASP (this is a .NET 2.0 web project, CS and VB, and I'm also referencing a 3.5 non-web project for when I need 3.5 services). But I'm having tons of problems:

  • I can't specify a [DataContract], it tells me I may be missing an assembly.
  • I can't figure out how to use an ISerializable interface, nor do I know if it will solve my problem or not. Specifically, adding things through GetObjectData doesn't seem to do squat when I use a JavaScriptSerializer; I always get "{}".
  • Tagging my class with [Serializable] doesn't seem to do anything.
  • I don't really know what other options are out there.

Some code:

[Serializable]
public class MyClass : ISerializable
{
    void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
    {
        info.AddValue("test", "contents");
    }
}

// in my ASP page
MyClass tmp = new MyClass();
JavaScriptSerializer s = new JavaScriptSerializer(); // System.Web.Script.Serialization.JavaScriptSerializer, to be specific.
s.Serialize(tmp) // "{}" whether here, or by returning a MyClass instance from my WebMethod function (as Object, MyClass, etc.)

I'm fairly new to CS/VB and .NET, so forgive me if I'm missing something painfully obvious, and that's why I can't find anything online ^^;

So, my plainly-stated question is: how can I make my own classes serializable in ASP with WebMethods?

A: 

Have you looked into maybe creating a WCF service? It has json serialization out-of-the-box.

CyberDude
Had not, no. After poking around, it looks like it's rather significantly larger / more complicated than my goals (a better / simplified JSON object, especially as .net 2 has none). Unless it's simpler than it looks? In any case, ASP.NET 2 does JSON serialization as well, I'd just rather not convert between their formats and mine for every single call (possibly hundreds) when it can / should be done directly.
Groxx
What about DataContractJsonSerializer?
CyberDude
I've got no System.Runtime.Serialization.Json in the 3.5 project... how would I get that?
Groxx