Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON: better define a type of object inside or outside the object?

Context

We are building a JSON API for web (HTML+JS) and mobile (iOS/Android/Windows).

Server needs to send data with a base structure and a variable structure. In our example, the base structure includes "name" and "description", the variable structure is called "template" and have different fields depending on its type. We figured out at least three ways to write it (there may be more):

A: variable structure type defined outside the object

{
  "id": "A001",
  "name": "My First Game",
  ...,
  "template_type": "BATTLE",
  "template": {
    ...
  }
}

In this scenario, the client should look at "template_type" in order to determine how to parse "template". The "template" object alone is not self-sufficient to know what it is.

B: variable structure type defined inside the object

{
  "id": "A001",
  "name": "My First Game",
  ...,
  "template": {
    "type": "BATTLE",
    ...
  }
}

In this scenario, the client should look at "type" inside "template" in order to determine how to parse "template". The "template" object alone is self-sufficient to know what it is.

C: variable structure type defined by the key of the object

{
  "id": "A001",
  "name": "My First Game",
  ...,
  "template_battle": {
    ...
  }
}

In this scenario, the client should look at all keys ("template_battle", "template_puzzle", ...) in order to determine which type of game we have. The "template_battle" object alone is self-sufficient to know what it is, because it would always be the "BATTLE" type.

Question

Any recommendation on which JSON solution is the most client friendly for web and mobile to parse and use? (you can propose other solutions)

like image 905
Cœur Avatar asked Nov 02 '16 09:11

Cœur


People also ask

What is object type in JSON?

Objects are the mapping type in JSON. They map “keys” to “values”. In JSON, the “keys” must always be strings. Each of these pairs is conventionally referred to as a “property”. In Python, "objects" are analogous to the dict type.

How do you represent an object in JSON?

JSON Objects are surrounded by curly braces “{ }” and are written in key/value pairs. Keys must be strings (text) and values must be valid JSON data types: string, number, another JSON object, array, boolean or null.

What is the difference between JSON string and JSON object?

JSON is a string format. The data is only JSON when it is in a string format. When it is converted to a JavaScript variable, it becomes a JavaScript object.

What is inside JSON?

JSON structure As described above, JSON is a string whose format very much resembles JavaScript object literal format. You can include the same basic data types inside JSON as you can in a standard JavaScript object — strings, numbers, arrays, booleans, and other object literals.


3 Answers

Personally, I would put the type on the template itself for a simple reason, that is encapsulation. Imagine you want to separate the creation of the template and the outside object (remember separation of concerns and the single responsibility principle (https://en.wikipedia.org/wiki/Single_responsibility_principle)). If the type is on the outside object, you will always have to specify the type of the template, to be able to create it. That's a possibility, but it increases coupling and violates encapsulation.

For further reading I recommend https://en.wikipedia.org/wiki/SOLID_(object-oriented_design) for the beginning.

like image 61
samwise Avatar answered Nov 09 '22 05:11

samwise


The approach B would be much better IMHO. That is simply because, it provides a generic approach for user to access the template's attributes without concerning about its type. In this manner, user can simply write his program for a generic template which include the type as an attribute of itself.

For example, imagine you have a object type named Template which maps the json definition of a template to a Java object.

Class Template{
  String type;
  String attribute1;
  String attribute2;

......
......
}

By using approach B, you can directly map the json definition of that template, to above template object.(In this case, it is a Java object but of course the concept works for any other programming language).

User does not need to have an prior knowledge of template type, before accessing the template's definition. That's why it is said to be a more generic approach.

like image 37
Minudika Malshan Gammanpila Avatar answered Nov 09 '22 05:11

Minudika Malshan Gammanpila


I'd prefer your B variant over A and C.

However, you might also consider a structure like this:

{
   "longDesc": "The Long description and other(?)necessary hints here", 
   "type": "template",
   "ID": {
      "A001": {
         "name": "My First Game",
         "type": "BATTLE"
         /*more data here*/
      },
      "A002": {
         "name": "My 2nd Game",
         "type": "STRATEGY"
         /*more data here*/
      }
   }
};

It might give a better feel in everyday use.

like image 34
Bekim Bacaj Avatar answered Nov 09 '22 07:11

Bekim Bacaj