Java Code Examples for com.fasterxml.jackson.databind.node.ObjectNode#isEmpty()

The following examples show how to use com.fasterxml.jackson.databind.node.ObjectNode#isEmpty() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractJPAJSONLoggerDAO.java    From syncope with Apache License 2.0 6 votes vote down vote up
private Optional<ObjectNode> buildContainer() {
    ObjectNode logger = MAPPER.createObjectNode();
    if (type != null) {
        logger.put("type", type.name());
    }
    if (StringUtils.isNotBlank(category)) {
        logger.put("category", category);
    }
    if (StringUtils.isNotBlank(subcategory)) {
        logger.put("subcategory", subcategory);
    }
    if (result != null) {
        logger.put("result", result.name());
    }

    if (!logger.isEmpty()) {
        ObjectNode container = MAPPER.createObjectNode();
        container.set("logger", logger);
        return Optional.of(container);
    }

    return Optional.empty();
}
 
Example 2
Source File: JsonSubTypesResolver.java    From jsonschema-generator with Apache License 2.0 4 votes vote down vote up
/**
 * Create the custom schema definition for the given subtype, considering the {@link JsonTypeInfo#include()} setting.
 *
 * @param javaType targeted subtype
 * @param typeInfoAnnotation annotation for looking up the type identifier and determining the kind of inclusion/serialization
 * @param attributesToInclude optional: additional attributes to include on the actual/contained schema definition
 * @param context generation context
 * @return created custom definition (or {@code null} if no supported subtype resolution scenario could be detected
 */
private ObjectNode createSubtypeDefinition(ResolvedType javaType, JsonTypeInfo typeInfoAnnotation, ObjectNode attributesToInclude,
        SchemaGenerationContext context) {
    final String typeIdentifier = this.getTypeIdentifier(javaType, typeInfoAnnotation);
    if (typeIdentifier == null) {
        return null;
    }
    final ObjectNode definition = context.getGeneratorConfig().createObjectNode();
    switch (typeInfoAnnotation.include()) {
    case WRAPPER_ARRAY:
        definition.put(context.getKeyword(SchemaKeyword.TAG_TYPE), context.getKeyword(SchemaKeyword.TAG_TYPE_ARRAY));
        ArrayNode itemsArray = definition.withArray(context.getKeyword(SchemaKeyword.TAG_ITEMS));
        itemsArray.addObject()
                .put(context.getKeyword(SchemaKeyword.TAG_TYPE), context.getKeyword(SchemaKeyword.TAG_TYPE_STRING))
                .put(context.getKeyword(SchemaKeyword.TAG_CONST), typeIdentifier);
        if (attributesToInclude == null || attributesToInclude.isEmpty()) {
            itemsArray.add(context.createStandardDefinitionReference(javaType, this));
        } else {
            itemsArray.addObject()
                    .withArray(context.getKeyword(SchemaKeyword.TAG_ALLOF))
                    .add(context.createStandardDefinitionReference(javaType, this))
                    .add(attributesToInclude);
        }
        break;
    case WRAPPER_OBJECT:
        definition.put(context.getKeyword(SchemaKeyword.TAG_TYPE), context.getKeyword(SchemaKeyword.TAG_TYPE_OBJECT));
        ObjectNode propertiesNode = definition.with(context.getKeyword(SchemaKeyword.TAG_PROPERTIES));
        if (attributesToInclude == null || attributesToInclude.isEmpty()) {
            propertiesNode.set(typeIdentifier, context.createStandardDefinitionReference(javaType, this));
        } else {
            propertiesNode.with(typeIdentifier)
                    .withArray(context.getKeyword(SchemaKeyword.TAG_ALLOF))
                    .add(context.createStandardDefinitionReference(javaType, this))
                    .add(attributesToInclude);
        }
        break;
    case PROPERTY:
    case EXISTING_PROPERTY:
        final String propertyName = Optional.ofNullable(typeInfoAnnotation.property())
                .filter(name -> !name.isEmpty())
                .orElseGet(() -> typeInfoAnnotation.use().getDefaultPropertyName());
        ObjectNode additionalPart = definition.withArray(context.getKeyword(SchemaKeyword.TAG_ALLOF))
                .add(context.createStandardDefinitionReference(javaType, this))
                .addObject();
        if (attributesToInclude != null && !attributesToInclude.isEmpty()) {
            additionalPart.setAll(attributesToInclude);
        }
        additionalPart.put(context.getKeyword(SchemaKeyword.TAG_TYPE), context.getKeyword(SchemaKeyword.TAG_TYPE_OBJECT))
                .with(context.getKeyword(SchemaKeyword.TAG_PROPERTIES))
                .with(propertyName)
                .put(context.getKeyword(SchemaKeyword.TAG_CONST), typeIdentifier);
        break;
    default:
        return null;
    }
    return definition;
}
 
Example 3
Source File: PlainJsonDocumentSerializer.java    From crnk-framework with Apache License 2.0 4 votes vote down vote up
private void writeJsonApi(JsonGenerator gen, ObjectNode jsonapi, SerializerProvider serializerProvider) throws IOException {
	if (jsonapi != null && !jsonapi.isEmpty(serializerProvider)) {
		gen.writeObjectField("jsonapi", jsonapi);
	}
}
 
Example 4
Source File: PlainJsonDocumentSerializer.java    From crnk-framework with Apache License 2.0 4 votes vote down vote up
private void writeLinks(JsonGenerator gen, ObjectNode links, SerializerProvider serializerProvider) throws IOException {
	if (links != null && !links.isEmpty(serializerProvider)) {
		gen.writeObjectField("links", links);
	}
}
 
Example 5
Source File: PlainJsonDocumentSerializer.java    From crnk-framework with Apache License 2.0 4 votes vote down vote up
private void writeMeta(JsonGenerator gen, ObjectNode meta, SerializerProvider serializerProvider) throws IOException {
	if (meta != null && !meta.isEmpty(serializerProvider)) {
		gen.writeObjectField("meta", meta);
	}
}
 
Example 6
Source File: JsonUnflattener.java    From json-flattener with Apache License 2.0 4 votes vote down vote up
/**
 * Returns a JSON string of nested objects by the given flattened JSON string.
 * 
 * @return a JSON string of nested objects
 */
public String unflatten() {
  StringWriter sw = new StringWriter();
  if (root.isArray()) {
    ArrayNode unflattenedArray = unflattenArray((ArrayNode) root);
    sw.append(writeByConfig(unflattenedArray));
    return sw.toString();
  }
  if (!root.isObject()) {
    return root.toString();
  }

  ObjectNode flattened = (ObjectNode) root;
  JsonNode unflattened =
      flattened.isEmpty() ? mapper.createObjectNode() : null;

  Iterator<String> names = flattened.fieldNames();
  while (names.hasNext()) {
    String key = names.next();
    JsonNode currentVal = unflattened;
    String objKey = null;
    Integer aryIdx = null;

    Matcher matcher = keyPartPattern().matcher(key);
    while (matcher.find()) {
      String keyPart = matcher.group();

      if (objKey != null ^ aryIdx != null) {
        if (isJsonArray(keyPart)) {
          currentVal = findOrCreateJsonArray(currentVal, objKey, aryIdx);
          objKey = null;
          aryIdx = extractIndex(keyPart);
        } else { // JSON object
          if (flattened.get(key).isArray()) { // KEEP_ARRAYS mode
            flattened.set(key,
                unflattenArray((ArrayNode) flattened.get(key)));
          }
          currentVal = findOrCreateJsonObject(currentVal, objKey, aryIdx);
          objKey = extractKey(keyPart);
          aryIdx = null;
        }
      }

      if (objKey == null && aryIdx == null) {
        if (isJsonArray(keyPart)) {
          aryIdx = extractIndex(keyPart);
          if (currentVal == null) currentVal = mapper.createArrayNode();
        } else { // JSON object
          objKey = extractKey(keyPart);
          if (currentVal == null) currentVal = mapper.createObjectNode();
        }
      }

      if (unflattened == null) unflattened = currentVal;
    }

    setUnflattenedValue(flattened, key, currentVal, objKey, aryIdx);
  }

  sw.append(writeByConfig(unflattened));
  return sw.toString();
}