1

I tried tro return date in ISO format using Json.Net from ASP.NET MVC4 controller

public JsonResult Sales() {
  var saleList = new List<Sale>();

  ... 
        var str = JsonConvert.SerializeObject(saleList);
        return Json(str, JsonRequestBehavior.AllowGet);
        }

public class Sale
{
    public DateTime saledate { get; set; }
    ...
}

But it returns whole object json notation as single string.

How to return date in ISO format as json object ?

2
  • Are you free to use ServiceStack serializer? Commented Nov 17, 2013 at 20:40
  • I can use any free software in C# Commented Nov 17, 2013 at 20:41

2 Answers 2

1

You can do it with ServiceStack JSON serializer but first you have to integrate it to ASP.NET MVC.

After installing the package, configure DateTime serialization in application start:

JsConfig.DateHandler = JsonDateHandler.ISO8601;

Create an ActionResult type for JSON content:

public class CustomJsonResult : ActionResult
{
    private readonly object _data;
    private readonly string _content;
    private readonly Encoding _encoding;

    public CustomJsonResult(object data) : this(data, null, null) { }

    public CustomJsonResult(object data, string content) : this(data, content, null) { }

    public CustomJsonResult(object data, Encoding encoding) : this(data, null, encoding) { }

    public CustomJsonResult(object data, string content, Encoding encoding)
    {
        _data = data;
        _content = content;
        _encoding = encoding;
    }

    public override void ExecuteResult(ControllerContext context)
    {
        if (context == null)
        {
            throw new ArgumentNullException("context");
        }
        HttpResponseBase response = context.HttpContext.Response;
        response.ContentType = string.IsNullOrEmpty(_content) ? "application/json" : _content;

        if (_encoding != null)
        {
            response.ContentEncoding = _encoding;
        }

        response.Write(JsonSerializer.SerializeToString(_data));
    }
}

Then you can add these methods to a base controller:

protected CustomJsonResult CustomJson(object data)
{
    return new CustomJsonResult(data);
}

protected CustomJsonResult CustomJson(object data, string content)
{
    return new CustomJsonResult(data, content);
}

protected CustomJsonResult CustomJson(object data, Encoding encoding)
{
    return new CustomJsonResult(data, encoding);
}

protected CustomJsonResult CustomJson(object data, string content, Encoding encoding)
{
    return new CustomJsonResult(data, content, encoding);
}

At last you can return the result like this:

return CustomJson(saleList);
Sign up to request clarification or add additional context in comments.

2 Comments

This looks to complicated. Maybe it is possible to write Json() replacement which uses this converter ?
@Andrus Maybe but I think this is the proper way to use SS serializer in an MVC app.
0

You can set settings when using an overload to SerializeObject that takes an JsonSerializerSettings parameter:

public static string SerializeObject(
    Object value,
    JsonSerializerSettings settings
)

The JsonSerializerSettings have a property called DateFormatHandlingused to distinguish between Microsoft Format and ISO format.

You could also use a custom converter in JSON.NET. Custom Converters can be applied using the CustomConverter attribute.

An example can be found in the JSON.NET documentation: http://james.newtonking.com/json/help/index.html

I would prefer the first possibility.

3 Comments

I can create json string but how to return it from json controller ? Uing return Json(.. ) Json() does not accept json string.
If you use JSON.NET in your MVC project you can set the settings globally. See stackoverflow.com/questions/13274625/…
I updated question. using Json.NET returns object as single string.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.