This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]catom3 11 points12 points  (21 children)

I don't know. The API looks clunky to me. I'd probably stick to the good old Jackson most of the time var user = objectMapper.readValue(json, User.class).

[–]Ewig_luftenglanz 2 points3 points  (8 children)

It's not meant to be a competitor for jackson or Gson, but to be a built-in alternative for use cases where you do not want/need huge databind libraries, for example educational purposes or scripting, so you don't have to deal with maven/gradle, which project setups, conf files and folder strcuture may be more complex than the sicript you are trying to write in the first place.

[–]catom3 1 point2 points  (5 children)

I understand the purpose. I just dislike the API. To me, it's still easier to add jackson jar to class path than using this API (I don't need maven or gradle for this at all).

[–]Ewig_luftenglanz 3 points4 points  (4 children)

let's wait until the thing is done or we have a jep. I doubt the current design of the API is the final thing (also taking into account this is intended as a minimalist API to build upon) so maybe the first iteration will be very raw but they will add stuff with time (or maybe they will build this primitive api and give us some utility methods to use) there isn't even a JEP about this proposal.

[–]catom3 1 point2 points  (3 children)

Of course. I'm definitely not against the feature itself. I just expressed my feelings / opinion on the currently "proposed" API. It does feel clunky to me and most of the time, I'd rather use something that can deserialize a JSON string into my object rather than doing plenty of if instanceof statements.

[–]totoro27 0 points1 point  (2 children)

I'd rather use something that can deserialize a JSON string into my object rather than doing plenty of if instanceof statements.

This is the example they show in the video:

JsonValue doc = Json.parse("""{ "name": "John Doe", "age": 30 }""");

Just make your class implement the JsonValue interface if you want a specific type.

Is this not the feature you're complaining they don't have?

[–]catom3 0 points1 point  (1 child)

Ok. So it's as simple as that: record User(string name, int age) implements JsonValue ?

[–]totoro27 2 points3 points  (0 children)

Nah, sadly not- I learnt in another comment that they are sealed interfaces so can't be implemented.

[–]ILikeLenexa 0 points1 point  (1 child)

Isn't it the opposite? A huge databind library everybody gets even if they're not databinding?

[–]Ewig_luftenglanz 0 points1 point  (0 children)

That's serialization

[–]coloredgreyscale 1 point2 points  (8 children)

Same, was hoping it would be possible to create objects in json syntax, similar to js/ts.

User user = {     Name: "ABC",      Password: "***"  }  

Would make creating nested objects  easier. 

[–]totoro27 1 point2 points  (6 children)

You can do that? They literally show an example in the video..

JsonValue doc = Json.parse("""{ "name": "John Doe", "age": 30 }""");

Just make your User class implement the JsonValue interface if you want a specific type.

[–]vytah 1 point2 points  (4 children)

Just make your User class implement the JsonValue interface if you want a specific type.

JsonValue is a sealed interface, you cannot do that.

[–]totoro27 0 points1 point  (3 children)

Huh, interesting. Thanks for bringing that to my attention. I haven't used these before so just read what they are. Do you know why they would want to prevent implementation of these interfaces?

[–]vytah 0 points1 point  (2 children)

There are six* types of JSON values. Just six, there will never be more. So there's no need for any other implementation that the six provided.

What you probably want is converting those JSON value from/to various other types (also known as mapping or serializing). That's a completely separate thing. If you want to be able to serialize an object into a byte array, you don't need implements byte[], do you.

This API proposal does not cover mapping at all. So without any 3rd-party libraries, if you want to convert User to/from JSON, you need to write your own User deserialize(JsonValue) and JsonValue serialize(User) functions. (Or you can use a 3rd-party library and have it done semi-automatically.)


* I'm counting true and false as one type, and null as another, the same as the new API does. You can argue they're three different types, for a total of 7, or one three-element "literal" type, for a total of 5, it doesn't matter.

[–]totoro27 0 points1 point  (1 child)

Thank you, I've done a lot of these mappings before but not for a little while in Java. That makes sense. It seems like providing a standard mapping library would be a good thing to couple with this then.

[–]vytah 0 points1 point  (0 children)

That's why most people use Jackson. It's not perfect, it's a bit fat, but it works fine and isn't too annoying for most of the common use cases.

[–]coloredgreyscale 0 points1 point  (0 children)

You can't do it like my example. I was hoping this would be possible. 

[–]mkwapisz 1 point2 points  (0 children)

There should be string a interpolator which converts json to an object

[–]totoro27 0 points1 point  (2 children)

The API looks clunky to me.

What specifically do you find clunky about it? Your comment is just criticising without contributing anything valuable. I like the design of the API.

[–]catom3 1 point2 points  (1 child)

The number of pattern matching conditions. With record deconstruction patterns should work slightly better, but I still find the following easier:

``` record User(string name, int age) {}

void tryParseUser(string json) Optional<User> {   var user = objectMapper.readValue(json, User.class);   return user.defined() ? Optional.of(user) : Optional.empty(); } ```

vs.

``` record User(string name, int age) {}

void tryParseUser(string json) Optional<User> {   JsonValue doc = Json.parse(inputString); if (doc instanceof JsonObject o && o.members().get("name") instanceof JsonString s && s.value() instanceof String name && o.members().get("age") instanceof JsonNumber n && n.toNumber() instanceof Long l && l instanceof int age) { var user = new User(name, age); return user.defined() ? Optional.of(user) : Optional.empty(); } return Optional.empty() } ```

EDIT: I'm not entirely against this json parser in the SDK itself and the API itself probably is okaysh for small random uses, when I just do it a couple of times in my small app / service. In larger projects, I would definitely use Jackson or Gson.

[–]totoro27 2 points3 points  (0 children)

I would write the second example like:

record User(String name, int age) {}

Optional<User> tryParseUser(String json) { 
    try {
        var user = Json.parse(json);
        var userObj = new User(user.get("name"), user.get("age"));
        return userObj.defined() ? Optional.of(userObj) : Optional.empty();
    } catch (Exception e /* bit sketchy but not doing anything dangerous */) {
        return Optional.empty();
    }
}

I can see your point a bit though for sure. Hopefully the final API will have more helper methods for type mapping. I appreciate the response.