Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
C# ASP
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
JsonNetIHtmlStringTesting Project was missing the controller file.
packages Import of testing code.
.gitattributes Import of testing code.
.gitignore Import of testing code.
JsonNetIHtmlStringTesting.sln Import of testing code.
LICENSE Added LICENSE.
README.markdown

README.markdown

Default .NET JavaScript serialization

// Controller action method
public JsonResult Default() {
    return Json(new {
        String = test,
        IHtmlString = MvcHtmlString.Create(test),
    }, JsonRequestBehavior.AllowGet);
}

// JSON output
{
    "String": "some <span>test</span>.",
    "IHtmlString": {}
}

Default Json.NET serialization

// Controller action method
public StandardJsonNetJsonResult StandardJsonNet() {
    return new StandardJsonNetJsonResult(new {
        String = test,
        IHtmlString = MvcHtmlString.Create(test),
    }, JsonRequestBehavior.AllowGet);
}

// JSON output
{
    "String": "some <span>test</span>.",
    "IHtmlString": {}
}

Custom Json.NET serialization (with IHtmlStringConverter)

// Custom JsonConverter method
public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) {
    IHtmlString source = value as IHtmlString;
    if (source == null) {
        return;
    }

    writer.WriteValue(source.ToString());
}

// Controller action method
public CustomJsonNetJsonResult CustomJsonNet() {
    return new CustomJsonNetJsonResult(new {
        String = test,
        IHtmlString = MvcHtmlString.Create(test),
    }, JsonRequestBehavior.AllowGet);
}

// JSON output
{
    "String": "some <span>test</span>.",
    "IHtmlString": "some <span>test</span>."
}
Something went wrong with that request. Please try again.