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

The following examples show how to use org.nd4j.shade.jackson.databind.ObjectMapper#configure() . 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: 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 2
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 3
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 4
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 5
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 6
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 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: ProfilingListener.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Get a new JSON mapper for use in serializing/deserializing JSON format
 */
public static ObjectMapper jsonMapper() {
    ObjectMapper json = new ObjectMapper();
    json.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    json.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    json.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, false);
    json.disable(SerializationFeature.INDENT_OUTPUT);   //One line

    return json;
}
 
Example 10
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 11
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 12
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 13
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 14
Source File: SequenceElement.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
public static ObjectMapper mapper() {
    /*
          DO NOT ENABLE INDENT_OUTPUT FEATURE
          we need THIS json to be single-line
      */
    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);
    return ret;
}
 
Example 15
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 16
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 17
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 18
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);
    }
}