private void setupObjectNodes(final JsonNode schema) {
    JsonNode node = schema.path("properties");

    if (node.isObject()) properties.putAll(CollectionUtils.toMap(node.fields()));

    node = schema.path("patternProperties");

    if (node.isObject()) patternProperties.putAll(CollectionUtils.toMap(node.fields()));

    node = schema.path("additionalProperties");

    if (node.isObject()) additionalProperties = node;
  }
Exemplo n.º 2
0
 private static void mergeJsonObject(ObjectNode src, ObjectNode other) {
   Iterator<Map.Entry<String, JsonNode>> ite = other.fields();
   while (ite.hasNext()) {
     Map.Entry<String, JsonNode> pair = ite.next();
     JsonNode s = src.get(pair.getKey());
     JsonNode v = pair.getValue();
     if (v.isObject() && s != null && s.isObject()) {
       mergeJsonObject((ObjectNode) s, (ObjectNode) v);
     } else if (v.isArray() && s != null && s.isArray()) {
       mergeJsonArray((ArrayNode) s, (ArrayNode) v);
     } else {
       src.replace(pair.getKey(), v);
     }
   }
 }
  @Override
  public Tuple convert(String source) {
    TupleBuilder builder = TupleBuilder.tuple();
    try {

      JsonNode root = mapper.readTree(source);
      for (Iterator<Entry<String, JsonNode>> it = root.fields(); it.hasNext(); ) {
        Entry<String, JsonNode> entry = it.next();
        String name = entry.getKey();
        JsonNode node = entry.getValue();
        if (node.isObject()) {
          // tuple
          builder.addEntry(name, convert(node.toString()));
        } else if (node.isArray()) {
          builder.addEntry(name, nodeToList(node));
        } else if (node.isNull()) {
          builder.addEntry(name, null);
        } else if (node.isBoolean()) {
          builder.addEntry(name, node.booleanValue());
        } else if (node.isNumber()) {
          builder.addEntry(name, node.numberValue());
        } else {
          builder.addEntry(name, mapper.treeToValue(node, Object.class));
        }
      }
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
    return builder.build();
  }
Exemplo n.º 4
0
  @Override
  public SchemaAndValue toConnectData(String topic, byte[] value) {
    JsonNode jsonValue;
    try {
      jsonValue = deserializer.deserialize(topic, value);
    } catch (SerializationException e) {
      throw new DataException(
          "Converting byte[] to Kafka Connect data failed due to serialization error: ", e);
    }

    if (enableSchemas
        && (jsonValue == null
            || !jsonValue.isObject()
            || jsonValue.size() != 2
            || !jsonValue.has("schema")
            || !jsonValue.has("payload")))
      throw new DataException(
          "JsonDeserializer with schemas.enable requires \"schema\" and \"payload\" fields and may not contain additional fields");

    // The deserialized data should either be an envelope object containing the schema and the
    // payload or the schema
    // was stripped during serialization and we need to fill in an all-encompassing schema.
    if (!enableSchemas) {
      ObjectNode envelope = JsonNodeFactory.instance.objectNode();
      envelope.set("schema", null);
      envelope.set("payload", jsonValue);
      jsonValue = envelope;
    }

    return jsonToConnect(jsonValue);
  }
  @Override
  public Tuple convert(String source) {
    TupleBuilder builder = TupleBuilder.tuple();
    try {

      JsonNode root = mapper.readTree(source);
      for (Iterator<Entry<String, JsonNode>> it = root.fields(); it.hasNext(); ) {
        Entry<String, JsonNode> entry = it.next();
        String name = entry.getKey();
        JsonNode node = entry.getValue();
        if (node.isObject()) {
          // tuple
          builder.addEntry(name, convert(node.toString()));
        } else if (node.isArray()) {
          builder.addEntry(name, nodeToList(node));
        } else {
          if (name.equals("id")) {
            // TODO how should this be handled?
          } else if (name.equals("timestamp")) {
            // TODO how should this be handled?
          } else {
            builder.addEntry(name, node.asText());
          }
        }
      }
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
    return builder.build();
  }
 @Test
 public void unclosedObject_AutoClosesOnClose() throws IOException {
   jsonGenerator.writeStartObject();
   jsonGenerator.writeFieldName("stringProp").writeValue("stringVal");
   JsonNode node = toJsonNode();
   assertTrue(node.isObject());
 }
Exemplo n.º 7
0
 private static void mergeJsonArray(ArrayNode src, ArrayNode other) {
   for (int i = 0; i < other.size(); i++) {
     JsonNode s = src.get(i);
     JsonNode v = other.get(i);
     if (s == null) {
       src.add(v);
     } else if (v.isObject() && s.isObject()) {
       mergeJsonObject((ObjectNode) s, (ObjectNode) v);
     } else if (v.isArray() && s.isArray()) {
       mergeJsonArray((ArrayNode) s, (ArrayNode) v);
     } else {
       src.remove(i);
       src.insert(i, v);
     }
   }
 }
  @Test
  public void testCommandGetSingleDocument() {
    running(
        fakeApplication(),
        () -> {
          try {
            DbHelper.open("1234567890", TEST_USER, TEST_USER);
            ObjectNode cmd = MAPPER.createObjectNode();
            ObjectNode p = MAPPER.createObjectNode();
            p.put("collection", TEST_COLLECTION);
            p.put("id", sGenIds.get(0));
            cmd.put(ScriptCommand.RESOURCE, "documents");
            cmd.put(ScriptCommand.NAME, "get");
            cmd.put(ScriptCommand.PARAMS, p);

            JsonNode node = CommandRegistry.execute(cmd, null);

            assertNotNull(node);
            assertTrue(node.isObject());
            assertNotNull(node.get("generated"));
            assertNotNull(node.get("id"));
            assertEquals(node.get("id").asText(), sGenIds.get(0));
            assertEquals(node.get("@class").asText(), TEST_COLLECTION);
          } catch (Throwable t) {
            fail(ExceptionUtils.getFullStackTrace(t));
          } finally {
            DbHelper.close(DbHelper.getConnection());
          }
        });
  }
  @Test
  public void testCreateDocument() {
    running(
        fakeApplication(),
        () -> {
          try {
            DbHelper.open("1234567890", TEST_USER, TEST_USER);
            ObjectNode params = MAPPER.createObjectNode();
            ObjectNode doc = MAPPER.createObjectNode();
            doc.put("fresh", "fresh");
            params.put("collection", TEST_COLLECTION);
            params.put("data", doc);
            ObjectNode cmd = ScriptCommands.createCommand("documents", "post", params);

            JsonNode exec = CommandRegistry.execute(cmd, null);
            assertNotNull(exec);
            assertTrue(exec.isObject());
            assertNotNull(exec.get("id"));
            assertEquals(TEST_COLLECTION, exec.get("@class").asText());

          } catch (Throwable t) {
            fail(ExceptionUtils.getFullStackTrace(t));
          } finally {
            DbHelper.close(DbHelper.getConnection());
          }
        });
  }
Exemplo n.º 10
0
    @Override
    public AtomicColumnType fromJsonNode(JsonNode json) {
      if (json.isObject() && json.has("value")) {
        return null;
      }
      BaseType baseType = BaseType.fromJson(json, "key");

      if (baseType != null) {

        AtomicColumnType atomicColumnType = new AtomicColumnType(baseType);

        JsonNode node = null;
        if ((node = json.get("min")) != null) {
          atomicColumnType.setMin(node.asLong());
        }

        if ((node = json.get("max")) != null) {
          if (node.isNumber()) {
            atomicColumnType.setMax(node.asLong());
          } else if ("unlimited".equals(node.asText())) {
            atomicColumnType.setMax(Long.MAX_VALUE);
          }
        }
        return atomicColumnType;
      }

      return null;
    }
Exemplo n.º 11
0
 public String parseData(JsonNode node, String f)
     throws JsonProcessingException, IOException, JSONException {
   System.out.println("---");
   ObjectNode json = factory.objectNode();
   type.push(f);
   Iterator<String> fieldNames = node.fieldNames();
   String uri = type.toString().replaceAll(", ", ".").replaceAll("[\\[\\]]", "");
   json.put("type_uri", uri);
   ObjectNode object = factory.objectNode();
   while (fieldNames.hasNext()) {
     String fieldName = fieldNames.next();
     JsonNode fieldValue = node.get(fieldName);
     // if (node.findParent("description").get("description") != null)
     // json.set("value", node.findParent("description").get("description"));
     if (fieldValue.isObject()) {
       ObjectNode assoc = factory.objectNode();
       assoc.set(uri + "." + parseData(fieldValue, fieldName).replaceAll("\n", ""), fieldValue);
       json.set("composite", assoc);
     } else {
       json.set(fieldName, fieldValue);
     }
   }
   // json.append("}");
   System.out.println("xxx");
   System.out.println(m.writerWithDefaultPrettyPrinter().writeValueAsString(json));
   TopicModel t = new TopicModel(new JSONObject(json.toString()));
   System.err.println(t.getUri());
   System.err.println(t.getChildTopicsModel());
   return type.pop();
 }
  @Test
  public void testCommandAlterDocument() {
    running(
        fakeApplication(),
        () -> {
          try {
            DbHelper.open("1234567890", TEST_USER, TEST_USER);
            ObjectNode cmd = MAPPER.createObjectNode();
            ObjectNode p = MAPPER.createObjectNode();
            p.put("id", sGenIds.get(0));
            p.put("collection", TEST_COLLECTION);

            cmd.put(ScriptCommand.RESOURCE, "documents");
            cmd.put(ScriptCommand.NAME, "get");

            cmd.put(ScriptCommand.PARAMS, p);

            JsonNode node = CommandRegistry.execute(cmd, null);
            assertNotNull(node);
            assertTrue(node.isObject());
            ObjectNode doc = node.deepCopy();
            doc.put("extra", "extra");

            ObjectNode upd = MAPPER.createObjectNode();
            upd.put(ScriptCommand.RESOURCE, "documents");
            upd.put(ScriptCommand.NAME, "put");

            ObjectNode params = MAPPER.createObjectNode();
            params.put("collection", TEST_COLLECTION);
            params.put("id", doc.get("id").asText());
            params.put("data", doc);
            upd.put(ScriptCommand.PARAMS, params);
            JsonNode res = CommandRegistry.execute(upd, null);
            assertNotNull(res);
            assertTrue(res.isObject());
            assertNotNull(res.get("extra"));
            assertEquals(res.get("id"), doc.get("id"));
            assertEquals("extra", res.get("extra").asText());
          } catch (Throwable t) {
            fail(ExceptionUtils.getFullStackTrace(t));
          } finally {
            DbHelper.close(DbHelper.getConnection());
          }
        });
  }
Exemplo n.º 13
0
 @Override
 public DataSourceImpl getNested(String attrName) {
   JsonNode json = data.get(attrName);
   if (json == null) {
     throw new ConfigException("Attribute " + attrName + " is required but not set");
   }
   if (!json.isObject()) {
     throw new ConfigException("Attribute " + attrName + " must be an object");
   }
   return newInstance(model, (ObjectNode) json);
 }
Exemplo n.º 14
0
 @Override
 public DataSourceImpl getNestedOrSetEmpty(String attrName) {
   JsonNode json = data.get(attrName);
   if (json == null) {
     json = data.objectNode();
     data.set(attrName, json);
   } else if (!json.isObject()) {
     throw new ConfigException("Attribute " + attrName + " must be an object");
   }
   return newInstance(model, (ObjectNode) json);
 }
  private void setupArrayNodes(final JsonNode schema) {
    JsonNode node = schema.path("items");

    /**
     * We don't bother at this point: if items is a schema, then it will be used for each and every
     * element of the instance to validate -- it's just as if additionalItems were never defined.
     * So, as items is defined as a list above, we just leave it empty and assign the contents of
     * the keyword to additionalItems.
     */
    if (node.isObject()) {
      additionalItems = node;
      return;
    }

    if (node.isArray()) for (final JsonNode item : node) items.add(item);

    node = schema.path("additionalItems");

    if (node.isObject()) additionalItems = node;
  }
 /** Pushes one variable scope level. */
 private void pushSection(String name) {
   JsonNode node = currentNode.path(name);
   ObjectNode obj = null;
   if (node.isObject()) {
     obj = (ObjectNode) node;
   } else {
     obj = JsonUtils.createObjectNode();
     currentNode.put(name, obj);
   }
   variables.push(currentNode);
   currentNode = obj;
 }
Exemplo n.º 17
0
 public void testEmbeddedObjectInObject() throws Exception {
   TokenBuffer buf = new TokenBuffer(MAPPER);
   buf.writeStartObject();
   buf.writeFieldName("pojo");
   buf.writeObject(MARKER);
   buf.writeEndObject();
   JsonNode node = MAPPER.readTree(buf.asParser());
   buf.close();
   assertTrue(node.isObject());
   assertEquals(1, node.size());
   JsonNode n = node.get("pojo");
   assertTrue(n.isPojo());
   assertSame(MARKER, ((POJONode) n).getPojo());
 }
 private List<Object> nodeToList(JsonNode node) {
   List<Object> list = new ArrayList<Object>(node.size());
   for (int i = 0; i < node.size(); i++) {
     JsonNode item = node.get(i);
     if (item.isObject()) {
       list.add(convert(item.toString()));
     } else if (item.isArray()) {
       list.add(nodeToList(item));
     } else {
       list.add(item.asText());
     }
   }
   return list;
 }
Exemplo n.º 19
0
 public static Map<String, ?> toMap(JsonNode node) throws IOException {
   if (node.isObject()) {
     Map<String, Object> map = new LinkedHashMap<>();
     Iterator<String> fieldNames = node.fieldNames();
     while (fieldNames.hasNext()) {
       String fieldName = fieldNames.next();
       JsonNode fieldNode = node.get(fieldName);
       map.put(fieldName, toObject(fieldNode));
     }
     return map;
   } else {
     throw new IllegalArgumentException("Can only transform a JSON object into a map");
   }
 }
Exemplo n.º 20
0
  private SchemaAndValue jsonToConnect(JsonNode jsonValue) {
    if (jsonValue == null) return SchemaAndValue.NULL;

    if (!jsonValue.isObject()
        || jsonValue.size() != 2
        || !jsonValue.has(JsonSchema.ENVELOPE_SCHEMA_FIELD_NAME)
        || !jsonValue.has(JsonSchema.ENVELOPE_PAYLOAD_FIELD_NAME))
      throw new DataException(
          "JSON value converted to Kafka Connect must be in envelope containing schema");

    Schema schema = asConnectSchema(jsonValue.get(JsonSchema.ENVELOPE_SCHEMA_FIELD_NAME));
    return new SchemaAndValue(
        schema, convertToConnect(schema, jsonValue.get(JsonSchema.ENVELOPE_PAYLOAD_FIELD_NAME)));
  }
Exemplo n.º 21
0
  /**
   * Return a map out of an object's members
   *
   * <p>If the node given as an argument is not a map, an empty map is returned.
   *
   * @param node the node
   * @return a map
   */
  public static Map<String, JsonNode> asMap(final JsonNode node) {
    if (!node.isObject()) return Collections.emptyMap();

    final Iterator<Map.Entry<String, JsonNode>> iterator = node.fields();
    final Map<String, JsonNode> ret = Maps.newHashMap();

    Map.Entry<String, JsonNode> entry;

    while (iterator.hasNext()) {
      entry = iterator.next();
      ret.put(entry.getKey(), entry.getValue());
    }

    return ret;
  }
Exemplo n.º 22
0
  /**
   * Handles the given {@link JsonNode} and writes the responses to the given {@link OutputStream}.
   *
   * @param node the {@link JsonNode}
   * @param ops the {@link OutputStream}
   * @throws IOException on error
   */
  protected void handleNode(JsonNode node, OutputStreamWrapper opsw) throws IOException {

    // handle objects
    if (node.isObject()) {
      handleObject(ObjectNode.class.cast(node), opsw);

      // handle arrays
    } else if (node.isArray()) {
      handleArray(ArrayNode.class.cast(node), opsw);

      // bail on bad data
    } else {
      throw new IllegalArgumentException("Invalid JsonNode type: " + node.getClass().getName());
    }
  }
 @Test
 public void simpleObject_AllPrimitiveTypes() throws IOException {
   jsonGenerator.writeStartObject();
   jsonGenerator.writeFieldName("stringProp").writeValue("stringVal");
   jsonGenerator.writeFieldName("integralProp").writeValue(42);
   jsonGenerator.writeFieldName("booleanProp").writeValue(true);
   jsonGenerator.writeFieldName("doubleProp").writeValue(123.456);
   jsonGenerator.writeEndObject();
   JsonNode node = toJsonNode();
   assertTrue(node.isObject());
   assertEquals("stringVal", node.get("stringProp").textValue());
   assertEquals(42, node.get("integralProp").longValue());
   assertEquals(true, node.get("booleanProp").booleanValue());
   assertEquals(123.456, node.get("doubleProp").doubleValue(), DELTA);
 }
Exemplo n.º 24
0
  /**
   * Return true if value is not present or if underlying JSON is empty object, array or null.
   * WARNING this method can throw same exceptions as {@link JsonNodeValue#get()} in a case if
   * source is invalid JSON string.
   */
  public boolean isEmpty() {
    if (!isPresent()) {
      return true;
    }

    JsonNode n = get();

    if ((n.isObject() || n.isArray()) && n.size() == 0) {
      return true;
    }

    if (n.isNull()) {
      return true;
    }

    return false;
  }
 private Task getTaskFromJson(JsonNode node) throws JsonProcessingException {
   if (node.isObject()) {
     ObjectNode onode = (ObjectNode) node;
     final JsonNode type = onode.remove("type");
     final JsonNode attributes = onode.remove("attributes");
     final JsonNode relationships = onode.remove("relationships");
     final JsonNode links = onode.remove("links");
     Iterator<Map.Entry<String, JsonNode>> fields = attributes.fields();
     while (fields.hasNext()) {
       Map.Entry<String, JsonNode> f = fields.next();
       onode.put(f.getKey(), f.getValue().textValue());
     }
     return mapper.treeToValue(onode, Task.class);
   } else {
     throw new JsonMappingException("Not an object: " + node);
   }
 }
Exemplo n.º 26
0
  private static List<Geocache> parseCaches(final ObjectNode response) {
    try {
      // Check for empty result
      final JsonNode results = response.path("results");
      if (!results.isObject()) {
        return Collections.emptyList();
      }

      // Get and iterate result list
      final List<Geocache> caches = new ArrayList<>(results.size());
      for (final JsonNode cache : results) {
        caches.add(parseSmallCache((ObjectNode) cache));
      }
      return caches;
    } catch (ClassCastException | NullPointerException e) {
      Log.e("OkapiClient.parseCachesResult", e);
    }
    return Collections.emptyList();
  }
  @Override
  protected final void checkValue(
      final Collection<JsonPointer> pointers,
      final ProcessingReport report,
      final JsonSchemaTree tree)
      throws ProcessingException {
    final JsonNode node = getNode(tree);
    final Map<String, JsonNode> map = Maps.newTreeMap();
    map.putAll(JacksonUtils.asMap(node));

    String key;
    JsonNode value;

    for (final Map.Entry<String, JsonNode> entry : map.entrySet()) {
      key = entry.getKey();
      value = entry.getValue();
      if (value.isObject()) pointers.add(BASE_POINTER.append(key));
      else checkDependency(report, entry.getKey(), tree);
    }
  }
 private List<Object> nodeToList(JsonNode node) throws JsonProcessingException {
   List<Object> list = new ArrayList<Object>(node.size());
   for (int i = 0; i < node.size(); i++) {
     JsonNode item = node.get(i);
     if (item.isObject()) {
       list.add(convert(item.toString()));
     } else if (item.isArray()) {
       list.add(nodeToList(item));
     } else if (item.isNull()) {
       list.add(null);
     } else if (item.isBoolean()) {
       list.add(item.booleanValue());
     } else if (item.isNumber()) {
       list.add(item.numberValue());
     } else {
       list.add(mapper.treeToValue(item, Object.class));
     }
   }
   return list;
 }
Exemplo n.º 29
0
  /**
   * Determines whether or not the given {@link JsonNode} matches the given type. This method is
   * limitted to a few java types only and shouldn't be used to determine with great accuracy
   * whether or not the types match.
   *
   * @param node the {@link JsonNode}
   * @param type the {@link Class}
   * @return true if the types match, false otherwise
   */
  protected boolean isMatchingType(JsonNode node, Class<?> type) {

    if (node.isNull()) {
      return true;

    } else if (node.isTextual()) {
      return String.class.isAssignableFrom(type);

    } else if (node.isNumber()) {
      return Number.class.isAssignableFrom(type)
          || short.class.isAssignableFrom(type)
          || int.class.isAssignableFrom(type)
          || long.class.isAssignableFrom(type)
          || float.class.isAssignableFrom(type)
          || double.class.isAssignableFrom(type);

    } else if (node.isArray() && type.isArray()) {
      return (node.size() > 0) ? isMatchingType(node.get(0), type.getComponentType()) : false;

    } else if (node.isArray()) {
      return type.isArray() || Collection.class.isAssignableFrom(type);

    } else if (node.isBinary()) {
      return byte[].class.isAssignableFrom(type)
          || Byte[].class.isAssignableFrom(type)
          || char[].class.isAssignableFrom(type)
          || Character[].class.isAssignableFrom(type);

    } else if (node.isBoolean()) {
      return boolean.class.isAssignableFrom(type) || Boolean.class.isAssignableFrom(type);

    } else if (node.isObject() || node.isPojo()) {
      return !type.isPrimitive()
          && !String.class.isAssignableFrom(type)
          && !Number.class.isAssignableFrom(type)
          && !Boolean.class.isAssignableFrom(type);
    }

    // not sure if it's a matching type
    return false;
  }
Exemplo n.º 30
0
    private ViewArray processArrayNode(ArrayNode arrayNode) {
      ViewArray node = new ViewArray();

      if (arrayNode.size() != 1) {
        throw new IllegalArgumentException(
            "Arrays in example view data need to contain one and only one element");
      }

      JsonNode element = arrayNode.get(0);
      if (element == null || element.isNull()) {
        throw new IllegalArgumentException(
            "Arrays in example view data cannot contain null elements");
      }

      if (element.isArray()) {
        throw new IllegalArgumentException(
            "Arrays in example view data cannot be directly nested within other arrays.");
      } else if (element.isObject()) {
        node.element = processObjectNode((ObjectNode) element);
      }

      return node;
    }