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

com.datastax.driver.extras.codecs.json.JacksonJsonCodec Maven / Gradle / Ivy

There is a newer version: 3.11.5
Show newest version
/*
 *      Copyright (C) 2012-2015 DataStax Inc.
 *
 *   Licensed 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 com.datastax.driver.extras.codecs.json;

import java.io.IOException;
import java.nio.ByteBuffer;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;

import com.datastax.driver.core.DataType;
import com.datastax.driver.core.ParseUtils;
import com.datastax.driver.core.ProtocolVersion;
import com.datastax.driver.core.TypeCodec;
import com.datastax.driver.core.exceptions.InvalidTypeException;
import com.datastax.driver.core.utils.Bytes;

/**
 * A JSON codec that uses the Jackson
 * library to perform serialization and deserialization of JSON objects.
 * 

* Note that this codec maps a single object to a single JSON structure at a time; * mapping of arrays or collections to root-level JSON arrays is not supported, * but such a codec can be easily crafted after this one. */ public class JacksonJsonCodec extends TypeCodec { private final ObjectMapper objectMapper = new ObjectMapper(); public JacksonJsonCodec(Class javaType) { super(DataType.varchar(), javaType); } @Override public ByteBuffer serialize(T value, ProtocolVersion protocolVersion) throws InvalidTypeException { if (value == null) return null; try { return ByteBuffer.wrap(objectMapper.writeValueAsBytes(value)); } catch (JsonProcessingException e) { throw new InvalidTypeException(e.getMessage(), e); } } @Override @SuppressWarnings("unchecked") public T deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) throws InvalidTypeException { if (bytes == null) return null; try { return (T)objectMapper.readValue(Bytes.getArray(bytes), toJacksonJavaType()); } catch (IOException e) { throw new InvalidTypeException(e.getMessage(), e); } } @Override public String format(T value) throws InvalidTypeException { if (value == null) return "NULL"; String json; try { json = objectMapper.writeValueAsString(value); } catch (IOException e) { throw new InvalidTypeException(e.getMessage(), e); } return ParseUtils.quote(json); } @Override @SuppressWarnings("unchecked") public T parse(String value) throws InvalidTypeException { if (value == null || value.isEmpty() || value.equalsIgnoreCase("NULL")) return null; if (!ParseUtils.isQuoted(value)) throw new InvalidTypeException("JSON strings must be enclosed by single quotes"); String json = ParseUtils.unquote(value); try { return (T)objectMapper.readValue(json, toJacksonJavaType()); } catch (IOException e) { throw new InvalidTypeException(e.getMessage(), e); } } /** * This method acts as a bridge between Guava's {@link com.google.common.reflect.TypeToken TypeToken} API, which is used * by the driver, and Jackson's {@link JavaType} API. * @return A {@link JavaType} instance corresponding to the codec's {@link #getJavaType() Java type}. */ protected JavaType toJacksonJavaType() { return TypeFactory.defaultInstance().constructType(getJavaType().getType()); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy