I have a .net Webservice which should communicate with a Java app via json.
Now I have a method on the server side that looks like this:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public DateTime GetDate(DateTime input)
{
return input;
}
from a C# app I can send a receive DateTime value. It's a convention that Date values are serialized as:
\/Date(1279176056000)\/
where the number is defined as seconds since epoch. So if I want to call that service my json request string must look like this:
{"input":"\/Date(1279176056000)\/"}
Howevery, I don't know how to achive that with the json.org.* classes on the Java side.
The problem: If I use this code:
JSONObject json = new JSONObject();
json.put("input", "\\/Date(1279176056000)\\/");
the JSONObject is smart enough to escape the string itself before sending it through the wire, so I get:
{"input":"\\/Date(1279176056000)\\/"}
which results in an Exception during server side deserialisation:
System.FormatException: \/Date(1279183256000)\/ is not a valid value for DateTime
bei System.ComponentModel.DateTimeConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
bei System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)
bei System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)
bei System.Web.Script.Serialization.ObjectConverter.ConvertObjectToType(Object o, Type type, JavaScriptSerializer serializer)
bei System.Web.Script.Services.WebServiceMethodData.StrongTypeParameters(IDictionary`2 rawParams)
bei System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, IDictionary`2 parameters)
bei System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)
bei System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)
Long story short: How can I pass a backslash as a parameter to JSONObject without having it escaped?
Well, you might think I just just build the JSON string myself but I really want to send and receive more complex objects/arrays containing Date properties and I don't want to handle the whole JSON generation myself.