开发者

ASP.NET MVC: Can I get Json() to make use of an object's implicit string operator?

I have a custom Type that has an implicit string operator as follows (simplified):

public clas开发者_开发问答s TransactionType
{
    private string StringValue;

    public static implicit operator string(TransactionType arg)
    {
        return arg.StringValue;
    }

    public override string ToString() // This doesn't work, even when
    {                                 // returning an arbitrary constant string
        return arg.StringValue;       // such as "Testing"
    }
}

I am trying to return a JsonResult as follows:

[HttpPost]
public JsonResult Foo()
{
   var transactionType = new TransactionType();
   return Json(transactionType); 
}

The JSON string that I get is "TransactionType":{} instead of the value of StringValue

Is there an easy way to have Json() make use of the implicit string operator?

Thanks!

Edit: As indicated below, overriding ToString() also didn't work. I still just get an empty object "TransactionType":{} instead of the obejct's string representation.


ToString is the method that is being called just about anywhere in order to get string representations.

You should be overriding ToString in your class instead of using an implicit string operator.


Override ToString instead of defining your own string operator. Or do both. Have one call the other one.


Typically you'd want this to serialize to an object with a property and value, though I suppose you could simply return a string. If you're returning a string, you don't need JSON, you can use html or text -- either will work (return Content( transactionType.ToString() );) If you do want JSON, then I'd suggest simply setting the object up with a property.

public class TransactionType
{
    public string Value { get; set; }
} 

Then you can do exactly as you are trying:

[HttpPost]
public JsonResult Foo()
{
   var transactionType = new TransactionType { Value = "foo" };
   return Json(transactionType); 
}

This will serialize to { "Value" : "foo" }

Note as of MVC3, JsonResult still uses JavaScriptSerializer. If the property needs to be private you could use a DataContractJsonSerializer, mark the property as a DataMember (even if private), serialize the result, then return that string using a ContentResult, but with MIME type "application/json".

public class TransactionType
{
    public TransactionType( string type )
    {
        Value = type;
    }

    [DataMember]
    private string Value { get; set; }
    ...
} 

Then you can (after changing the return type), create a method to do the serialization and return the content result that is analogous to the Json() method.

[HttpPost]
public ActionResult Foo()
{
   var transactionType = new TransactionType( "foo" );
   return DataContractJson(transactionType); 
}

private ActionResult DataContractJson<T>( T obj ) where T : class
{
     using (var stream = new MemoryStream())
     {
         var serializer = new DataContractJsonSerializer(typeof(T));
         serializer.WriteObject( stream, obj );
         var content = Encoding.UTF8.GetString( stream.ToArray() );
         return Content( content, "application/json" );
     }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