com.buession.core.serializer.FastJsonJsonSerializer Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.
* See the NOTICE file distributed with this work for additional information regarding copyright ownership.
* The ASF 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.
*
* =========================================================================================================
*
* This software consists of voluntary contributions made by many individuals on behalf of the
* Apache Software Foundation. For more information on the Apache Software Foundation, please see
* .
*
* +-------------------------------------------------------------------------------------------------------+
* | License: http://www.apache.org/licenses/LICENSE-2.0.txt |
* | Author: Yong.Teng |
* | Copyright @ 2013-2020 Buession.com Inc. |
* +-------------------------------------------------------------------------------------------------------+
*/
package com.buession.core.serializer;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializeFilter;
import com.buession.core.serializer.type.TypeReference;
import com.buession.core.utils.Assert;
import java.nio.charset.Charset;
/**
* @author Yong.Teng
*/
public class FastJsonJsonSerializer extends AbstractJsonSerializer {
@Override
public String serialize(final V object) throws SerializerException{
Assert.isNull(object, "Object cloud not be null.");
return JSON.toJSONString(object);
}
@Override
public byte[] serializeAsBytes(final V object) throws SerializerException{
Assert.isNull(object, "Object cloud not be null.");
return JSON.toJSONBytes(object);
}
@Override
public byte[] serializeAsBytes(final V object, final String charsetName) throws SerializerException{
return serializeAsBytes(object, Charset.forName(charsetName));
}
@Override
public byte[] serializeAsBytes(final V object, final Charset charset) throws SerializerException{
Assert.isNull(object, "Object cloud not be null.");
return JSON.toJSONBytes(charset, object, SerializeConfig.globalInstance, new SerializeFilter[0], null, JSON
.DEFAULT_GENERATE_FEATURE);
}
@Override
public V deserialize(final String str) throws SerializerException{
Assert.isNull(str, "String cloud not be null.");
return JSON.parseObject(str, new com.alibaba.fastjson.TypeReference() {
});
}
@Override
public V deserialize(final String str, final Class clazz) throws SerializerException{
Assert.isNull(str, "String cloud not be null.");
return JSON.parseObject(str, clazz);
}
@Override
public V deserialize(final String str, final TypeReference type) throws SerializerException{
Assert.isNull(str, "String cloud not be null.");
return JSON.parseObject(str, type.getType());
}
@Override
public V deserialize(final byte[] bytes) throws SerializerException{
Assert.isNull(bytes, "Bytes cloud not be null.");
return JSON.parseObject(new String(bytes), new com.alibaba.fastjson.TypeReference() {
});
}
@Override
public V deserialize(byte[] bytes, Class clazz) throws SerializerException{
Assert.isNull(bytes, "Bytes cloud not be null.");
return JSON.parseObject(bytes, clazz);
}
@Override
public V deserialize(final byte[] bytes, final TypeReference type) throws SerializerException{
Assert.isNull(bytes, "Bytes cloud not be null.");
return deserialize(new String(bytes), type);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy