Hacker News new | ask | show | jobs
by adrianlmm 1618 days ago
>Book book2 = new JsonParser().parse(json, Book.class);

why not: JsonParser().parse<Book>(json)

3 comments

the `<Book>` generic type doesn't translate to anything at run time, so you cannot actually parse the json out as a book class, unless you already knew it was going to be a Book. The parse() method cannot be generic over all possible inputs as is - unless the user also pass in the `Book.class` parameter!
You can use something like `new JsonParser<Book>(){}.parse(json)`

Not saying that's a good idea, though.

Type erasure.
JsonParser() is Scala syntax. Not sure how you'd accomplish that in Java and as for parse<Book> Java's generics are erased at compile time.
Parent just missed the new keyword, and generics can work based on return type as well with the above syntax. It has nothing to do with erasure, we are at compile time.