Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2012 Amazon.com, Inc. or its affiliates. All Rights Reserved.
   *
   * Licensed under the Apache License, Version 2.0 (the "License").
   * You may not use this file except in compliance with the License.
   * A copy of the License is located at
   *
   *  http://aws.amazon.com/apache2.0
   *
  * or in the "license" file accompanying this file. This file 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.amazonaws.services.simpleworkflow.flow;
 
 
Implements conversion through Jackson JSON processor. Consult its documentation on how to ensure that classes are serializable, configure their serialization through annotations and com.fasterxml.jackson.databind.ObjectMapper parameters.

Note that default configuration used by this class includes class name of the every serialized value into the produced JSON. It is done to support polymorphic types out of the box. But in some cases it might be beneficial to disable polymorphic support as it produces much more concise and portable output.

Author(s):
fateev
 
 public class JsonDataConverter extends DataConverter {
 
     protected final ObjectMapper mapper;

    
Create instance of the converter that uses ObjectMapper with Feature set to false and default typing set to com.fasterxml.jackson.databind.ObjectMapper.DefaultTyping.NON_FINAL.
 
     public JsonDataConverter() {
         this(new ObjectMapper());
         // ignoring unknown properties makes us more robust to changes in the schema
 
         // This will allow including type information all non-final types.  This allows correct 
         // serialization/deserialization of generic collections, for example List<MyType>. 
     }

    
Create instance of the converter that uses com.fasterxml.jackson.databind.ObjectMapper configured externally.
 
     public JsonDataConverter(ObjectMapper mapper) {
         this. = mapper;
     }
 
     @Override
     public String toData(Object valuethrows DataConverterException {
         try {
             return .writeValueAsString(value);
         }
         catch (JsonGenerationException e) {
             throwDataConverterException(evalue);
         }
         catch (JsonMappingException e) {
             throwDataConverterException(evalue);
         }
         catch (IOException e) {
             throwDataConverterException(evalue);
         }
         throw new IllegalStateException("not reachable");
     }
 
     private void throwDataConverterException(Throwable eObject value) {
         if (value == null) {
             throw new DataConverterException("Failure serializing null value"e);
         }
         throw new DataConverterException("Failure serializing \"" + value + "\" of type \"" + value.getClass() + "\""e);
     }
 
     @Override
     public <T> T fromData(String serializedClass<T> valueTypethrows DataConverterException {
         try {
             return .readValue(serializedvalueType);
         }
         catch (JsonParseException e) {
             throw new DataConverterException(e);
        }
        catch (JsonMappingException e) {
            throw new DataConverterException(e);
        }
        catch (IOException e) {
            throw new DataConverterException(e);
        }
    }
New to GrepCode? Check out our FAQ X