Example usage for com.fasterxml.jackson.databind ObjectMapper setVisibilityChecker

List of usage examples for com.fasterxml.jackson.databind ObjectMapper setVisibilityChecker

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind ObjectMapper setVisibilityChecker.

Prototype

public void setVisibilityChecker(VisibilityChecker<?> vc) 

Source Link

Document

Method for setting currently configured visibility checker; object used for determining whether given property element (method, field, constructor) can be auto-detected or not.

Usage

From source file:com.vethrfolnir.TestJsonAfterUnmarshal.java

public static void main(String[] args) throws Exception {
    ArrayList<TestThing> tsts = new ArrayList<>();

    for (int i = 0; i < 21; i++) {

        final int nr = i;
        tsts.add(new TestThing() {
            {//w  ww.j  a  v a  2  s.  com
                id = 1028 * nr + 256;
                name = "Name-" + nr;
            }
        });
    }

    ObjectMapper mp = new ObjectMapper();
    mp.setVisibilityChecker(mp.getDeserializationConfig().getDefaultVisibilityChecker()
            .withCreatorVisibility(JsonAutoDetect.Visibility.NONE)
            .withFieldVisibility(JsonAutoDetect.Visibility.ANY)
            .withGetterVisibility(JsonAutoDetect.Visibility.NONE)
            .withIsGetterVisibility(JsonAutoDetect.Visibility.NONE)
            .withSetterVisibility(JsonAutoDetect.Visibility.NONE));

    mp.configure(SerializationFeature.INDENT_OUTPUT, true);

    ByteArrayOutputStream br = new ByteArrayOutputStream();
    mp.writeValue(System.err, tsts);
    mp.writeValue(br, tsts);

    ByteArrayInputStream in = new ByteArrayInputStream(br.toByteArray());
    tsts = mp.readValue(in, new TypeReference<ArrayList<TestThing>>() {
    });

    System.err.println();
    System.out.println("Got: " + tsts);
}

From source file:parser.JsonWriter.java

/**
 * Creates json file for the cloudDSFPlus with all new attributes.
 * /*from   ww  w  .j a  v  a  2s.co m*/
 * @param workbook
 * @throws JsonGenerationException
 * @throws JsonMappingException
 * @throws IOException
 */
private static void writeCloudDSFPlusJson(XSSFWorkbook workbook)
        throws JsonGenerationException, JsonMappingException, IOException {
    // instantiate parser for CloudDSFPlus and read excel
    CloudDSFPlusParser cloudDSFPlusParser = new CloudDSFPlusParser(workbook);
    CloudDSF cdsf = cloudDSFPlusParser.readExcel();
    // check the internal consistency and if successfull serialize data
    if (cdsf.checkSanity()) {
        // Helper Method
        // cdsf.printCloudDSF();
        // Jackson objectmapper and settings
        ObjectMapper mapper = new ObjectMapper();
        mapper.enable(SerializationFeature.INDENT_OUTPUT);
        // Ignore missing getters to serialize all values
        mapper.setVisibilityChecker(mapper.getSerializationConfig().getDefaultVisibilityChecker()
                .withFieldVisibility(JsonAutoDetect.Visibility.ANY)
                .withGetterVisibility(JsonAutoDetect.Visibility.NONE));
        mapper.setSerializationInclusion(Include.NON_NULL);
        // create json structure
        JsonNode rootNode = mapper.createObjectNode();
        ((ObjectNode) rootNode).putPOJO("cdsfPlus", cdsf);
        ((ObjectNode) rootNode).putPOJO("links", cdsf.getInfluencingDecisions());
        ((ObjectNode) rootNode).putPOJO("outcomeLinks", cdsf.getInfluencingOutcomes());
        // Serialize CloudDSFPlus into json file
        File file = new File("cloudDSFPlus.json");
        mapper.writeValue(file, rootNode);
        System.out.println("Knowledge Base has been successfully verified and exported");
    } else {
        // knowledge base is not valid abort serialization
        System.out.println("The knowledge base is not valid");
    }
}

From source file:parser.JsonWriter.java

/**
 * Generates json file for the CloudDSF avoiding any unnecessary attribute serialization.
 * // w  w w . j av a 2s . c o m
 * @param workbook
 * @throws JsonGenerationException
 * @throws JsonMappingException
 * @throws IOException
 */
private static void writeCloudDSFJson(XSSFWorkbook workbook)
        throws JsonGenerationException, JsonMappingException, IOException {
    // Instantiate parser to parse file for CloudDSF
    CloudDSFParser parser = new CloudDSFParser(workbook);
    // CloudDSF object representing all necessary information
    CloudDSF cdsf = parser.readExcel();
    // Helper Method to check content
    // cdsf.printCloudDSF();
    // Create task tree for legacy visualizations
    TaskTree taskTree = new TaskTree();
    taskTree.setTasks(cdsf.getTasks());

    // Jackson objectmapper and settings
    ObjectMapper mapper = new ObjectMapper();
    // Pretty Print
    mapper.enable(SerializationFeature.INDENT_OUTPUT);
    // If getter is found values will be serialized avoiding unnecessary attributes
    mapper.setVisibilityChecker(mapper.getSerializationConfig().getDefaultVisibilityChecker()
            .withFieldVisibility(JsonAutoDetect.Visibility.DEFAULT)
            .withGetterVisibility(JsonAutoDetect.Visibility.DEFAULT));
    // Ignore fields with null values to avoid serialization of empty lists
    mapper.setSerializationInclusion(Include.NON_NULL);
    // Write all relations into one list to conform to legacy implementation
    cdsf.setInfluencingRelations();
    // create json root node and add json objects
    JsonNode rootNode = mapper.createObjectNode();
    ((ObjectNode) rootNode).putPOJO("decisionTree", cdsf);
    ((ObjectNode) rootNode).putPOJO("taskTree", taskTree);
    ((ObjectNode) rootNode).putPOJO("linksArray", cdsf.getInfluencingRelations());
    // serialize CloudDSF into file
    File file = new File("cloudDSF.json");
    mapper.writeValue(file, rootNode);
}

From source file:org.jongo.marshall.jackson.JacksonProcessor.java

public static ObjectMapper createPreConfiguredMapper() {
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(FAIL_ON_UNKNOWN_PROPERTIES, false);
    mapper.configure(AUTO_DETECT_GETTERS, false);
    mapper.configure(AUTO_DETECT_SETTERS, false);
    mapper.setSerializationInclusion(NON_NULL);
    mapper.setVisibilityChecker(VisibilityChecker.Std.defaultInstance().withFieldVisibility(ANY));

    SimpleModule module = new SimpleModule("jongo", new Version(1, 0, 0, null, null, null));
    addBSONTypeSerializers(module);//from   ww w  .  ja  va  2 s.co  m
    mapper.registerModule(module);
    return mapper;
}

From source file:org.mango.marshall.jackson.JacksonProcessor.java

public static ObjectMapper createPreConfiguredMapper() {
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(FAIL_ON_UNKNOWN_PROPERTIES, false);
    mapper.configure(AUTO_DETECT_GETTERS, false);
    mapper.configure(AUTO_DETECT_SETTERS, false);
    mapper.setSerializationInclusion(NON_NULL);
    mapper.setVisibilityChecker(VisibilityChecker.Std.defaultInstance().withFieldVisibility(ANY));

    SimpleModule module = new SimpleModule("mango", new Version(1, 0, 0, null, null, null));
    addBSONTypeSerializers(module);// www. j  a v a2  s  .com
    mapper.registerModule(module);
    return mapper;
}

From source file:je.backit.rest.JacksonContextResolver.java

private static ObjectMapper init() {
    ObjectMapper om = new ObjectMapper();
    om.registerModule(new JSR310Module());
    om.registerModule(new JooqModule());
    om.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    om.getFactory().configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false)
            .configure(JsonGenerator.Feature.FLUSH_PASSED_TO_STREAM, false);
    om.configure(WRITE_DATES_AS_TIMESTAMPS, false);
    om.setVisibilityChecker(om.getSerializationConfig().getDefaultVisibilityChecker()
            .withIsGetterVisibility(NONE).withGetterVisibility(NONE).withFieldVisibility(ANY));
    return om;//  w w w.  j a va 2  s. c om
}

From source file:org.nohope.jongo.JacksonProcessor.java

@Nonnull
private static ObjectMapper createPreConfiguredMapper() {
    final ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new JodaModule());
    mapper.registerModule(new ColorModule());

    mapper.configure(FAIL_ON_UNKNOWN_PROPERTIES, false);
    mapper.configure(AUTO_DETECT_GETTERS, false);
    mapper.configure(AUTO_DETECT_SETTERS, false);
    mapper.setSerializationInclusion(NON_NULL);
    mapper.setVisibilityChecker(VisibilityChecker.Std.defaultInstance().withFieldVisibility(ANY));

    mapper.enableDefaultTypingAsProperty(ObjectMapper.DefaultTyping.NON_FINAL,
            JsonTypeInfo.Id.CLASS.getDefaultPropertyName());

    final SimpleModule module = new SimpleModule("jongo", Version.unknownVersion());
    module.addKeySerializer(Object.class, ComplexKeySerializer.S_OBJECT);
    module.addKeyDeserializer(String.class, ComplexKeyDeserializer.S_OBJECT);
    module.addKeyDeserializer(Object.class, ComplexKeyDeserializer.S_OBJECT);

    //addBSONTypeSerializers(module);

    mapper.registerModule(module);//w  w w. j  a  v  a  2s.c om
    return mapper;
}

From source file:de.fau.cs.inf2.tree.evaluation.TreeEvalIO.java

public static ObjectMapper createJSONMapper(final DataFormat format) {
    final ObjectMapper mapper;
    {// w w w  .  ja v  a 2 s.co  m
        switch (format) {
        case FORMAT_JSON: {
            mapper = new ObjectMapper();
            break;
        }
        default: {
            assert (false);
            return null;
        }
        }
    }

    // configure mapper
    {
        mapper.enable(SerializationFeature.INDENT_OUTPUT);
        mapper.setVisibilityChecker(mapper.getVisibilityChecker().with(JsonAutoDetect.Visibility.NONE));
        mapper.setSerializationInclusion(Include.NON_NULL);
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.S");
        mapper.setDateFormat(df);
        addMixIns(mapper, TimeSummary.class, MixInTimeSummary.class);
        addMixIns(mapper, DiffSummary.class, MixInDiffSummary.class);
        addMixIns(mapper, TreeMatcherTypeEnum.class, MixInTreeMatcherTypeEnum.class);
        addMixIns(mapper, PsoResult.class, MixInPSOResult.class);
        addMixIns(mapper, ValidationDecision.class, MixInValidationDecision.class);
        addMixIns(mapper, ValidationDecisionList.class, MixInValidationDecisionList.class);
        addMixIns(mapper, ValidationEntry.class, MixInValidationEntry.class);
        addMixIns(mapper, ValidationEnum.class, MixInValidationEnum.class);
        addMixIns(mapper, ValidationRating.class, MixInValidationRating.class);
        addMixIns(mapper, ValidationInputSummary.class, MixInValidationInputSummary.class);
        addMixIns(mapper, ValidationInfo.class, MixInValidationInfo.class);

    }

    return mapper;
}

From source file:org.gameontext.mediator.JsonProvider.java

public JsonProvider() {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.setVisibilityChecker(
            objectMapper.getVisibilityChecker().withFieldVisibility(JsonAutoDetect.Visibility.ANY));
    setMapper(objectMapper);/*  w ww  .j  ava2  s. c o  m*/
}

From source file:com.samlikescode.sandbox.jackson.mixin.MixinTest.java

@Test
public void mixinTest() throws Exception {
    ObjectMapper om = new ObjectMapper();
    om.setVisibilityChecker(om.getVisibilityChecker().with(JsonAutoDetect.Visibility.NONE));
    om.addMixIn(Person.class, PersonMixin.class);

    //        om.readValue()

    System.out.println(om.writeValueAsString(testPerson));
}