4

I am trying to adhere to the naming conventions in JavaScript as well as in C#. This leads to interesting issues when passing JSONized data back and forth. When I access an x/y coordinate client-side I expect the property to be lowercase, but server-side it is uppercase.

Observe:

public class ComponentDiagramPolygon
{
    public List<System.Drawing.Point> Vertices { get; set; }

    public ComponentDiagramPolygon()
    {
        Vertices = new List<System.Drawing.Point>();
    }
}

public JsonResult VerticesToJsonPolygon(int componentID)
{
    PlanViewComponent planViewComponent = PlanViewServices.GetComponentsForPlanView(componentID, SessionManager.Default.User.UserName, "image/png");
    ComponentDiagram componentDiagram = new ComponentDiagram();

    componentDiagram.LoadComponent(planViewComponent, Guid.NewGuid());

    List<ComponentDiagramPolygon> polygons = new List<ComponentDiagramPolygon>();

    if (componentDiagram.ComponentVertices.Any())
    {
        ComponentDiagramPolygon polygon = new ComponentDiagramPolygon();
        componentDiagram.ComponentVertices.ForEach(vertice => polygon.Vertices.Add(vertice));
        polygons.Add(polygon);
    }

    return Json(polygons, JsonRequestBehavior.AllowGet);
}

I understand that if I am able to use the C# attribute 'JsonProperty' to customize naming conventions. Yet, as far as I can tell, this is only applicable to classes which I am owner of.

How may I change System.Drawing.Point's properties when passing back to the client?

5
  • If you are using JsonProperty then you are using JSON.NET, and not the stock JavaScriptSerializer; is that so?
    – Kirk Woll
    Feb 14, 2012 at 22:35
  • Yes. The reference to JSON.NET is already included into the project -- I just have not utilized it (yet) in this scenario. Feb 14, 2012 at 22:38
  • This seems similar to this question from a couple of days ago: stackoverflow.com/questions/9247478/…. Feb 14, 2012 at 22:48
  • @AvnerShahar-Kashtan Thank you. I have read that post now -- it seems to imply that best practice says to leave the capitalization alone. Is that correct? Feb 14, 2012 at 22:53
  • 2
    I'd say leave it alone. While I completely understand your motivation, you can also argue that since it's a representation the C# model, it should stay true to that. I.e. it's not so much that that the client-side receives JSON data, but rather that it receives C# data. That it happens to be JSON is largely irrelevant, as it could just as easily be XML or some binary format. Just pretend you're accessing the C# data directly, and ignore the specifics of how it's transmitted.
    – Flambino
    Feb 15, 2012 at 0:00

2 Answers 2

2

You can cheat, by projecting into a new anonymous type:

var projected = polygons.Select(p => new { Vertices = p.Vertices.Select(v => new { x = v.X, y = v.Y }) });

return Json(projected, JsonRequestBehavior.AllowGet);
0

How about writing your own Json.NET based converter:

public class NJsonResult : ActionResult
{
    private object _obj { get; set; }

    public NJsonResult(object obj)
    {
        _obj = obj;
    }

    public override void ExecuteResult(ControllerContext context)
    {
        context.HttpContext.Response.AddHeader("content-type", "application/json");
        context.HttpContext.Response.Write(
                JsonConvert.SerializeObject(_obj, 
                                            Formatting.Indented,
                                            new JsonSerializerSettings
                                                {
                                                    ContractResolver = new CamelCasePropertyNamesContractResolver()
                                                }));
    }
}

This will just work for your entire application, without properties renaming necessity (to lowercase) in your classes in following way: return Json(new { x = ..., y = ...});

And below is the usage example in controller action:

[AcceptVerbs(HttpVerbs.Get)]
public virtual NJsonResult GetData()
{
    var data = ...
    return new NJsonResult(data);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.