|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2020 Intuit |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + *******************************************************************************/ |
| 16 | +package com.intuit.ipp.serialization; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | + |
| 20 | +import com.fasterxml.jackson.core.JsonGenerator; |
| 21 | +import com.fasterxml.jackson.databind.JsonSerializer; |
| 22 | +import com.fasterxml.jackson.databind.SerializerProvider; |
| 23 | +import com.intuit.ipp.data.RecurringTransaction; |
| 24 | +import com.intuit.ipp.exception.SerializationException; |
| 25 | +import com.intuit.ipp.util.Logger; |
| 26 | + |
| 27 | +/** |
| 28 | + * Custom Json serializer class to handle RecurringTransaction while marshalling |
| 29 | + * |
| 30 | + */ |
| 31 | +/** |
| 32 | + * @author dderose |
| 33 | + * |
| 34 | + */ |
| 35 | +public class RecurringTransactionSerializer extends JsonSerializer<RecurringTransaction> { |
| 36 | + |
| 37 | + /** |
| 38 | + * logger instance |
| 39 | + */ |
| 40 | + private static final org.slf4j.Logger LOG = Logger.getLogger(); |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | + @Override |
| 45 | + public void serialize(RecurringTransaction recurringTransaction, JsonGenerator jgen, SerializerProvider provider) throws IOException { |
| 46 | + |
| 47 | + jgen.writeStartObject(); |
| 48 | + |
| 49 | + // for IntuitObject |
| 50 | + if (recurringTransaction.getIntuitObject() != null) { |
| 51 | + jgen.writeFieldName(recurringTransaction.getIntuitObject().getValue().getClass().getSimpleName()); |
| 52 | + try { |
| 53 | + jgen.writeNumber(new JSONSerializer().serialize(recurringTransaction.getIntuitObject())); |
| 54 | + } catch (SerializationException e) { |
| 55 | + LOG.error("SerializationException while generating Json for IntuitObject in RecurringTransactionSerializer.", e); |
| 56 | + throw new IOException(e); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | + jgen.writeEndObject(); |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | +} |
0 commit comments