Skip to content
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
Closed

Empty Json serializes to "undefined" #1442

SmorkalovG opened this issue Feb 24, 2016 · 6 comments

Comments

@SmorkalovG
Copy link

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

@SmorkalovG
Copy link
Author

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
Copy link
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.

@SmorkalovG
Copy link
Author

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
Copy link
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.

@SmorkalovG
Copy link
Author

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

@s-ludwig
Copy link
Member

Fixed by #1548

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants