2

I am changing some code from Gson to Jackson where I have to check if the type of the current element is a primitive.

I could do something like this with Gson:

JsonElement element = entry.getValue();
if (element.isJsonPrimitive()) {
    ...
}

in Jackson, the Json node type is one of

JsonNodeType: { ARRAY, BINARY, BOOLEAN, MISSING, NULL, NUMBER, OBJECT, POJO, STRING }

Molham
  • 165
  • 1
  • 8
  • Maybe [this](https://stackoverflow.com/questions/19760138/parsing-json-in-java-without-knowing-json-format) helps – nortontgueno Mar 26 '19 at 14:09
  • 2
    You could try `JsonNode.isValueNode()` which will return `true` for anything other than ARRAY, OBJECT and MISSING. – Thomas Mar 26 '19 at 14:12

1 Answers1

4

Jackson's JsonNode class has isValueNode method should do the same:

@Override
public final boolean isValueNode()
{
    switch (getNodeType()) {
        case ARRAY: case OBJECT: case MISSING:
            return false;
        default:
            return true;
    }
}

If node is: ARRAY, OBJECT or MISSING it returns false; for other types - true

Michał Ziober
  • 37,175
  • 18
  • 99
  • 146