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

Introduce class Json for constants and static factory methods? #52

Closed
ralfstx opened this issue Jun 2, 2015 · 2 comments
Closed

Introduce class Json for constants and static factory methods? #52

ralfstx opened this issue Jun 2, 2015 · 2 comments

Comments

@ralfstx
Copy link
Owner

ralfstx commented Jun 2, 2015

With the constants NULL, TRUE, and FALSE defined on JsonValue, these constants are inherited by the subtypes of JsonValue. For example, the expression JsonArray.TRUE would compile although it doesn't make any sense. The same is true for static factory methods. The expression JsonArray.valueOf(true) would also compile and return JsonValue.TRUE which is not an instance of JsonArray.

Moving these constants and factory methods to a dedicated class Json would solve these issues. Moreover, it would result in shorter and more concise code:

Json.NULL, Json.TRUE, etc. instead of JsonValue.NULL, JsonValue.TRUE.

We could consider to rename the factory methods to value instead of valueOf.

Json.value(true), Json.value(23)...

This would also allow to add factory methods for arrays such as:

Json.array(23, 42), etc.

@sbandara
Copy link

sbandara commented Jun 2, 2015

This all looks really clean and would be fun to use. Since you can't override static methods, I guess you'd also move all parser access there as well?

@ralfstx
Copy link
Owner Author

ralfstx commented Jun 4, 2015

The static readFrom methods don't have this problem. For example, JsonArray.readFrom() shadows JsonValue.readFrom(), but both methods make sense and return the expected type.

However, when this new Json class becomes the central entrypoint to the API, it would certainly make sense to provide these methods there as well, e.g. Json.readArray(), Json.readObject() ...

ralfstx added a commit that referenced this issue Jul 10, 2015
Since JsonValue has two subclasses JsonArray and JsonObject, the
constants NULL, TRUE, and FALSE defined on JsonValue are available
on the subclasses by inheritance. For example, the expression

    JsonArray.TRUE

actually compiles even though it's non-sense. The same is true for
the static factory methods. I've seen this happen, despite compiler
warnings, and it can result in pretty incomprehensible code.

Move constants to a dedicated util class Json and deprecate the
constants on JsonValue to prevent those cases.

This also results in shorter code and matches common language: for
example, Json.NULL represents "json null".

In a next step, static factory methods can be moved to this class as
well.

See #52
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