r/java
•
u/loicmathieu
•
Jul 23 '26
•
1
JEP 540: Simple JSON API (Incubator)
https://openjdk.org/jeps/540
79
Upvotes
View all comments
1
u/Efficient_Device_525 Jul 30 '26 edited Aug 01 '26
u/s888marks, I really like the simplicity of this API! For the vast majority of cases I've delt with parsing json documents, a simple API such as this would be quite ideal.
I especially appreciate getting the location of the json values that cause parsing errors. However, with the current API, any errors in user validation/transformation code will not have this benefit.
For example, if I get an error in
Instant.parse(event.get("time").asString()), I will get the json Path if there is no "time" member or if its value is not a string, but I will not get that Path if the error is caused by the string not complying with the ISO_INSTANT format.So, what if we allowed all user parsing code to be called by the Json API methods instead? This way, all parsing errors could be automatically wrapped with their json location. If we change each of the current JsonValue parsing methods to return a
JsonValue.Parser:=Function<JsonValue, T>we could bend the current API to work something more like this:This way,
.getand.mapEachcould automatically wrap anyRuntimeExceptionwith aJsonValueException(if it isn't one already) that should accept theJsonValue.path()information of the parsing error. Besides the builtinJsonValue.Parsers of the API, users would be able to create their own for specific cases like the "tid" example shown in the JEP.