6

I'm parsing JSON strings sent from my WLAN speakers using Java/Jackson, whose format is unfortunately varying. While there's some static and not so important part of the response which is quite easy to parse, the really essential stuff may have many different structures, depending on the command sent to the speaker.

For that purpose, I think jackson's TypeReference() is best to map the current structure to key/value pairs and then see what we have. Problem is that I don't really understand how TypeReference works, and I don't want to blindly use "magic" functions where I have no clue what's actually happening. The reference states that sub classing is used, but the following syntax is not really clear to me, especially and foremost the empty curly brackets at the end:

TypeReference ref = new TypeReference<List<Integer>>() { };

Can someone explain to me how this class works? Many thanks in advance!

  • 1
    Best answered by just looking at the source code... And Jackson happens to be open-source. – ernest_k Jun 20 '18 at 10:52
  • its used to avoid the type ereasure when mapping json collections to a specific type. The braces are because type reference is an abstract class. you're making a new concrete impl. – Darren Forsythe Jun 20 '18 at 10:59
  • @DarrenForsythe: thank you, but that's what confuses me... a concrete implementation of an abstract class? I'm by no means a pro (you'll get that :-) ), but I always thought that abstract classes are MEANT not to be instantiated. – Oidipous_REXX Jun 20 '18 at 11:10
  • you are not instantiating the abstract class, you are making a concrete (anonymous) class from it. Abstract classes are to be extended by concrete implementations which is what is happening here. – Darren Forsythe Jun 20 '18 at 11:17
  • @DarrenForsythe: so the code above is some kind of shorthand for public class TypeReference> extends TypeReference {} ? Sorry for my stupid questions, I just don't get the whole thing yet. Maybe I'm just too retarded or inexperienced :-( – Oidipous_REXX Jun 20 '18 at 11:59
  • Regardless of the question of how `TypeReference()` works, if you want to parse a JSON object with varying properties, the best way is the parse into a `Map` and then you can query the map's keys and entries. – Sharon Ben Asher Jun 21 '18 at 09:02

1 Answers1

2

Type Reference / TypeToken / ParameterizedTypeReference

TypeReference is from Jackson, there's also Gson's TypeToken and Spring's ParameterizedTypeReference.

Why we need it?

The purpose is to capture the generic type and retain it at runtime, because generics only works at compile-time (type erasure), so at runtime, in your example, this List<Integer> has been erased.

Instantiate a direct subclass of an abstract class i.e TypeReference

The ending class body {} is part of a class instance creation expression to create an anonymous class. The reference is JLS - Section # 15.9.1.

15.9.1. Determining the Class being Instantiated If the class instance creation expression ends in a class body, then the class being instantiated is an anonymous class. Then:

If T denotes a class, then an anonymous direct subclass of the class named by T is declared. It is a compile-time error if the class denoted by T is a final class.

In either case, the body of the subclass is the ClassBody given in the class instance creation expression.

The class being instantiated is the anonymous subclass.

jumping_monkey
  • 5,941
  • 2
  • 43
  • 58