Example usage for com.fasterxml.jackson.databind DeserializationContext handleUnknownProperty

List of usage examples for com.fasterxml.jackson.databind DeserializationContext handleUnknownProperty

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind DeserializationContext handleUnknownProperty.

Prototype

public boolean handleUnknownProperty(JsonParser paramJsonParser, JsonDeserializer<?> paramJsonDeserializer,
            Object paramObject, String paramString) 

Source Link

Usage

From source file:no.ssb.jsonstat.v2.deser.DimensionDeserializer.java

private void parseCategory(Dimension.Builder dimension, JsonParser p, DeserializationContext ctxt)
        throws IOException {
    Map<String, String> index = null;
    Map<String, String> label = null;
    while (p.nextValue() != JsonToken.END_OBJECT) {
        switch (p.getCurrentName()) {
        case "index":
            index = parseIndex(p, ctxt);
            break;
        case "label":
            label = parseCategoryLabel(p, ctxt);
            break;
        case "unit":
            // TODO: Support units.
            parseUnit(p, ctxt);/* w w w .  j  av a2 s. c o  m*/
            break;
        default:
            ctxt.handleUnknownProperty(p, this, Dimension.Builder.class, p.getCurrentName());
            break;
        }
    }
    checkArgument(!(index == null && label == null), "either label or index is required");

    // Once we have everything, we can build the dimension.

    if (index == null) {
        checkArgument(label.size() >= 1,
                "category label must contain a least one element if " + "no index is provided");
        dimension.withIndexedLabels(ImmutableMap.copyOf(label));
        return;
    }
    if (label == null) {
        dimension.withCategories(ImmutableSet.copyOf(index.keySet()));
        return;
    }

    // TODO: Maybe the checks should reside inside the builder?
    checkArgument(label.size() == index.size(), "label and index's sizes were inconsistent");

    ImmutableMap<String, String> withIndex = index.keySet().stream()
            .collect(toImmutableMap(Function.identity(), label::get));
    dimension.withIndexedLabels(withIndex);

}

From source file:no.ssb.jsonstat.v2.deser.DimensionDeserializer.java

@Override
public Dimension.Builder deserialize(JsonParser p, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {

    // Get the name first.
    String name = parseName(p, ctxt);

    if (p.getCurrentToken() == JsonToken.START_OBJECT) {
        p.nextToken();/*w  w  w. ja va 2 s.c om*/
    }

    Dimension.Builder dimension;
    dimension = Dimension.create(name);

    while (p.nextValue() != JsonToken.END_OBJECT) {
        switch (p.getCurrentName()) {
        case "category":
            parseCategory(dimension, p, ctxt);
            break;
        case "label":
            dimension.withLabel(parseLabel(p, ctxt));
            break;
        default:
            ctxt.handleUnknownProperty(p, this, Dimension.Builder.class, p.getCurrentName());
            break;
        }
    }

    return dimension;
}

From source file:no.ssb.jsonstat.v2.deser.DatasetDeserializer.java

@Override
public DatasetBuildable deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
    if (p.getCurrentToken() == JsonToken.START_OBJECT) {
        p.nextToken();/*from   www .  ja  v a2s. co  m*/
    }

    Set<String> ids = Collections.emptySet();
    List<Integer> sizes = Collections.emptyList();
    Multimap<String, String> roles = ArrayListMultimap.create();
    Map<String, Dimension.Builder> dims = Collections.emptyMap();
    List<Number> values = Collections.emptyList();

    DatasetBuilder builder = Dataset.create();
    Optional<String> version = Optional.empty();
    Optional<String> clazz = Optional.empty();
    Optional<ObjectNode> extension = Optional.empty();

    while (p.nextValue() != JsonToken.END_OBJECT) {
        switch (p.getCurrentName()) {
        case "label":
            builder.withLabel(_parseString(p, ctxt));
            break;
        case "source":
            builder.withSource(_parseString(p, ctxt));
            break;
        case "href":
            break;
        case "updated":
            Instant updated = parseEcmaDate(_parseString(p, ctxt));
            builder.updatedAt(updated);
            break;
        case "value":
            values = parseValues(p, ctxt);
            break;
        case "dimension":
            if (!version.orElse("1.x").equals("2.0")) {
                dims = Maps.newHashMap();
                // Deal with the id, size and role inside dimension.
                while (p.nextValue() != JsonToken.END_OBJECT) {
                    switch (p.getCurrentName()) {
                    case "id":
                        ids = p.readValueAs(ID_SET);
                        break;
                    case "size":
                        sizes = p.readValueAs(SIZE_LIST);
                        break;
                    case "role":
                        roles = p.readValueAs(ROLE_MULTIMAP);
                        break;
                    default:
                        dims.put(p.getCurrentName(), ctxt.readValue(p, Dimension.Builder.class));
                    }
                }
            } else {
                dims = p.readValueAs(DIMENSION_MAP);
            }
            break;
        case "id":
            ids = p.readValueAs(ID_SET);
            break;
        case "size":
            sizes = p.readValueAs(SIZE_LIST);
            break;
        case "role":
            roles = p.readValueAs(ROLE_MULTIMAP);
            break;
        case "extension":
            extension = Optional.of(ctxt.readValue(p, ObjectNode.class));
            break;
        case "link":
        case "status":
            // TODO
            p.skipChildren();
            break;
        case "version":
            version = Optional.of(_parseString(p, ctxt));
            break;
        case "class":
            // TODO
            clazz = Optional.of(_parseString(p, ctxt));
            break;
        default:
            boolean handled = ctxt.handleUnknownProperty(p, this, Dimension.Builder.class, p.getCurrentName());
            if (!handled)
                p.skipChildren();
            break;
        }
    }

    // Setup roles
    for (Map.Entry<String, String> dimRole : roles.entries()) {
        Dimension.Roles role = Dimension.Roles.valueOf(dimRole.getKey().toUpperCase());
        Dimension.Builder dimension = checkNotNull(dims.get(dimRole.getValue()),
                "could not assign the role {} to the dimension {}. The dimension did not exist", role,
                dimRole.getValue()

        );
        dimension.withRole(role);
    }

    List<Dimension.Builder> orderedDimensions = Lists.newArrayList();
    for (String dimensionName : ids) {
        orderedDimensions.add(dims.get(dimensionName));
    }

    // TODO: Check size?

    // Check ids and add to the data set.
    checkArgument(ids.size() == dims.size(), "dimension and size did not match");

    if (extension.isPresent()) {
        builder.withExtension(extension.get());
    }

    return builder.withDimensions(orderedDimensions).withValues(values);
}