All Downloads are FREE. Search and download functionalities are using the official Maven repository.

co.elastic.clients.json.SimpleJsonpMapper Maven / Gradle / Ivy

There is a newer version: 8.17.0
Show newest version
/*
 * Licensed to Elasticsearch B.V. under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Elasticsearch B.V. licenses this file to you under
 * the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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.
 */

package co.elastic.clients.json;

import jakarta.json.JsonException;
import jakarta.json.spi.JsonProvider;
import jakarta.json.stream.JsonGenerator;

import java.util.HashMap;
import java.util.Map;

/**
 * A simple implementation of JsonpMapper that only handles classes of the Java API client.
 * 

* To handle application classes serialization and deserialization, consider using JacksonJsonpMapper or * JsonbJsonpMapper. */ public class SimpleJsonpMapper extends JsonpMapperBase { public static SimpleJsonpMapper INSTANCE = new SimpleJsonpMapper(true); public static SimpleJsonpMapper INSTANCE_REJECT_UNKNOWN_FIELDS = new SimpleJsonpMapper(false); private static final Map, JsonpSerializer> serializers = new HashMap<>(); private static final Map, JsonpDeserializer> deserializers = new HashMap<>(); static { serializers.put(String.class, (JsonpSerializer) (value, generator, mapper) -> generator.write(value)); serializers.put(Boolean.class, (JsonpSerializer) (value, generator, mapper) -> generator.write(value)); serializers.put(Boolean.TYPE, (JsonpSerializer) (value, generator, mapper) -> generator.write(value)); serializers.put(Integer.class, (JsonpSerializer) (value, generator, mapper) -> generator.write(value)); serializers.put(Integer.TYPE, (JsonpSerializer) (value, generator, mapper) -> generator.write(value)); serializers.put(Long.class, (JsonpSerializer) (value, generator, mapper) -> generator.write(value)); serializers.put(Long.TYPE, (JsonpSerializer) (value, generator, mapper) -> generator.write(value)); serializers.put(Float.class, (JsonpSerializer) (value, generator, mapper) -> generator.write(value)); serializers.put(Float.TYPE, (JsonpSerializer) (value, generator, mapper) -> generator.write(value)); serializers.put(Double.class, (JsonpSerializer) (value, generator, mapper) -> generator.write(value)); serializers.put(Double.TYPE, (JsonpSerializer) (value, generator, mapper) -> generator.write(value)); deserializers.put(String.class, JsonpDeserializer.stringDeserializer()); deserializers.put(Boolean.class, JsonpDeserializer.booleanDeserializer()); deserializers.put(Boolean.TYPE, JsonpDeserializer.booleanDeserializer()); deserializers.put(Integer.class, JsonpDeserializer.integerDeserializer()); deserializers.put(Integer.TYPE, JsonpDeserializer.integerDeserializer()); deserializers.put(Long.class, JsonpDeserializer.longDeserializer()); deserializers.put(Long.TYPE, JsonpDeserializer.longDeserializer()); deserializers.put(Float.class, JsonpDeserializer.floatDeserializer()); deserializers.put(Float.TYPE, JsonpDeserializer.floatDeserializer()); deserializers.put(Double.class, JsonpDeserializer.doubleDeserializer()); deserializers.put(Double.TYPE, JsonpDeserializer.doubleDeserializer()); } private final boolean ignoreUnknownFields; public SimpleJsonpMapper(boolean ignoreUnknownFields) { this.ignoreUnknownFields = ignoreUnknownFields; } public SimpleJsonpMapper() { // Lenient by default this(true); } @Override public boolean ignoreUnknownFields() { return ignoreUnknownFields; } @Override public JsonProvider jsonProvider() { return JsonpUtils.provider(); } @Override public void serialize(T value, JsonGenerator generator) { JsonpSerializer serializer = findSerializer(value); if (serializer == null) { @SuppressWarnings("unchecked") JsonpSerializer serializer_ = (JsonpSerializer)serializers.get(value.getClass()); serializer = serializer_; } if (serializer != null) { serializer.serialize(value, generator, this); } else { throw new JsonException( "Cannot find a serializer for type " + value.getClass().getName() + ". Consider using a full-featured JsonpMapper" ); } } @Override protected JsonpDeserializer getDefaultDeserializer(Class clazz) { @SuppressWarnings("unchecked") JsonpDeserializer deserializer = (JsonpDeserializer) deserializers.get(clazz); if (deserializer != null) { return deserializer; } else { throw new JsonException( "Cannot find a deserializer for type " + clazz.getName() + ". Consider using a full-featured JsonpMapper" ); } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy