Marian Ban
Marian Ban

Reputation: 8168

Json data formatting best practice

I have a SPA web page. Data are loaded with ajax requests. I need to show some culture sensitive data on that page. What is the best way for formating this kind of data? Basicaly I have two options:

1. Send all data preformated to client (preformat on server)

Preformated
{
    DurrationInMinutes = '2 min'
    DurationInSeconds = '120 sec'
}

Pros:

Cons:

2. Send raw data to client (format on client)

RawData
{
        Durration = 1645678 // milliseconds        
}

Pros:

Cons:

Edit

I end up with client side option

Thanks

Upvotes: 0

Views: 416

Answers (2)

Atle
Atle

Reputation: 5457

For just simple presentation of the data, both methods are OK. But keep in mind a couple of other considerations:

  • All data sent to the client is in principle available for the user, even if it's not shown directly. This can in some situations be a security issue.
  • Sending the raw data to the client and letting the client handle the rest gives more possibilities for dynamic viewing on the client side
  • Depending on type of application and data, sending data to the client can reduce load on server. However users with slow clients can be an issue.

If none of these considerations apply, I would go with what is most optimal for developing and maintenance. The answer then depends on local issues, resources, framework used etc.

Upvotes: 3

Geeky Guy
Geeky Guy

Reputation: 9399

I don't understand the cons of the server side option - how is is that formatting things once will cause that? Specially when you can keep your localized data in resource files, and simply let the framework handle the localization with a simple configuration? And yes, AFAIK, pure Javascript doesn't have all the same localization capabilities of .NET (or Java, or any other framework, for that matter).

Also, the pros of the client side option - How is it that coding in the side your dev are less experienced helps unit testing and maintenance? Do you think that just because it's not on server side, it doesn't have to be tested to be ok?

My two cents: unless you find a Javascript framework that can do that, let the server side handle it. Otherwise you'll be reinventing the wheel. Also take into account that the more logic you have on the client side, the worse your site look-and-feel will be on low end machines, tablets and cell phones.

Upvotes: 1

Related Questions