Union types

Using a union type here seems to be appropriate, but the corresponding union type may end up polluting the code everywhere, because you’ll have to mention null|String|Boolean|Double|Int|Long all over the place. It is much better from design standpoint to encapsulate it into JsonValue class as that is going be the only type you’ll have to carry around and you’ll be able to attach all your utility functions to this type. I would actually go a step further, and instead of Pair<String, Any?> or Pair<String,JsonValue> also define JsonPair class with the convenient constructors for it, too. Having defined them once, you’ll reap benefits everywhere throughout your code.

3 Likes