-
Notifications
You must be signed in to change notification settings - Fork 185
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
Comments
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? |
The static However, when this new |
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
With the constants
NULL
,TRUE
, andFALSE
defined onJsonValue
, these constants are inherited by the subtypes ofJsonValue
. For example, the expressionJsonArray.TRUE
would compile although it doesn't make any sense. The same is true for static factory methods. The expressionJsonArray.valueOf(true)
would also compile and returnJsonValue.TRUE
which is not an instance ofJsonArray
.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 ofJsonValue.NULL
,JsonValue.TRUE
.We could consider to rename the factory methods to
value
instead ofvalueOf
.Json.value(true)
,Json.value(23)
...This would also allow to add factory methods for arrays such as:
Json.array(23, 42)
, etc.The text was updated successfully, but these errors were encountered: