Java Code Examples for org.nd4j.shade.jackson.databind.ObjectMapper#enable()

The following examples show how to use org.nd4j.shade.jackson.databind.ObjectMapper#enable() . 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: Schema.java    From DataVec with Apache License 2.0 6 votes vote down vote up
private String toJacksonString(JsonFactory factory) {
    ObjectMapper om = new ObjectMapper(factory);
    om.registerModule(new JodaModule());
    om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    om.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    om.enable(SerializationFeature.INDENT_OUTPUT);
    om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
    om.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
    String str;
    try {
        str = om.writeValueAsString(this);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    return str;
}
 
Example 2
Source File: TestSchedules.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testJson() throws Exception {

    ObjectMapper om = new ObjectMapper();
    om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    om.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    om.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
    om.enable(SerializationFeature.INDENT_OUTPUT);

    ISchedule[] schedules = new ISchedule[]{
            new ExponentialSchedule(ScheduleType.ITERATION, 1.0, 0.5),
            new InverseSchedule(ScheduleType.ITERATION, 1.0, 0.5, 2),
            new MapSchedule.Builder(ScheduleType.ITERATION).add(0, 1.0).add(10,0.5).build(),
            new PolySchedule(ScheduleType.ITERATION, 1.0, 2, 100),
            new SigmoidSchedule(ScheduleType.ITERATION, 1.0, 0.5, 10),
            new StepSchedule(ScheduleType.ITERATION, 1.0, 0.9, 100)};


    for(ISchedule s : schedules){
        String json = om.writeValueAsString(s);
        ISchedule fromJson = om.readValue(json, ISchedule.class);
        assertEquals(s, fromJson);
    }
}
 
Example 3
Source File: LossFunctionJson.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testJsonSerialization() throws Exception {

    INDArray w = Nd4j.create(new double[] {1.0, 2.0, 3.0});

    ILossFunction[] lossFns = new ILossFunction[] {new LossBinaryXENT(), new LossBinaryXENT(w),
                    new LossCosineProximity(), new LossHinge(), new LossKLD(), new LossL1(), new LossL1(w),
                    new LossL2(), new LossL2(w), new LossMAE(), new LossMAE(w), new LossMAPE(), new LossMAPE(w),
                    new LossMCXENT(), new LossMCXENT(w), new LossMSE(), new LossMSE(w), new LossMSLE(),
                    new LossMSLE(w), new LossNegativeLogLikelihood(), new LossNegativeLogLikelihood(w),
                    new LossPoisson(), new LossSquaredHinge(), new LossMultiLabel()};

    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    mapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
    mapper.enable(SerializationFeature.INDENT_OUTPUT);

    for (ILossFunction lf : lossFns) {
        String asJson = mapper.writeValueAsString(lf);
        //            System.out.println(asJson);

        ILossFunction fromJson = mapper.readValue(asJson, ILossFunction.class);
        assertEquals(lf, fromJson);
    }
}
 
Example 4
Source File: Schema.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
private String toJacksonString(JsonFactory factory) {
    ObjectMapper om = new ObjectMapper(factory);
    om.registerModule(new JodaModule());
    om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    om.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    om.enable(SerializationFeature.INDENT_OUTPUT);
    om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
    om.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
    String str;
    try {
        str = om.writeValueAsString(this);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    return str;
}
 
Example 5
Source File: TestSchedules.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testJson() throws Exception {

    ObjectMapper om = new ObjectMapper();
    om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    om.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    om.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
    om.enable(SerializationFeature.INDENT_OUTPUT);

    ISchedule[] schedules = new ISchedule[]{
            new ExponentialSchedule(ScheduleType.ITERATION, 1.0, 0.5),
            new InverseSchedule(ScheduleType.ITERATION, 1.0, 0.5, 2),
            new MapSchedule.Builder(ScheduleType.ITERATION).add(0, 1.0).add(10,0.5).build(),
            new PolySchedule(ScheduleType.ITERATION, 1.0, 2, 100),
            new SigmoidSchedule(ScheduleType.ITERATION, 1.0, 0.5, 10),
            new StepSchedule(ScheduleType.ITERATION, 1.0, 0.9, 100),
            new CycleSchedule(ScheduleType.ITERATION, 1.5, 100)
    };


    for(ISchedule s : schedules){
        String json = om.writeValueAsString(s);
        ISchedule fromJson = om.readValue(json, ISchedule.class);
        assertEquals(s, fromJson);
    }
}
 
Example 6
Source File: LossFunctionJson.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testJsonSerialization() throws Exception {

    INDArray w = Nd4j.create(new double[] {1.0, 2.0, 3.0});

    ILossFunction[] lossFns = new ILossFunction[] {new LossBinaryXENT(), new LossBinaryXENT(w),
                    new LossCosineProximity(), new LossHinge(), new LossKLD(), new LossL1(), new LossL1(w),
                    new LossL2(), new LossL2(w), new LossMAE(), new LossMAE(w), new LossMAPE(), new LossMAPE(w),
                    new LossMCXENT(), new LossMCXENT(w), new LossMSE(), new LossMSE(w), new LossMSLE(),
                    new LossMSLE(w), new LossNegativeLogLikelihood(), new LossNegativeLogLikelihood(w),
                    new LossPoisson(), new LossSquaredHinge(), new LossMultiLabel()};

    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    mapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
    mapper.enable(SerializationFeature.INDENT_OUTPUT);

    for (ILossFunction lf : lossFns) {
        String asJson = mapper.writeValueAsString(lf);
        //            System.out.println(asJson);

        ILossFunction fromJson = mapper.readValue(asJson, ILossFunction.class);
        assertEquals(lf, fromJson);
    }
}
 
Example 7
Source File: JsonMappers.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
private static ObjectMapper configureMapper(ObjectMapper ret) {
    ret.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    ret.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    ret.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, false);
    ret.enable(SerializationFeature.INDENT_OUTPUT);
    SimpleModule atomicModule = new SimpleModule();
    atomicModule.addSerializer(AtomicDouble.class, new JsonSerializerAtomicDouble());
    atomicModule.addSerializer(AtomicBoolean.class, new JsonSerializerAtomicBoolean());
    atomicModule.addDeserializer(AtomicDouble.class, new JsonDeserializerAtomicDouble());
    atomicModule.addDeserializer(AtomicBoolean.class, new JsonDeserializerAtomicBoolean());
    ret.registerModule(atomicModule);
    //Serialize fields only, not using getters
    ret.setVisibilityChecker(ret.getSerializationConfig().getDefaultVisibilityChecker()
            .withFieldVisibility(JsonAutoDetect.Visibility.ANY)
            .withGetterVisibility(JsonAutoDetect.Visibility.NONE)
            .withSetterVisibility(JsonAutoDetect.Visibility.NONE)
            .withCreatorVisibility(JsonAutoDetect.Visibility.ANY)
    );
    return ret;
}
 
Example 8
Source File: BaseEvaluation.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
private static ObjectMapper configureMapper(ObjectMapper ret) {
    ret.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    ret.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    ret.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, false);
    ret.enable(SerializationFeature.INDENT_OUTPUT);
    SimpleModule atomicModule = new SimpleModule();
    atomicModule.addSerializer(AtomicDouble.class,new JsonSerializerAtomicDouble());
    atomicModule.addSerializer(AtomicBoolean.class,new JsonSerializerAtomicBoolean());
    atomicModule.addDeserializer(AtomicDouble.class,new JsonDeserializerAtomicDouble());
    atomicModule.addDeserializer(AtomicBoolean.class,new JsonDeserializerAtomicBoolean());
    ret.registerModule(atomicModule);
    //Serialize fields only, not using getters
    ret.setVisibilityChecker(ret.getSerializationConfig().getDefaultVisibilityChecker()
            .withFieldVisibility(JsonAutoDetect.Visibility.ANY)
            .withGetterVisibility(JsonAutoDetect.Visibility.NONE)
            .withSetterVisibility(JsonAutoDetect.Visibility.NONE)
            .withCreatorVisibility(JsonAutoDetect.Visibility.NONE));
    return ret;
}
 
