Package com.abiquo.model.adapter

Source Code of com.abiquo.model.adapter.MetadataJsonSerializer

/**
* Licensed to Abiquo Holdings S.L. (Abiquo) 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.
*/
package com.abiquo.model.adapter;

import java.io.IOException;
import java.util.Map;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.introspect.AnnotationIntrospectorPair;
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector;

public class MetadataJsonSerializer extends JsonSerializer<Map<String, Object>>
{

    @Override
    public void serialize(final Map<String, Object> value, final JsonGenerator jgen,
        final SerializerProvider provider) throws IOException, JsonProcessingException
    {
        if (value.containsKey("metadata"))
        {
            byte[] metadata =
                new ObjectMapper().setAnnotationIntrospector(
                    new AnnotationIntrospectorPair(new JacksonAnnotationIntrospector(),
                        new JaxbAnnotationIntrospector(TypeFactory.defaultInstance())))
                    .writeValueAsBytes(value.get("metadata"));
            jgen.writeRawValue(new String(metadata));
        }
        else
        {
            throw new JsonGenerationException("Missing metadata start tag");
        }
    }

}
TOP

Related Classes of com.abiquo.model.adapter.MetadataJsonSerializer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.