3 ms·
>Book book2 = new JsonParser().parse(json, Book.class); why not: JsonParser().parse<Book>(json)
by adrianlmm 5y ago
>Book book2 = new JsonParser().parse(json, Book.class);
why not: JsonParser().parse<Book>(json)
- vturner 5y agoJsonParser() 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.
- kaba0 5y agoParent 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.
- chii 5y agothe `<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!
- vbezhenar 5y agoYou can use something like `new JsonParser<Book>(){}.parse(json)` Not saying that's a good idea, though.
- dehrmann 5y agoType erasure.