Example 9
Source File: TestJson.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
protected static ObjectMapper getObjectMapper(JsonFactory factory) {
    ObjectMapper om = new ObjectMapper(factory);
    om.registerModule(new JodaModule());
    om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    om.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    om.enable(SerializationFeature.INDENT_OUTPUT);
    om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
    om.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
    om.setVisibility(PropertyAccessor.CREATOR, JsonAutoDetect.Visibility.ANY);
    return om;
}
 
Example 10
Source File: JsonMapper.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
private static ObjectMapper getMapper(){
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    mapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
    mapper.enable(SerializationFeature.INDENT_OUTPUT);
    mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
    mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
    mapper.setVisibility(PropertyAccessor.CREATOR, JsonAutoDetect.Visibility.ANY);

    return mapper;
}
 
Example 11
Source File: BaseTrainingMaster.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
protected static ObjectMapper getNewMapper(JsonFactory jsonFactory) {
    ObjectMapper om = new ObjectMapper(jsonFactory);
    om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    om.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    om.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
    om.enable(SerializationFeature.INDENT_OUTPUT);
    om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
    om.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
    return om;
}
 
Example 12
Source File: StaticPageUtil.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
public static String renderHTMLContent(Component... components) throws Exception {

        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        mapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
        mapper.enable(SerializationFeature.INDENT_OUTPUT);

        Configuration cfg = new Configuration(new Version(2, 3, 23));

        // Where do we load the templates from:
        cfg.setClassForTemplateLoading(StaticPageUtil.class, "");

        // Some other recommended settings:
        cfg.setIncompatibleImprovements(new Version(2, 3, 23));
        cfg.setDefaultEncoding("UTF-8");
        cfg.setLocale(Locale.US);
        cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);

        ClassPathResource cpr = new ClassPathResource("assets/dl4j-ui.js");
        String scriptContents = IOUtils.toString(cpr.getInputStream(), "UTF-8");

        Map<String, Object> pageElements = new HashMap<>();
        List<ComponentObject> list = new ArrayList<>();
        int i = 0;
        for (Component c : components) {
            list.add(new ComponentObject(String.valueOf(i), mapper.writeValueAsString(c)));
            i++;
        }
        pageElements.put("components", list);
        pageElements.put("scriptcontent", scriptContents);


        Template template = cfg.getTemplate("staticpage.ftl");
        Writer stringWriter = new StringWriter();
        template.process(pageElements, stringWriter);

        return stringWriter.toString();
    }
 
