2009-06-08 19 views
13

यदि मुझे एक विश्वसनीय क्लाइंट से निम्न जेसन मिलता है, तो मैं java.util.Date को सुंदरता से कैसे उदारतापूर्वक प्राप्त करूं?Grails दिनांक unmarshalling

{ 
    "class": "url", 
    "link": "http://www.empa.ch", 
    "rating": 5, 
    "lastcrawl" : "2009-06-04 16:53:26.706 CEST", 
    "checksum" : "837261836712xxxkfjhds", 
} 

उत्तर

18

स्पष्ट तरीका संभव तिथि प्रारूप के लिए एक कस्टम DataBinder रजिस्टर करने के लिए शायद है (यह संभव (उर्फ प्रदान किए बिना हार्ड-कोड है।) प्रारूप, कि मैं सुंदर ढंग से से क्या मतलब है ...)।

beans = { 
    "customEditorRegistrar"(CustomEditorRegistrar) 
} 
:

import java.beans.PropertyEditorSupport; 
import java.text.ParseException; 
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import java.util.Collections; 
import java.util.List; 

public class CustomDateBinder extends PropertyEditorSupport { 

    private final List<String> formats; 

    public CustomDateBinder(List formats) { 
     List<String> formatList = new ArrayList<String>(formats.size()); 
     for (Object format : formats) { 
      formatList.add(format.toString()); // Force String values (eg. for GStrings) 
     } 
     this.formats = Collections.unmodifiableList(formatList); 
    } 

    @Override 
    public void setAsText(String s) throws IllegalArgumentException { 
     if (s != null) 
      for (String format : formats) { 
       // Need to create the SimpleDateFormat every time, since it's not thead-safe 
       SimpleDateFormat df = new SimpleDateFormat(format); 
       try { 
        setValue(df.parse(s)); 
        return; 
       } catch (ParseException e) { 
        // Ignore 
       } 
      } 
    } 
} 

तुम भी एक PropertyEditorRegistrar

import org.springframework.beans.PropertyEditorRegistrar; 
import org.springframework.beans.PropertyEditorRegistry; 

import grails.util.GrailsConfig; 
import java.util.Date; 
import java.util.List; 

public class CustomEditorRegistrar implements PropertyEditorRegistrar { 
    public void registerCustomEditors(PropertyEditorRegistry reg) { 
     reg.registerCustomEditor(Date.class, new CustomDateBinder(GrailsConfig.get("grails.date.formats", List.class))); 
    } 
}   

को लागू करने और अपने grails एप्लिकेशन के अंतर्गत/conf/वसंत/resources.groovy में एक वसंत-सेम परिभाषा बनाने की आवश्यकता होगी

और आखिर में अपने grails-app/conf/Config.groovy में दिनांक प्रारूपों को परिभाषित करें:

grails.date.formats = ["yyyy-MM-dd HH:mm:ss.SSS ZZZZ", "dd.MM.yyyy HH:mm:ss"] 
+0

बस सोच रहा है कि क्या आप एक कारण है कि आप जावा में (ऊपर के रूप में) ग्रोवी के बजाय इसे लागू करना चाहते हैं? कोड ग्रोवी के साथ काफी छोटा होगा। –

+0

मैंने पूर्व में जावा में कोड का एक समान टुकड़ा लागू किया था जब ग्रोवी अब उससे धीमा था। इस मामले में ग्रोवी ने एक महान छलांग लगाई। मैं सिर्फ पुराने जावा कोड को आलसी से बाहर कर रहा हूं ;-) –

+0

कोड का अच्छा टुकड़ा, जो आप करेंगे, उसके क्लासिक। हालांकि पार्सिंग प्रयासों के माध्यम से फिर से शुरू करने के बजाय प्रारूप को पुनर्प्राप्त करने के लिए लोकेल का सबसे साफ तरीका उपयोग करना होगा। – Gepsens

5

ध्यान रखें कि Grails 2.3+ का नया संस्करण बॉक्स के बाहर इस प्रकार की सुविधा का समर्थन करता है। देखें Date Formats for Data Binding

इसी के साथ

कहा, तुम 2.3 से पहले Grails के एक संस्करण का उपयोग करने के लिए मजबूर कर रहे हैं, CustomEditorRegistrar प्रतिवाद चेतावनी समाप्त करने के लिए निम्नलिखित कोड का उपयोग कर अद्यतन किया जा सकता है, और भी @Component एनोटेशन, जो की अनुमति देता है का उपयोग करता है आप resources.groovy में सीधे बीन जोड़ने के चरण को हटाने/छोड़ने के लिए। यह भी नहीं कि मैंने grails.dconfinding.dateFormats को grails.dconfinding.dateFormats में बदल दिया है, जो अब Grails 2.3+ में समर्थित संपत्ति से मेल खाता है। अंत में, मेरा संस्करण एक .groovy है, नहीं .java फ़ाइल।

import javax.annotation.Resource 
import org.codehaus.groovy.grails.commons.GrailsApplication 
import org.springframework.beans.PropertyEditorRegistrar 
import org.springframework.beans.PropertyEditorRegistry 
import org.springframework.stereotype.Component 

@Component 
public class CustomEditorRegistrar implements PropertyEditorRegistrar { 

    @Resource 
    GrailsApplication grailsApplication 

    public void registerCustomEditors(PropertyEditorRegistry reg){ 
     def dateFormats = grailsApplication.config.grails.databinding.dateFormats as List 
     reg.registerCustomEditor(Date.class, new CustomDateBinder(dateFormats)) 
    } 
} 
+0

धन्यवाद आदमी। आपने मेरा दिन बचाया @ बाइंडिंगफॉर्मैट सही विकल्प है। –