1

I'd like to store map bounds in a JSON object. For code completion, I've declared the object using JSDoc:

/**
 * @name MapBounds
 * @class MapBounds
 * This class represents a map boundary definition.
 *
 * @property {number} minLat Specifies the minimum latitude of the boundary.
 * @property {number} minLon Specifies the minimum longitude of the boundary.
 * @property {number} maxLat Specifies the maximum latitude of the boundary.
 * @property {number} maxLon Specifies the maximum longitude of the boundary.
 */

When it comes time to create the MapBounds object, my code looks similar to this (this example doesn't have real lat/lon values):

var newMapBounds = MapBounds( {minLat: 50, minLon: 50, maxLat: 100, maxLon: 100} );

I'm specifying the object using JSON and then casting it to the type I need. Is there a better way to do this?

Thanks.

5
  • 5
    There are no "type casts" in JavaScript. That's not really JSON; it's the native JavaScript object literal syntax (from which JSON was derived). When it's directly part of the JavaScript source, it's just JavaScript. Commented Jan 25, 2013 at 20:13
  • Good to know. Is my code alright or could it use improvement? Commented Jan 25, 2013 at 20:15
  • Well I guess it's OK; there's nothing glaringly wrong or anything. It's not super-clear what it is you're doing, but generally that sort of thing is quite idiomatic in JavaScript. Commented Jan 25, 2013 at 20:16
  • Is the code var newMapBounds = MapBounds( {minLat: 50, minLon: 50, maxLat: 100, maxLon: 100} ); in JavaScript, or some other language? Commented Jan 25, 2013 at 20:38
  • Using a JavaScript literal (which is basically a JSON object) should suffice, no? Commented Jan 25, 2013 at 22:50

2 Answers 2

1

Here's how you would construct a Json Object with that format (it's just a JavaScript object literal)

{
 minLat: 0, // Specifies the minimum latitude of the boundary
 minLon: 0, // Specifies the minimum longitude of the boundary
 maxLat: 0, // Specifies the maximum latitude of the boundary
 maxLon: 0  // Specifies the maximum longitude of the boundary
}

It's very typical to have to "Map" Json objects to classes when submitting it to a web service. Some frameworks (i.e. MVC .NET) will do this for you if you pass a JSON object to the request and provide a object type as the web service parameter. None the less, the above format is how you would represent that structure on the JavaScript side of things.

Edit: This type of mapping from a JSON object to a specified class on the server-side code either happens by the framework you are using on your server (i.e. .NET / MVC) or you manually do it on the server. Once you submit your JSON object to the server, server-side code takes over and you shouldn't need to document that side of things on the client-side of things (i.e. JavaScript)

Sign up to request clarification or add additional context in comments.

2 Comments

Is there JSDoc I'd add to that so that my IDE has code completion on it?
I added an Edit, if that doesn't answer your question let me know
0

First of all there are no classes in JavaScript. Objects are created by calling functions to initialize the objects and inheritance is based on a prototype chain. See https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Inheritance_and_the_prototype_chain

JSON is only capable to store plain objects without a prototype chain, that is all objects encoded in JSON do only inherit from Object.

Thus one can conclude the only way to create objects of certain types from JSON is to use self-written functions which process the plain objects that result from deserializing JSON. The self-written function would then call the constructor functions using new … to rebuild the prototype chain. This seems to be what you are doing already and that's the way go.

You could use some framework that performs the steps mentioned above to save yourself from handling serialization manually.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.