View Javadoc

    1   /**
    2    * Copyright (c) 2002-2011 "Neo Technology,"
    3    * Network Engine for Objects in Lund AB [http://neotechnology.com]
    4    *
    5    * This file is part of Neo4j.
    6    *
    7    * Neo4j is free software: you can redistribute it and/or modify
    8    * it under the terms of the GNU General Public License as published by
    9    * the Free Software Foundation, either version 3 of the License, or
    10   * (at your option) any later version.
    11   *
    12   * This program is distributed in the hope that it will be useful,
    13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    15   * GNU General Public License for more details.
    16   *
    17   * You should have received a copy of the GNU General Public License
    18   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    19   */
    20  package org.neo4j.server.rest.repr.formats;
    21
    22  import java.net.URI;
    23  import java.net.URISyntaxException;
    24  import java.util.ArrayList;
    25  import java.util.Collections;
    26  import java.util.HashMap;
    27  import java.util.List;
    28  import java.util.Map;
    29
    30  import javax.ws.rs.core.MediaType;
    31
    32  import org.neo4j.server.rest.domain.JsonHelper;
    33  import org.neo4j.server.rest.domain.JsonParseException;
    34  import org.neo4j.server.rest.repr.BadInputException;
    35  import org.neo4j.server.rest.repr.ListWriter;
    36  import org.neo4j.server.rest.repr.MappingWriter;
    37  import org.neo4j.server.rest.repr.RepresentationFormat;
    38
    39  public class JsonFormat extends RepresentationFormat
    40  {
    41      public JsonFormat()
    42      {
    43          super( MediaType.APPLICATION_JSON_TYPE );
    44      }
    45
    46      @Override
    47      protected ListWriter serializeList( String type )
    48      {
    49          return new ListWrappingWriter( new ArrayList<Object>() );
    50      }
    51
    52      @Override
    53      protected String complete( ListWriter serializer )
    54      {
    55          return JsonHelper.createJsonFrom( ( (ListWrappingWriter) serializer ).data );
    56      }
    57
    58      @Override
    59      protected MappingWriter serializeMapping( String type )
    60      {
    61          return new MapWrappingWriter( new HashMap<String, Object>() );
    62      }
    63
    64      @Override
    65      protected String complete( MappingWriter serializer )
    66      {
    67          return JsonHelper.createJsonFrom( ( (MapWrappingWriter) serializer ).data );
    68      }
    69
    70      @Override
    71      protected String serializeValue( String type, Object value )
    72      {
    73          return JsonHelper.createJsonFrom( value );
    74      }
    75
    76      private boolean empty( String input )
    77      {
    78          return input == null || "".equals( input.trim() );
    79      }
    80
    81      @Override
    82      public Map<String, Object> readMap( String input ) throws BadInputException
    83      {
    84          if ( empty( input ) ) return Collections.emptyMap();
    85          try
    86          {
    87              return JsonHelper.jsonToMap( stripByteOrderMark( input ) );
    88          }
    89          catch ( JsonParseException ex )
    90          {
    91              throw new BadInputException( ex );
    92          }
    93      }
    94
    95      @Override
    96      public List<Object> readList( String input )
    97      {
    98          // TODO tobias: Implement readList() [Dec 10, 2010]
    99          throw new UnsupportedOperationException( "Not implemented: JsonInput.readList()" );
    100     }
    101
    102     @Override
    103     public Object readValue( String input ) throws BadInputException
    104     {
    105         if ( empty( input ) ) return Collections.emptyMap();
    106         try
    107         {
    108             return JsonHelper.jsonToSingleValue( stripByteOrderMark( input ) );
    109         }
    110         catch ( JsonParseException ex )
    111         {
    112             throw new BadInputException( ex );
    113         }
    114     }
    115
    116     @Override
    117     public URI readUri( String input ) throws BadInputException
    118     {
    119         try
    120         {
    121             return new URI( readValue( input ).toString() );
    122         }
    123         catch ( URISyntaxException e )
    124         {
    125             throw new BadInputException( e );
    126         }
    127     }
    128
    129     private String stripByteOrderMark( String string )
    130     {
    131         if ( string != null && string.length() > 0 && string.charAt( 0 ) == 0xfeff )
    132         {
    133             return string.substring( 1 );
    134         }
    135         return string;
    136     }
    137 }