admin管理员组文章数量:1026170
I've looked around for a suitable method to catch or prevent invalid JSON.parse
calls, specifically in the case of WebSocket messages that don't involve type/catch
block due to its performance hit.
I've almost fully moved my RESTful API to pure WebSocket API using JSON for munications. The only problem is, I can't figure out how to prevent JSON.parse from halting the app when a malformed message string is put through my onmessage
function. All messages sent from the server are theoretically proper JSON that's been stringified, so the question also is, is this an edge case to worry about? Since the function thats used to send data from the serverside JSON stringifies before sending.
I'm using React
and Redux
with redux-thunk
to open a WebSocket and add event listeners, so on a message the function below is being run.
function onMessage(msg) {
const data = JSON.parse(msg.data);
return {
type: data.type,
data: data.data
}
}
But this, of course, breaks if msg is not a valid JSON string then halting execution of the app.
So, without a try/catch
block, is the only option to (somehow) ensure valid JSON is being sent? Or is this an edge case I shouldn't be worried about.
EDIT
This may not be such a big issue for client side since all messages are ing from a centralized point (the server), though on the other hand, quite a big issue for the server, seeing it's possible it to receive messages that have not been sent from the application.
Is try/catch
really the devil it's made out to be? Since the only thing I can think of is to create a regex check, which in itself would end up being quite plicated.
I've looked around for a suitable method to catch or prevent invalid JSON.parse
calls, specifically in the case of WebSocket messages that don't involve type/catch
block due to its performance hit.
I've almost fully moved my RESTful API to pure WebSocket API using JSON for munications. The only problem is, I can't figure out how to prevent JSON.parse from halting the app when a malformed message string is put through my onmessage
function. All messages sent from the server are theoretically proper JSON that's been stringified, so the question also is, is this an edge case to worry about? Since the function thats used to send data from the serverside JSON stringifies before sending.
I'm using React
and Redux
with redux-thunk
to open a WebSocket and add event listeners, so on a message the function below is being run.
function onMessage(msg) {
const data = JSON.parse(msg.data);
return {
type: data.type,
data: data.data
}
}
But this, of course, breaks if msg is not a valid JSON string then halting execution of the app.
So, without a try/catch
block, is the only option to (somehow) ensure valid JSON is being sent? Or is this an edge case I shouldn't be worried about.
EDIT
This may not be such a big issue for client side since all messages are ing from a centralized point (the server), though on the other hand, quite a big issue for the server, seeing it's possible it to receive messages that have not been sent from the application.
Is try/catch
really the devil it's made out to be? Since the only thing I can think of is to create a regex check, which in itself would end up being quite plicated.
-
Why would server not send valid
JSON
? – guest271314 Commented Aug 9, 2017 at 3:37 - @guest271314 Yeah, after thinking about it, it's probably not so much an issue for the client, though more so an issue for the server since it's possible for a non-JSON message to be sent, in which case causing the error on the server. – sparcut Commented Aug 9, 2017 at 3:44
-
Why would it be possible for invalid
JSON
to be sent to server? – guest271314 Commented Aug 9, 2017 at 3:45 -
@guest271314 Since WebSocket messages are strings, there is no enforcement to send
JSON
. Thus, one could send any string, which would then be run throughJSON.parse
causing the said error to be thrown. The client app will never send non-JSON or malformedJSON
but it is very simple for one to send a non-JSON message to the WebSocket server thus crashing it with this error. – sparcut Commented Aug 9, 2017 at 5:58 - @guest271314 Added extra info to initial post. – sparcut Commented Aug 9, 2017 at 6:11
1 Answer
Reset to default 4don't involve
type/catch
block due to its performance hit.
Forget the myths. You want to catch an exception, like the one from JSON.parse
, you use a try
/catch
block. It's that simple and not a significant performance hit. Of course you could also write your own logic to validate JSON strings (not with regex!), but that's gonna be a plete parser which just doesn't use exceptions to signal malformed input - and much slower than the native function.
Is this an edge case to worry about?
On the client, hardly. You're controlling the server and making sure to send only valid JSON strings. If you don't, I'd worry much more about the server than about a few clients crashing. The users will most likely reload the page and continue.
Though on the other hand, quite a big issue for the server, seeing it's possible it to receive messages that have not been sent from the application.
Yes. On the server you absolutely need to worry about malformed input. If sending invalid JSON makes your server crash, that's really bad.
I've looked around for a suitable method to catch or prevent invalid JSON.parse
calls, specifically in the case of WebSocket messages that don't involve type/catch
block due to its performance hit.
I've almost fully moved my RESTful API to pure WebSocket API using JSON for munications. The only problem is, I can't figure out how to prevent JSON.parse from halting the app when a malformed message string is put through my onmessage
function. All messages sent from the server are theoretically proper JSON that's been stringified, so the question also is, is this an edge case to worry about? Since the function thats used to send data from the serverside JSON stringifies before sending.
I'm using React
and Redux
with redux-thunk
to open a WebSocket and add event listeners, so on a message the function below is being run.
function onMessage(msg) {
const data = JSON.parse(msg.data);
return {
type: data.type,
data: data.data
}
}
But this, of course, breaks if msg is not a valid JSON string then halting execution of the app.
So, without a try/catch
block, is the only option to (somehow) ensure valid JSON is being sent? Or is this an edge case I shouldn't be worried about.
EDIT
This may not be such a big issue for client side since all messages are ing from a centralized point (the server), though on the other hand, quite a big issue for the server, seeing it's possible it to receive messages that have not been sent from the application.
Is try/catch
really the devil it's made out to be? Since the only thing I can think of is to create a regex check, which in itself would end up being quite plicated.
I've looked around for a suitable method to catch or prevent invalid JSON.parse
calls, specifically in the case of WebSocket messages that don't involve type/catch
block due to its performance hit.
I've almost fully moved my RESTful API to pure WebSocket API using JSON for munications. The only problem is, I can't figure out how to prevent JSON.parse from halting the app when a malformed message string is put through my onmessage
function. All messages sent from the server are theoretically proper JSON that's been stringified, so the question also is, is this an edge case to worry about? Since the function thats used to send data from the serverside JSON stringifies before sending.
I'm using React
and Redux
with redux-thunk
to open a WebSocket and add event listeners, so on a message the function below is being run.
function onMessage(msg) {
const data = JSON.parse(msg.data);
return {
type: data.type,
data: data.data
}
}
But this, of course, breaks if msg is not a valid JSON string then halting execution of the app.
So, without a try/catch
block, is the only option to (somehow) ensure valid JSON is being sent? Or is this an edge case I shouldn't be worried about.
EDIT
This may not be such a big issue for client side since all messages are ing from a centralized point (the server), though on the other hand, quite a big issue for the server, seeing it's possible it to receive messages that have not been sent from the application.
Is try/catch
really the devil it's made out to be? Since the only thing I can think of is to create a regex check, which in itself would end up being quite plicated.
-
Why would server not send valid
JSON
? – guest271314 Commented Aug 9, 2017 at 3:37 - @guest271314 Yeah, after thinking about it, it's probably not so much an issue for the client, though more so an issue for the server since it's possible for a non-JSON message to be sent, in which case causing the error on the server. – sparcut Commented Aug 9, 2017 at 3:44
-
Why would it be possible for invalid
JSON
to be sent to server? – guest271314 Commented Aug 9, 2017 at 3:45 -
@guest271314 Since WebSocket messages are strings, there is no enforcement to send
JSON
. Thus, one could send any string, which would then be run throughJSON.parse
causing the said error to be thrown. The client app will never send non-JSON or malformedJSON
but it is very simple for one to send a non-JSON message to the WebSocket server thus crashing it with this error. – sparcut Commented Aug 9, 2017 at 5:58 - @guest271314 Added extra info to initial post. – sparcut Commented Aug 9, 2017 at 6:11
1 Answer
Reset to default 4don't involve
type/catch
block due to its performance hit.
Forget the myths. You want to catch an exception, like the one from JSON.parse
, you use a try
/catch
block. It's that simple and not a significant performance hit. Of course you could also write your own logic to validate JSON strings (not with regex!), but that's gonna be a plete parser which just doesn't use exceptions to signal malformed input - and much slower than the native function.
Is this an edge case to worry about?
On the client, hardly. You're controlling the server and making sure to send only valid JSON strings. If you don't, I'd worry much more about the server than about a few clients crashing. The users will most likely reload the page and continue.
Though on the other hand, quite a big issue for the server, seeing it's possible it to receive messages that have not been sent from the application.
Yes. On the server you absolutely need to worry about malformed input. If sending invalid JSON makes your server crash, that's really bad.
本文标签:
版权声明:本文标题:javascript - How to ensure a WebSocket message is JSON string, preventing JSON.parse error? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745623646a2159727.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论