Project

General

Profile

Revision e6044ca7

IDe6044ca78c8161aa67ec0fa788361a9caa079f2a
Parent 8526e03d
Child f33a82a7

Added by Andreas Kohlbecker almost 3 years ago

fix #7979 DateTimeJSONValueProcessor returns null for null

View differences:

cdmlib-remote/src/main/java/eu/etaxonomy/cdm/remote/json/processor/value/DateTimeJSONValueProcessor.java
1 1
/**
2 2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy 
3
* European Distributed Institute of Taxonomy
4 4
* http://www.e-taxonomy.eu
5
* 
5
*
6 6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7 7
* See LICENSE.TXT at the top of this package for the full license terms.
8 8
*/
9 9

  
10 10
package eu.etaxonomy.cdm.remote.json.processor.value;
11 11

  
12
import net.sf.json.JsonConfig;
13
import net.sf.json.processors.JsonValueProcessor;
14

  
15 12
import org.apache.log4j.Logger;
16 13
import org.joda.time.DateTime;
17 14
import org.joda.time.format.DateTimeFormatter;
18 15
import org.joda.time.format.ISODateTimeFormat;
19 16

  
17
import net.sf.json.JsonConfig;
18
import net.sf.json.processors.JsonValueProcessor;
19

  
20 20
/**
21 21
 * @author n.hoffmann
22 22
 * @since 24.07.2008
......
27 27
			.getLogger(DateTimeJSONValueProcessor.class);
28 28

  
29 29
	private static DateTimeFormatter iso8601Format = ISODateTimeFormat.dateTime();
30
	
30

  
31 31
	/* (non-Javadoc)
32 32
	 * @see net.sf.json.processors.JsonValueProcessor#processArrayValue(java.lang.Object, net.sf.json.JsonConfig)
33 33
	 */
34
	public Object processArrayValue(Object object, JsonConfig jsonConfig) {
34
	@Override
35
    public Object processArrayValue(Object object, JsonConfig jsonConfig) {
35 36
		DateTime dateTime = (DateTime) object;
36
        return DateTimeJSONValueProcessor.iso8601Format.print(dateTime);
37
        return formatDateTime(object);
37 38
	}
38 39

  
39 40
	/* (non-Javadoc)
40 41
	 * @see net.sf.json.processors.JsonValueProcessor#processObjectValue(java.lang.String, java.lang.Object, net.sf.json.JsonConfig)
41 42
	 */
42
	public Object processObjectValue(String key, Object object,
43
	@Override
44
    public Object processObjectValue(String key, Object object,
43 45
			JsonConfig jsonConfig) {
44
		DateTime dateTime = (DateTime) object;
45
        return DateTimeJSONValueProcessor.iso8601Format.print(dateTime);
46
	    return formatDateTime(object);
46 47
	}
48

  
49
    /**
50
     * @param object
51
     * @return
52
     */
53
    public Object formatDateTime(Object object) {
54
        if(object != null){
55
	        DateTime dateTime = (DateTime) object;
56
	        // WARNING! null means now!
57
	        return DateTimeJSONValueProcessor.iso8601Format.print(dateTime);
58
	    } else {
59
	        return null;
60
	    }
61
    }
47 62
}
cdmlib-remote/src/test/java/eu/etaxonomy/cdm/remote/json/processor/value/DateTimeJSONValueProcessorTest.java
1
/**
2
* Copyright (C) 2019 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.remote.json.processor.value;
10

  
11
import static org.junit.Assert.assertEquals;
12
import static org.junit.Assert.assertNull;
13

  
14
import org.joda.time.DateTime;
15
import org.junit.Test;
16

  
17
/**
18
 * @author a.kohlbecker
19
 * @since Jan 15, 2019
20
 *
21
 */
22
public class DateTimeJSONValueProcessorTest {
23

  
24
    @Test
25
    public void testFormatDateTime(){
26
        DateTimeJSONValueProcessor processor = new DateTimeJSONValueProcessor();
27
        DateTime dateTime = DateTime.parse("2010-06-30T01:20");
28
        assertEquals("2010-06-30T01:20:00.000+02:00", processor.formatDateTime(dateTime));
29
    }
30

  
31
    @Test
32
    public void testFormatDateTimeNull(){
33
        DateTimeJSONValueProcessor processor = new DateTimeJSONValueProcessor();
34
        assertNull(processor.formatDateTime(null));
35
    }
36

  
37
}

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)