2021-02-01 14:31:20 +09:00
|
|
|
/*
|
|
|
|
|
* ******************************************************************************
|
|
|
|
|
* *
|
|
|
|
|
* *
|
|
|
|
|
* * This program and the accompanying materials are made available under the
|
|
|
|
|
* * terms of the Apache License, Version 2.0 which is available at
|
|
|
|
|
* * https://www.apache.org/licenses/LICENSE-2.0.
|
|
|
|
|
* *
|
2021-02-01 17:47:29 +09:00
|
|
|
* * See the NOTICE file distributed with this work for additional
|
|
|
|
|
* * information regarding copyright ownership.
|
2021-02-01 14:31:20 +09:00
|
|
|
* * Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
|
* * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
|
* * License for the specific language governing permissions and limitations
|
|
|
|
|
* * under the License.
|
|
|
|
|
* *
|
|
|
|
|
* * SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
* *****************************************************************************
|
|
|
|
|
*/
|
2019-06-06 15:21:15 +03:00
|
|
|
|
|
|
|
|
package org.nd4j.serde.json;
|
|
|
|
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
2020-04-29 11:19:26 +10:00
|
|
|
import org.nd4j.common.primitives.AtomicBoolean;
|
|
|
|
|
import org.nd4j.common.primitives.AtomicDouble;
|
|
|
|
|
import org.nd4j.common.primitives.serde.JsonDeserializerAtomicBoolean;
|
|
|
|
|
import org.nd4j.common.primitives.serde.JsonDeserializerAtomicDouble;
|
|
|
|
|
import org.nd4j.common.primitives.serde.JsonSerializerAtomicBoolean;
|
|
|
|
|
import org.nd4j.common.primitives.serde.JsonSerializerAtomicDouble;
|
2022-09-20 15:40:53 +02:00
|
|
|
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
|
|
|
|
import com.fasterxml.jackson.databind.DeserializationFeature;
|
|
|
|
|
import com.fasterxml.jackson.databind.MapperFeature;
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
import com.fasterxml.jackson.databind.SerializationFeature;
|
|
|
|
|
import com.fasterxml.jackson.databind.module.SimpleModule;
|
|
|
|
|
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
|
2019-06-06 15:21:15 +03:00
|
|
|
|
|
|
|
|
@Slf4j
|
|
|
|
|
public class JsonMappers {
|
|
|
|
|
|
2022-10-21 15:19:32 +02:00
|
|
|
private static final ObjectMapper jsonMapper = configureMapper(new ObjectMapper());
|
|
|
|
|
private static final ObjectMapper yamlMapper = configureMapper(new ObjectMapper(new YAMLFactory()));
|
2019-06-06 15:21:15 +03:00
|
|
|
|
|
|
|
|
/**
|
2019-11-30 20:08:30 +11:00
|
|
|
* @return The default/primary ObjectMapper for deserializing JSON objects
|
2019-06-06 15:21:15 +03:00
|
|
|
*/
|
|
|
|
|
public static ObjectMapper getMapper(){
|
|
|
|
|
return jsonMapper;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-30 20:08:30 +11:00
|
|
|
/**
|
|
|
|
|
* @return The default/primary ObjectMapper for deserializing JSON objects
|
|
|
|
|
*/
|
|
|
|
|
public static ObjectMapper getYamlMapper(){
|
|
|
|
|
return jsonMapper;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static ObjectMapper configureMapper(ObjectMapper ret) {
|
2019-06-06 15:21:15 +03:00
|
|
|
ret.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
|
|
|
|
ret.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
|
2019-11-30 20:08:30 +11:00
|
|
|
ret.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, false);
|
2019-06-06 15:21:15 +03:00
|
|
|
ret.enable(SerializationFeature.INDENT_OUTPUT);
|
2019-11-30 20:08:30 +11:00
|
|
|
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;
|
2019-06-06 15:21:15 +03:00
|
|
|
}
|
|
|
|
|
}
|