how can you serialize an object to JSON in .NET 2.0 using C#?
JSON.org has references to a number of serializers in a number of languages, including more than half a dozen in C#. You should be able to find one which meets your API and licensing needs including JSONsharp with the LGPL license and the well-designed Json.NET.
If what you're serializing is fairly simple, it's not all that hard to write your own for a specific purpose. The JSON.org site has the syntax, and it's very straight-forward.
You can use the JavaScriptSerializer class from ASP.NET Ajax 1.0, which is compatible with .NET 2.0.
Are you trying to build an RPC server in the .NET side? If so look at Jayrock (jayrock.berlios.de). You get the source code and it will compile under 2.0.
Also setting an RPC server is a snap:
using Jayrock; using Jayrock.JsonRpc; using Jayrock.JsonRpc.Web; using Jayrock.Json; using Jayrock.Json.Conversion;
namespace myRPCService { [JsonRpcService("Service")] public class Service : JsonRpcHandler {
[JsonRpcMethod("call", Idempotent = true)]
public string call(string value)
{
JsonObject oJSON = JsonConvert.Import(typeof(JsonObject), value) as JsonObject;
...
return oJSON.ToString();
}
}
}
Use the DataContractJsonSerializer class. Like this:
http://www.west-wind.com/WebLog/posts/218001.aspx
Edit: Oh, you are asking about .NET 2.0. Sorry.