/*
 *  Copyright (c) 2016 Touch Instinct
 *
 *  This file is part of RoboSwag library.
 *
 *  Licensed 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 ru.touchin.templates.logansquare;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.bluelinelabs.logansquare.typeconverters.TypeConverter;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;

import org.joda.time.DateTime;

import java.io.IOException;

import ru.touchin.roboswag.core.log.Lc;

/**
 * LoganSquare converter for joda.time.DateTime
 */
public class LoganSquareJodaTimeConverter implements TypeConverter<DateTime> {

    @Override
    public DateTime parse(@NonNull final JsonParser jsonParser) throws IOException {
        final String dateString = jsonParser.getValueAsString();
        try {
            return new DateTime(dateString);
        } catch (RuntimeException e) {
            Lc.assertion(e);
        }
        return null;
    }

    @Override
    public void serialize(@Nullable final DateTime object,
                          @Nullable final String fieldName,
                          final boolean writeFieldNameForObject,
                          @NonNull final JsonGenerator jsonGenerator)
            throws IOException {
        if (fieldName != null) {
            jsonGenerator.writeStringField(fieldName, object.toString());
        } else {
            jsonGenerator.writeString(object.toString());
        }
    }

}

@@ -1355,16 +1355,85 @@
         
+if (dateString == null) %7B%0A            return null;%0A        %7D%0A        
 try %7B%0A  
@@ -1452,12 +1452,8 @@
 turn
- new
  Dat
@@ -1457,16 +1457,22 @@
 DateTime
+.parse
 (dateStr
@@ -1970,16 +1970,33 @@
 eldName,
+ object != null ?
  object.
@@ -1997,32 +1997,39 @@
 bject.toString()
+ : null
 );%0A        %7D els
@@ -2072,24 +2072,41 @@
 tring(object
+ != null ? object
 .toString())
@@ -2104,16 +2104,23 @@
 String()
+ : null
 );%0A     

add null objects support