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

com.arangodb.velocypack.internal.VPackDeserializers Maven / Gradle / Ivy

There is a newer version: 3.1.0
Show newest version
/*
 * DISCLAIMER
 *
 * Copyright 2016 ArangoDB GmbH, Cologne, Germany
 *
 * 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.
 *
 * Copyright holder is ArangoDB GmbH, Cologne, Germany
 */

package com.arangodb.velocypack.internal;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Timestamp;
import java.text.ParseException;
import java.util.Date;
import java.util.UUID;

import javax.xml.bind.DatatypeConverter;

import com.arangodb.velocypack.VPackDeserializationContext;
import com.arangodb.velocypack.VPackDeserializer;
import com.arangodb.velocypack.VPackSlice;
import com.arangodb.velocypack.exception.VPackException;
import com.arangodb.velocypack.exception.VPackParserException;
import com.arangodb.velocypack.internal.util.DateUtil;

/**
 * @author Mark Vollmary
 *
 */
public class VPackDeserializers {

	private VPackDeserializers() {
		super();
	}

	public static final VPackDeserializer STRING = new VPackDeserializer() {
		@Override
		public String deserialize(
			final VPackSlice parent,
			final VPackSlice vpack,
			final VPackDeserializationContext context) throws VPackException {
			return vpack.getAsString();
		}
	};
	public static final VPackDeserializer BOOLEAN = new VPackDeserializer() {
		@Override
		public Boolean deserialize(
			final VPackSlice parent,
			final VPackSlice vpack,
			final VPackDeserializationContext context) throws VPackException {
			return vpack.getAsBoolean();
		}
	};
	public static final VPackDeserializer INTEGER = new VPackDeserializer() {
		@Override
		public Integer deserialize(
			final VPackSlice parent,
			final VPackSlice vpack,
			final VPackDeserializationContext context) throws VPackException {
			return vpack.getAsInt();
		}
	};
	public static final VPackDeserializer LONG = new VPackDeserializer() {
		@Override
		public Long deserialize(
			final VPackSlice parent,
			final VPackSlice vpack,
			final VPackDeserializationContext context) throws VPackException {
			return vpack.getAsLong();
		}
	};
	public static final VPackDeserializer SHORT = new VPackDeserializer() {
		@Override
		public Short deserialize(
			final VPackSlice parent,
			final VPackSlice vpack,
			final VPackDeserializationContext context) throws VPackException {
			return vpack.getAsShort();
		}
	};
	public static final VPackDeserializer DOUBLE = new VPackDeserializer() {
		@Override
		public Double deserialize(
			final VPackSlice parent,
			final VPackSlice vpack,
			final VPackDeserializationContext context) throws VPackException {
			return vpack.getAsDouble();
		}
	};
	public static final VPackDeserializer FLOAT = new VPackDeserializer() {
		@Override
		public Float deserialize(
			final VPackSlice parent,
			final VPackSlice vpack,
			final VPackDeserializationContext context) throws VPackException {
			return vpack.getAsFloat();
		}
	};
	public static final VPackDeserializer BIG_INTEGER = new VPackDeserializer() {
		@Override
		public BigInteger deserialize(
			final VPackSlice parent,
			final VPackSlice vpack,
			final VPackDeserializationContext context) throws VPackException {
			return vpack.getAsBigInteger();
		}
	};
	public static final VPackDeserializer BIG_DECIMAL = new VPackDeserializer() {
		@Override
		public BigDecimal deserialize(
			final VPackSlice parent,
			final VPackSlice vpack,
			final VPackDeserializationContext context) throws VPackException {
			return vpack.getAsBigDecimal();
		}
	};
	public static final VPackDeserializer NUMBER = new VPackDeserializer() {
		@Override
		public Number deserialize(
			final VPackSlice parent,
			final VPackSlice vpack,
			final VPackDeserializationContext context) throws VPackException {
			return vpack.getAsNumber();
		}
	};
	public static final VPackDeserializer CHARACTER = new VPackDeserializer() {
		@Override
		public Character deserialize(
			final VPackSlice parent,
			final VPackSlice vpack,
			final VPackDeserializationContext context) throws VPackException {
			return vpack.getAsChar();
		}
	};
	public static final VPackDeserializer DATE = new VPackDeserializer() {
		@Override
		public Date deserialize(
			final VPackSlice parent,
			final VPackSlice vpack,
			final VPackDeserializationContext context) throws VPackException {
			final Date date;
			if (vpack.isString()) {
				try {
					date = DateUtil.parse(vpack.getAsString());
				} catch (final ParseException e) {
					throw new VPackParserException(e);
				}
			} else {
				date = vpack.getAsDate();
			}
			return date;
		}
	};
	public static final VPackDeserializer SQL_DATE = new VPackDeserializer() {

		@Override
		public java.sql.Date deserialize(
			final VPackSlice parent,
			final VPackSlice vpack,
			final VPackDeserializationContext context) throws VPackException {
			final java.sql.Date date;
			if (vpack.isString()) {
				try {
					date = new java.sql.Date(DateUtil.parse(vpack.getAsString()).getTime());
				} catch (final ParseException e) {
					throw new VPackParserException(e);
				}
			} else {
				date = vpack.getAsSQLDate();
			}
			return date;
		}
	};
	public static final VPackDeserializer SQL_TIMESTAMP = new VPackDeserializer() {
		@Override
		public Timestamp deserialize(
			final VPackSlice parent,
			final VPackSlice vpack,
			final VPackDeserializationContext context) throws VPackException {
			final java.sql.Timestamp date;
			if (vpack.isString()) {
				try {
					date = new java.sql.Timestamp(DateUtil.parse(vpack.getAsString()).getTime());
				} catch (final ParseException e) {
					throw new VPackParserException(e);
				}
			} else {
				date = vpack.getAsSQLTimestamp();
			}
			return date;
		}
	};
	public static final VPackDeserializer VPACK = new VPackDeserializer() {
		@Override
		public VPackSlice deserialize(
			final VPackSlice parent,
			final VPackSlice vpack,
			final VPackDeserializationContext context) throws VPackException {
			return vpack;
		}
	};
	public static final VPackDeserializer UUID = new VPackDeserializer() {
		@Override
		public UUID deserialize(
			final VPackSlice parent,
			final VPackSlice vpack,
			final VPackDeserializationContext context) throws VPackException {
			return java.util.UUID.fromString(vpack.getAsString());
		}
	};
	public static final VPackDeserializer BYTE_ARRAY = new VPackDeserializer() {
		@Override
		public byte[] deserialize(
			final VPackSlice parent,
			final VPackSlice vpack,
			final VPackDeserializationContext context) throws VPackException {
			return DatatypeConverter.parseBase64Binary(vpack.getAsString());
		}

	};
	public static final VPackDeserializer BYTE = new VPackDeserializer() {
		@Override
		public Byte deserialize(
			final VPackSlice parent,
			final VPackSlice vpack,
			final VPackDeserializationContext context) {
			return vpack.getAsByte();
		}
	};

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy