The issue is to decided the trade offs between following notations:
问题是决定下列表示法之间的权衡:
JSON based:
基于JSON:
"users": {
"id1": {
"id": "id1",
"firstname": "firstname1",
"lastname": "lastname1"
},
"id2": {
"id": "id2",
"firstaame": "firstname2",
"lastname": "lastname2"
}
}
Array Based:
基于数组:
users: [
{
"id": "id",
"key2": "value2",
"key3": "value3"
},
{
"id": "id",
"key2": "value2",
"key3": "value3"
}
]
Relating to this post on the same issue, I have decided (on front end) to use the JSON object notation instead of array of objects as it suits my requirements and better performance and less code in the browser.
关于同一问题上的这篇文章,我已经决定(在前端)使用JSON对象表示法而不是数组,因为它符合我的要求和更好的性能,更少的代码在浏览器中。
But the problem is that the list itself is not static. By this I mean the list is being generated i.e. fetched/stored from DB (NoSQL) and created for new entries through a Java API at the server. I am not able to decide on which notation should I use at the back end (which eventually will also affect the UI too).
但问题是列表本身并不是静态的。我的意思是,正在生成列表,即从DB (NoSQL)获取/存储列表,并通过服务器上的Java API为新条目创建列表。我无法决定在后端应该使用哪种表示法(这最终也会影响UI)。
Any thoughts/suggestion about performance, maintainability or scalability is appreciated.
任何关于性能、可维护性或可扩展性的想法或建议都是值得赞赏的。
5 个解决方案
#1
17
It is a total opinion based question. There might be many other points, but I can point out as below.
这是一个完全基于意见的问题。可能还有很多其他的点,但我可以在下面指出。
JSON based approach : If I am not wrong then this will be implemented using Map
on server side.
基于JSON的方法:如果我没有错误,那么这将在服务器端使用Map实现。
Advantage : In JavaScript you can directly use users.id1, users.id2 i.e. no need of iteration
优点:在JavaScript中可以直接使用用户。id1、用户。id2即不需要迭代
Disadvantage : On client side, some how you will require the ids present in your JSON i.e. either hard coding it or using some dynamic approach which will tell you which id is present in your JSON.
缺点:在客户端,您需要一些在JSON中显示的id的方法,例如硬编码它或者使用动态方法告诉您JSON中显示的id。
Array Based approach : If I am not wrong then this will be implemented using Array
/List
on server side.
基于数组的方法:如果我没有错,那么这将在服务器端使用数组/列表实现。
Advantage:
优势:
- On client side, you can directly iterate through array, without worrying in advance about which id is present inside it i.e. no hard coding.
- 在客户端,您可以直接遍历数组,而不必预先考虑数组中存在哪个id,即不进行硬编码。
- As pointed out by @JBNizet, array based approach will maintain the order.
- 正如@JBNizet指出的,基于数组的方法将保持顺序。
Disadvantage : If you want to fetch single id then you will need to iterate through the array.
缺点:如果希望获取单个id,则需要遍历数组。
Generally we don't send much information on client side, so array based approach will not create any problem. And transforming array into map is possible on both the side (server and client) if you want id based approach.
通常我们不会在客户端发送太多信息,所以基于数组的方法不会产生任何问题。如果您想要基于id的方法,则可以同时将数组转换为map(服务器和客户机)。
#2
5
On the server side, Arrays are stored as simple Lists: ArrayList<Content>
, while Objects are either stored as maps: HashMap<String, Content>
or, mostly, as Java Objects.
在服务器端,数组存储为简单的列表:ArrayList
In order to convert Java Entities to and from JSON, you can take a look at the Jackson project which does all that for you.
为了将Java实体转换为和从JSON转换,您可以看看Jackson项目,它为您做了所有这些事情。
I wouldn't worry about any performance differences between those two variants. It's more important to have an understandable, semantic API, so you should base your desicion on the business case rather than performance.
我不担心这两个变体之间的性能差异。更重要的是要有一个可理解的语义API,所以您应该基于业务案例而不是性能。
Looking at your example, I think an Array
is the better approach, since you want to return a list of users which are all equal. Sending the id twice makes no sense imho and increases the amount of data that has to be transmitted.
查看您的示例,我认为数组是更好的方法,因为您希望返回所有都相等的用户列表。两次发送id没有意义,增加了必须传输的数据量。
Furthermore, since Arrays
are much simpler to store and to iterate in Java, they should also provide better performance than Objects.
此外,由于数组在Java中存储和迭代要简单得多,因此它们也应该比对象提供更好的性能。
Some general differences:
一些一般性的差异:
- Arrays preserve the order
- 数组保存订单
- Arrays can contain duplicate entries
- 数组可以包含重复的条目
- Objects often have a bigger storage/network overhead
- 对象通常具有更大的存储/网络开销
- Arrays are faster to iterate (on the server side)
- 数组迭代速度更快(在服务器端)
#3
1
One big disadvantage of your first "JSON based" notation that comes to mind is that some frameworks will have problems with (de)serialization of this. For example the DataContractSerializer (C# .NET) will expect the fields id1
and id2
to be defined (hardcoded) in the class of your objects users
. I'm not sure if this applies to some Java frameworks, too. Maybe the framework will you use can deserialze it as a HashMap instead.
第一个“基于JSON”的表示法有一个很大的缺点,那就是有些框架在序列化时会遇到问题。例如,DataContractSerializer (c# . net)将期望在对象用户的类中定义id1和id2字段(硬编码)。我不确定这是否也适用于某些Java框架。也许您将要使用的框架可以将它反序列化为HashMap。
Altogether I'd find the array notation much more intuitive to work with when it comes to iteration etc.
总的来说,我发现数组表示法在迭代时更加直观。
#4
0
You can use object[property]
notation to access or set properties in an object in JavaScript.
可以使用对象[属性]表示法在JavaScript对象中访问或设置属性。
Go with array based approach at the backend, and convert the array to a map (JSON based as you refer to it) in the front end.
在后端采用基于数组的方法,并将数组转换为前端的映射(如您所指的JSON)。
var list = [{id: "id1", value: "One"}, {id: "id2", value: "Two"}]
var map = {};
list.forEach(function (item) { map[item.id] = item });
map.get("id1")
If your list changes, you can get the new list from backend and update your map in UI.
如果列表发生变化,可以从后端获取新的列表,并在UI中更新映射。
This way your backend is faster to respond as it does not have to convert list to a map. Your front end will do a O(n) iteration once over the list to convert it to a map. But that is a small price compared to O(n) you will pay every time you search on a list.
这样后端响应更快,因为它不必将list转换为map。您的前端将对列表进行一次O(n)迭代,将其转换为映射。但这只是一个很小的价格,与你每次搜索列表时支付的价格相比。
If you will be doing get by id predominantly on your data at the back end go with JSON Based at the backend itself (you can use LinkedHashMap
to preserve order).
如果您要在后台的数据上使用id,请使用基于后端本身的JSON(您可以使用LinkedHashMap来保持顺序)。
#5
0
Both approaches have their pros and cons and depends on what is it that you are looking at.
这两种方法都有各自的优缺点,这取决于你关注的是什么。
The array approach is easy to serialize and is more 'framework' friendly (you can add beans to a list and serialize the list and you are done). This allows, for example, a web-container to return the response without requiring any customization. This is likely to be supported out-of-the-box by most frameworks.
数组方法很容易序列化,而且更“框架”友好(您可以向列表添加bean并序列化列表,这样就完成了)。例如,这允许web容器返回响应,而不需要任何定制。这很可能会得到大多数框架的开箱即用支持。
On the other hand, the object based approach is more difficult to generate (in relative terms) but its easier to lookup given the key is known.
另一方面,基于对象的方法更难于生成(相对而言),但考虑到密钥是已知的,它更易于查找。
So for ease of implementation (by producer), go for the array based approach. For ease of use (consumed by clients) go for object based approach.
因此,为了便于实现(由生产者),采用基于数组的方法。为了便于使用(客户使用),使用基于对象的方法。
#1
17
It is a total opinion based question. There might be many other points, but I can point out as below.
这是一个完全基于意见的问题。可能还有很多其他的点,但我可以在下面指出。
JSON based approach : If I am not wrong then this will be implemented using Map
on server side.
基于JSON的方法:如果我没有错误,那么这将在服务器端使用Map实现。
Advantage : In JavaScript you can directly use users.id1, users.id2 i.e. no need of iteration
优点:在JavaScript中可以直接使用用户。id1、用户。id2即不需要迭代
Disadvantage : On client side, some how you will require the ids present in your JSON i.e. either hard coding it or using some dynamic approach which will tell you which id is present in your JSON.
缺点:在客户端,您需要一些在JSON中显示的id的方法,例如硬编码它或者使用动态方法告诉您JSON中显示的id。
Array Based approach : If I am not wrong then this will be implemented using Array
/List
on server side.
基于数组的方法:如果我没有错,那么这将在服务器端使用数组/列表实现。
Advantage:
优势:
- On client side, you can directly iterate through array, without worrying in advance about which id is present inside it i.e. no hard coding.
- 在客户端,您可以直接遍历数组,而不必预先考虑数组中存在哪个id,即不进行硬编码。
- As pointed out by @JBNizet, array based approach will maintain the order.
- 正如@JBNizet指出的,基于数组的方法将保持顺序。
Disadvantage : If you want to fetch single id then you will need to iterate through the array.
缺点:如果希望获取单个id,则需要遍历数组。
Generally we don't send much information on client side, so array based approach will not create any problem. And transforming array into map is possible on both the side (server and client) if you want id based approach.
通常我们不会在客户端发送太多信息,所以基于数组的方法不会产生任何问题。如果您想要基于id的方法,则可以同时将数组转换为map(服务器和客户机)。
#2
5
On the server side, Arrays are stored as simple Lists: ArrayList<Content>
, while Objects are either stored as maps: HashMap<String, Content>
or, mostly, as Java Objects.
在服务器端,数组存储为简单的列表:ArrayList
In order to convert Java Entities to and from JSON, you can take a look at the Jackson project which does all that for you.
为了将Java实体转换为和从JSON转换,您可以看看Jackson项目,它为您做了所有这些事情。
I wouldn't worry about any performance differences between those two variants. It's more important to have an understandable, semantic API, so you should base your desicion on the business case rather than performance.
我不担心这两个变体之间的性能差异。更重要的是要有一个可理解的语义API,所以您应该基于业务案例而不是性能。
Looking at your example, I think an Array
is the better approach, since you want to return a list of users which are all equal. Sending the id twice makes no sense imho and increases the amount of data that has to be transmitted.
查看您的示例,我认为数组是更好的方法,因为您希望返回所有都相等的用户列表。两次发送id没有意义,增加了必须传输的数据量。
Furthermore, since Arrays
are much simpler to store and to iterate in Java, they should also provide better performance than Objects.
此外,由于数组在Java中存储和迭代要简单得多,因此它们也应该比对象提供更好的性能。
Some general differences:
一些一般性的差异:
- Arrays preserve the order
- 数组保存订单
- Arrays can contain duplicate entries
- 数组可以包含重复的条目
- Objects often have a bigger storage/network overhead
- 对象通常具有更大的存储/网络开销
- Arrays are faster to iterate (on the server side)
- 数组迭代速度更快(在服务器端)
#3
1
One big disadvantage of your first "JSON based" notation that comes to mind is that some frameworks will have problems with (de)serialization of this. For example the DataContractSerializer (C# .NET) will expect the fields id1
and id2
to be defined (hardcoded) in the class of your objects users
. I'm not sure if this applies to some Java frameworks, too. Maybe the framework will you use can deserialze it as a HashMap instead.
第一个“基于JSON”的表示法有一个很大的缺点,那就是有些框架在序列化时会遇到问题。例如,DataContractSerializer (c# . net)将期望在对象用户的类中定义id1和id2字段(硬编码)。我不确定这是否也适用于某些Java框架。也许您将要使用的框架可以将它反序列化为HashMap。
Altogether I'd find the array notation much more intuitive to work with when it comes to iteration etc.
总的来说,我发现数组表示法在迭代时更加直观。
#4
0
You can use object[property]
notation to access or set properties in an object in JavaScript.
可以使用对象[属性]表示法在JavaScript对象中访问或设置属性。
Go with array based approach at the backend, and convert the array to a map (JSON based as you refer to it) in the front end.
在后端采用基于数组的方法,并将数组转换为前端的映射(如您所指的JSON)。
var list = [{id: "id1", value: "One"}, {id: "id2", value: "Two"}]
var map = {};
list.forEach(function (item) { map[item.id] = item });
map.get("id1")
If your list changes, you can get the new list from backend and update your map in UI.
如果列表发生变化,可以从后端获取新的列表,并在UI中更新映射。
This way your backend is faster to respond as it does not have to convert list to a map. Your front end will do a O(n) iteration once over the list to convert it to a map. But that is a small price compared to O(n) you will pay every time you search on a list.
这样后端响应更快,因为它不必将list转换为map。您的前端将对列表进行一次O(n)迭代,将其转换为映射。但这只是一个很小的价格,与你每次搜索列表时支付的价格相比。
If you will be doing get by id predominantly on your data at the back end go with JSON Based at the backend itself (you can use LinkedHashMap
to preserve order).
如果您要在后台的数据上使用id,请使用基于后端本身的JSON(您可以使用LinkedHashMap来保持顺序)。
#5
0
Both approaches have their pros and cons and depends on what is it that you are looking at.
这两种方法都有各自的优缺点,这取决于你关注的是什么。
The array approach is easy to serialize and is more 'framework' friendly (you can add beans to a list and serialize the list and you are done). This allows, for example, a web-container to return the response without requiring any customization. This is likely to be supported out-of-the-box by most frameworks.
数组方法很容易序列化,而且更“框架”友好(您可以向列表添加bean并序列化列表,这样就完成了)。例如,这允许web容器返回响应,而不需要任何定制。这很可能会得到大多数框架的开箱即用支持。
On the other hand, the object based approach is more difficult to generate (in relative terms) but its easier to lookup given the key is known.
另一方面,基于对象的方法更难于生成(相对而言),但考虑到密钥是已知的,它更易于查找。
So for ease of implementation (by producer), go for the array based approach. For ease of use (consumed by clients) go for object based approach.
因此,为了便于实现(由生产者),采用基于数组的方法。为了便于使用(客户使用),使用基于对象的方法。