| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| JsDateJsonBeanProcessor |
|
| 3.0;3 |
| 1 | /* | |
| 2 | * Copyright 2002-2009 the original author or authors. | |
| 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 | ||
| 17 | package net.sf.json.processors; | |
| 18 | ||
| 19 | import java.util.Calendar; | |
| 20 | import java.util.Date; | |
| 21 | ||
| 22 | import net.sf.json.JSONObject; | |
| 23 | import net.sf.json.JsonConfig; | |
| 24 | ||
| 25 | /** | |
| 26 | * Transforms a java.util.Date into a JSONObject ideal for JsDate conversion.<br> | |
| 27 | * Example:<br> | |
| 28 | * | |
| 29 | * <pre> | |
| 30 | { | |
| 31 | "minutes": 13, | |
| 32 | "seconds": 14, | |
| 33 | "hours": 12, | |
| 34 | "month": 5, | |
| 35 | "year": 2007, | |
| 36 | "day": 17, | |
| 37 | "milliseconds": 150 | |
| 38 | } | |
| 39 | </pre> | |
| 40 | * | |
| 41 | * @author Andres Almiray <aalmiray@users.sourceforge.net> | |
| 42 | */ | |
| 43 | 0 | public class JsDateJsonBeanProcessor implements JsonBeanProcessor { |
| 44 | ||
| 45 | /** | |
| 46 | * Processes the input bean into a compatible JsDate.<br> | |
| 47 | */ | |
| 48 | public JSONObject processBean( Object bean, JsonConfig jsonConfig ) { | |
| 49 | 0 | JSONObject jsonObject = null; |
| 50 | 0 | if( bean instanceof java.sql.Date ){ |
| 51 | 0 | bean = new Date( ((java.sql.Date) bean).getTime() ); |
| 52 | } | |
| 53 | 0 | if( bean instanceof Date ){ |
| 54 | 0 | Calendar c = Calendar.getInstance(); |
| 55 | 0 | c.setTime( (Date) bean ); |
| 56 | 0 | jsonObject = new JSONObject().element( "year", c.get( Calendar.YEAR ) ) |
| 57 | .element( "month", c.get( Calendar.MONTH ) ) | |
| 58 | .element( "day", c.get( Calendar.DAY_OF_MONTH ) ) | |
| 59 | .element( "hours", c.get( Calendar.HOUR_OF_DAY ) ) | |
| 60 | .element( "minutes", c.get( Calendar.MINUTE ) ) | |
| 61 | .element( "seconds", c.get( Calendar.SECOND ) ) | |
| 62 | .element( "milliseconds", c.get( Calendar.MILLISECOND ) ); | |
| 63 | 0 | }else{ |
| 64 | 0 | jsonObject = new JSONObject( true ); |
| 65 | } | |
| 66 | 0 | return jsonObject; |
| 67 | } | |
| 68 | } |