Example 13
Source File: WordVectorSerializer.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
private static ObjectMapper getModelMapper() {
    ObjectMapper ret = new ObjectMapper();
    ret.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    ret.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    ret.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
    ret.enable(SerializationFeature.INDENT_OUTPUT);
    return ret;
}
 
Example 14
Source File: Hdf5Archive.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Read JSON-formatted string attribute.
 *
 * @param attribute HDF5 attribute to read as JSON formatted string.
 * @return JSON formatted string from HDF5 attribute
 * @throws UnsupportedKerasConfigurationException Unsupported Keras config
 */
private String readAttributeAsJson(Attribute attribute) throws UnsupportedKerasConfigurationException {
    synchronized (Hdf5Archive.LOCK_OBJECT) {
        VarLenType vl = attribute.getVarLenType();
        int currBufferLength = 2048;
        String s;
        /* TODO: find a less hacky way to do this.
         * Reading variable length strings (from attributes) is a giant
         * pain. There does not appear to be any way to determine the
         * length of the string in advance, so we use a hack: choose a
         * buffer size and read the config. If Jackson fails to parse
         * it, then we must not have read the entire config. Increase
         * buffer and repeat.
         */
        while (true) {
            byte[] attrBuffer = new byte[currBufferLength];
            BytePointer attrPointer = new BytePointer(currBufferLength);
            attribute.read(vl, attrPointer);
            attrPointer.get(attrBuffer);
            s = new String(attrBuffer);
            ObjectMapper mapper = new ObjectMapper();
            mapper.enable(DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY);
            try {
                mapper.readTree(s);
                break;
            } catch (IOException e) {
                //OK - we don't know how long the buffer needs to be, so we'll try again with larger buffer
            }

            if(currBufferLength == MAX_BUFFER_SIZE_BYTES){
                throw new UnsupportedKerasConfigurationException("Could not read abnormally long HDF5 attribute: size exceeds " + currBufferLength + " bytes");
            } else {
                currBufferLength = (int)Math.min(MAX_BUFFER_SIZE_BYTES, currBufferLength * 4L);
            }
        }
        vl.deallocate();
        return s;
    }
}
 
Example 15
Source File: ResourceFile.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
public static final ObjectMapper newMapper() {
    ObjectMapper ret = new ObjectMapper();
    ret.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    ret.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    ret.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
    ret.enable(SerializationFeature.INDENT_OUTPUT);
    return ret;
}
 
Example 16
Source File: JsonMappers.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
private static void configureMapper(ObjectMapper ret) {
    ret.registerModule(new JodaModule());
    ret.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    ret.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    ret.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
    ret.enable(SerializationFeature.INDENT_OUTPUT);
    ret.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
    ret.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
    ret.setVisibility(PropertyAccessor.CREATOR, JsonAutoDetect.Visibility.ANY);     //Need this otherwise JsonProperty annotations on constructors won't be seen
}
 
Example 17
Source File: Schema.java    From DataVec with Apache License 2.0 5 votes vote down vote up
private static Schema fromJacksonString(String str, JsonFactory factory) {
    ObjectMapper om = new ObjectMapper(factory);
    om.registerModule(new JodaModule());
    om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    om.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    om.enable(SerializationFeature.INDENT_OUTPUT);
    om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
    om.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
    try {
        return om.readValue(str, Schema.class);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}