public static Object toObject()

in lang/java/avro/src/main/java/org/apache/avro/util/internal/JacksonUtils.java [117:200]


  public static Object toObject(JsonNode jsonNode, Schema schema) {
    if (schema != null && schema.getType().equals(Schema.Type.UNION)) {
      return toObject(jsonNode, schema.getTypes().get(0));
    }
    if (jsonNode == null) {
      return null;
    } else if (jsonNode.isNull()) {
      return JsonProperties.NULL_VALUE;
    } else if (jsonNode.isBoolean()) {
      return jsonNode.asBoolean();
    } else if (jsonNode.isInt()) {
      if (schema == null || schema.getType().equals(Schema.Type.INT)) {
        return jsonNode.asInt();
      } else if (schema.getType().equals(Schema.Type.LONG)) {
        return jsonNode.asLong();
      } else if (schema.getType().equals(Schema.Type.FLOAT)) {
        return (float) jsonNode.asDouble();
      } else if (schema.getType().equals(Schema.Type.DOUBLE)) {
        return jsonNode.asDouble();
      }
    } else if (jsonNode.isLong()) {
      if (schema == null || schema.getType().equals(Schema.Type.LONG)) {
        return jsonNode.asLong();
      } else if (schema.getType().equals(Schema.Type.INT)) {
        if (jsonNode.canConvertToInt()) {
          return jsonNode.asInt();
        } else {
          return jsonNode.asLong();
        }
      } else if (schema.getType().equals(Schema.Type.FLOAT)) {
        return (float) jsonNode.asDouble();
      } else if (schema.getType().equals(Schema.Type.DOUBLE)) {
        return jsonNode.asDouble();
      }
    } else if (jsonNode.isDouble() || jsonNode.isFloat()) {
      if (schema != null) {
        if (schema.getType().equals(Schema.Type.DOUBLE)) {
          return jsonNode.doubleValue();
        } else if (schema.getType().equals(Schema.Type.FLOAT)) {
          return jsonNode.floatValue();
        }
      } else if (jsonNode.isDouble()) {
        return jsonNode.doubleValue();
      } else {
        return jsonNode.floatValue();
      }
    } else if (jsonNode.isBinary()) {
      try {
        return jsonNode.binaryValue();
      } catch (IOException ex) {
        // only for TextNode, so, can't happen with binaryNode.
        throw new UncheckedIOException(ex);
      }
    } else if (jsonNode.isTextual()) {
      if (schema == null || schema.getType().equals(Schema.Type.STRING) || schema.getType().equals(Schema.Type.ENUM)) {
        return jsonNode.asText();
      } else if (schema.getType().equals(Schema.Type.BYTES) || schema.getType().equals(Schema.Type.FIXED)) {
        return jsonNode.textValue().getBytes(StandardCharsets.ISO_8859_1);
      }
    } else if (jsonNode.isArray()) {
      List<Object> l = new ArrayList<>();
      for (JsonNode node : jsonNode) {
        l.add(toObject(node, schema == null ? null : schema.getElementType()));
      }
      return l;
    } else if (jsonNode.isObject()) {
      Map<Object, Object> m = new LinkedHashMap<>();
      for (Iterator<String> it = jsonNode.fieldNames(); it.hasNext();) {
        String key = it.next();
        final Schema s;
        if (schema != null && schema.getType().equals(Schema.Type.MAP)) {
          s = schema.getValueType();
        } else if (schema != null && schema.getType().equals(Schema.Type.RECORD)) {
          s = schema.getField(key).schema();
        } else {
          s = null;
        }
        Object value = toObject(jsonNode.get(key), s);
        m.put(key, value);
      }
      return m;
    }
    return null;
  }