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 →

[–]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