New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty Json serializes to "undefined" #1442

Closed
SmorkalovG opened this Issue Feb 24, 2016 · 6 comments

Comments

Projects
None yet
2 participants
@SmorkalovG

SmorkalovG commented Feb 24, 2016

Why is Json().toString "undefined"? It is not a valid json, I can not pass it to the client.

@SmorkalovG

This comment has been minimized.

Show comment
Hide comment
@SmorkalovG

SmorkalovG Feb 24, 2016

Accidentally created the issue before wrote body. Sorry.
I am trying to use REST interface generator. The result of some method is:
struct Test {
ushort port;
string path;
Json arguments;
}
If method returns Test(8081, "/", Json()) then serialized json is {"port":8081,"path":"/","arguments":undefined}. It isn't valid. May be it is better to serialize Json() to {} or null?
If not, is there another way to add optional field to struct with arbitrary data?

SmorkalovG commented Feb 24, 2016

Accidentally created the issue before wrote body. Sorry.
I am trying to use REST interface generator. The result of some method is:
struct Test {
ushort port;
string path;
Json arguments;
}
If method returns Test(8081, "/", Json()) then serialized json is {"port":8081,"path":"/","arguments":undefined}. It isn't valid. May be it is better to serialize Json() to {} or null?
If not, is there another way to add optional field to struct with arbitrary data?

@s-ludwig

This comment has been minimized.

Show comment
Hide comment
@s-ludwig

s-ludwig Feb 25, 2016

Member

Try to use Json arguments = Json.emptyObject; in this case. Json.undefined is used, because everything else would be arbitrary, it could otherwise mean any of null, {} or [], but which of these makes sense depends on the situation.

If you need an optional field, you have two other possibilities:

  • Use the @optional attribute on the field in question, if optionality on the receiving side is enough (it will always be serialized when serializing)
  • Use Nullable!T. In this case, null will be used to denote the missing field.

It would be nice if Phobos also had an Optional!T type. I'd add support for it, so that the field is completely omitted if the Optional isn't set.

Member

s-ludwig commented Feb 25, 2016

Try to use Json arguments = Json.emptyObject; in this case. Json.undefined is used, because everything else would be arbitrary, it could otherwise mean any of null, {} or [], but which of these makes sense depends on the situation.

If you need an optional field, you have two other possibilities:

  • Use the @optional attribute on the field in question, if optionality on the receiving side is enough (it will always be serialized when serializing)
  • Use Nullable!T. In this case, null will be used to denote the missing field.

It would be nice if Phobos also had an Optional!T type. I'd add support for it, so that the field is completely omitted if the Optional isn't set.

@SmorkalovG

This comment has been minimized.

Show comment
Hide comment
@SmorkalovG

SmorkalovG Feb 25, 2016

Thank you for answer. Now I understand how to solve my problem.
But I still don't understand why it is possible to get invalid json as result of serialization. In my point of view it is a design problem. I think nothing should lead to invalid json. And there are not many ways to get rid of this behavior. Json().toString must throw an exception or return valid json (i.e. {} or null). Are there any reasons why it is "undefined"? Because it is human readable?

SmorkalovG commented Feb 25, 2016

Thank you for answer. Now I understand how to solve my problem.
But I still don't understand why it is possible to get invalid json as result of serialization. In my point of view it is a design problem. I think nothing should lead to invalid json. And there are not many ways to get rid of this behavior. Json().toString must throw an exception or return valid json (i.e. {} or null). Are there any reasons why it is "undefined"? Because it is human readable?

@s-ludwig

This comment has been minimized.

Show comment
Hide comment
@s-ludwig

s-ludwig Feb 25, 2016

Member

Oh, you are absolutely right, I missed that part. There are related tickets (#222, #958) and a partial PR (#1323). I think the best solution would be to follow JavaScript's rules for this - if an object has a field with an undefined value, skip it during serialization and else output null instead.

Member

s-ludwig commented Feb 25, 2016

Oh, you are absolutely right, I missed that part. There are related tickets (#222, #958) and a partial PR (#1323). I think the best solution would be to follow JavaScript's rules for this - if an object has a field with an undefined value, skip it during serialization and else output null instead.

@SmorkalovG SmorkalovG closed this Feb 25, 2016

@SmorkalovG

This comment has been minimized.

Show comment
Hide comment
@SmorkalovG

SmorkalovG Feb 25, 2016

Sorry, I thought PR fixes this situation. It is better if issue will be opened until it is fixed.

SmorkalovG commented Feb 25, 2016

Sorry, I thought PR fixes this situation. It is better if issue will be opened until it is fixed.

@s-ludwig

This comment has been minimized.

Show comment
Hide comment
@s-ludwig

s-ludwig Aug 31, 2016

Member

Fixed by #1548

Member

s-ludwig commented Aug 31, 2016

Fixed by #1548

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment