001/*
002 * Copyright (c) 2016-2017 Chris K Wensel. All Rights Reserved.
003 *
004 * Project and contact information: http://www.cascading.org/
005 *
006 * This file is part of the Cascading project.
007 *
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 *     http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 */
020
021package cascading.nested.json.hadoop2;
022
023import java.io.IOException;
024import java.io.InputStream;
025import java.io.OutputStream;
026
027import com.fasterxml.jackson.databind.JsonNode;
028import com.fasterxml.jackson.databind.ObjectMapper;
029import com.fasterxml.jackson.dataformat.ion.IonObjectMapper;
030import org.apache.hadoop.conf.Configured;
031import org.apache.hadoop.io.serializer.Deserializer;
032import org.apache.hadoop.io.serializer.Serialization;
033import org.apache.hadoop.io.serializer.Serializer;
034
035/**
036 * Class JSONHadoopSerialization creates a Hadoop {@link Serialization} provider.
037 */
038public class JSONHadoopSerialization extends Configured implements Serialization
039  {
040  ObjectMapper mapper = new IonObjectMapper();
041
042  @Override
043  public boolean accept( Class type )
044    {
045    return JsonNode.class.isAssignableFrom( type );
046    }
047
048  @Override
049  public Serializer getSerializer( Class type )
050    {
051    return new Serializer()
052      {
053      private OutputStream out;
054
055      @Override
056      public void open( OutputStream out )
057        {
058        this.out = out;
059        }
060
061      @Override
062      public void serialize( Object object ) throws IOException
063        {
064        mapper.writeValue( out, object );
065        }
066
067      @Override
068      public void close() throws IOException
069        {
070        this.out.close();
071        this.out = null;
072        }
073      };
074    }
075
076  @Override
077  public Deserializer getDeserializer( Class type )
078    {
079    return new Deserializer()
080      {
081      private InputStream in;
082
083      @Override
084      public void open( InputStream in )
085        {
086        this.in = in;
087        }
088
089      @Override
090      public Object deserialize( Object object ) throws IOException
091        {
092        return mapper.readTree( in );
093        }
094
095      @Override
096      public void close() throws IOException
097        {
098        this.in.close();
099        this.in = null;
100        }
101      };
102    }
103  }