Quantcast
Channel: Sleepless Dev
Viewing all articles
Browse latest Browse all 217

Boon supports for coercion and JSON serialization gets timezone, locale, UUID, URL, URI, File and Path (it had Class but I did not advertise it)

$
0
0
I added support for timezone, locale, UUID, URL, URI, File and Path.

public static class Sample
implements Serializable {

private static final long serialVersionUID = 1L;

private URL url;
private URI uri;
private File file;
private Date date;
private final UUID persistId;
private Path path;
private Locale locale;
private TimeZone timeZone;

private static final AtomicLongFieldUpdater<Sample> EFFECTIVEDATE_UPDATER
= AtomicLongFieldUpdater.newUpdater(Sample.class, "effectiveDate");


public Sample() {
this.persistId = UUID.randomUUID();
this.effectiveDate = System.currentTimeMillis();

try {
url = new URL("http://www.google.com");
} catch (MalformedURLException e) {
e.printStackTrace();
}

try {
uri = new URI("http://www.google.com");
} catch (URISyntaxException e) {
e.printStackTrace();
}

file =new File("/usr/local/bin/vertx");

date = new Date();
path = file.toPath();
locale = Locale.CANADA;
timeZone = TimeZone.getTimeZone("PST");
}


public UUID getPersistId() {
return persistId;
}

private volatile long effectiveDate;

public final long getEffectiveDate() {
return effectiveDate;
}

public final void setEffectiveDate(long effectiveDate) {
EFFECTIVEDATE_UPDATER.compareAndSet(this, this.effectiveDate, effectiveDate);
}

Richard Hightower
 
Owner
/**
*
* @author l man
*/
publicstaticclassSample
implementsSerializable{

privatestaticfinallongserialVersionUID=1L;

privateURLurl;
privateURIuri;
privateFilefile;
privateDatedate;
privatefinalUUIDpersistId;
privatePathpath;
privateLocalelocale;
privateTimeZonetimeZone;

privatestaticfinalAtomicLongFieldUpdater<Sample>EFFECTIVEDATE_UPDATER
=AtomicLongFieldUpdater.newUpdater(Sample.class,"effectiveDate");


publicSample(){
this.persistId=UUID.randomUUID();
this.effectiveDate=System.currentTimeMillis();

try{
url=newURL("http://www.google.com");
}catch(MalformedURLExceptione){
e.printStackTrace();
}

try{
uri=newURI("http://www.google.com");
}catch(URISyntaxExceptione){
e.printStackTrace();
}

file=newFile("/usr/local/bin/vertx");

date=newDate();
path=file.toPath();
locale=Locale.CANADA;
timeZone=TimeZone.getTimeZone("PST");
}


publicUUIDgetPersistId(){
returnpersistId;
}

privatevolatilelongeffectiveDate;

publicfinallonggetEffectiveDate(){
returneffectiveDate;
}

publicfinalvoidsetEffectiveDate(longeffectiveDate){
EFFECTIVEDATE_UPDATER.compareAndSet(this,this.effectiveDate,effectiveDate);
}
...
@Test
publicvoidtest4(){
Samplesample1=newSample();
Stringjson=JsonFactory.toJson(sample1);
puts(json);
puts(sample1);

ok=json.contains("\"effectiveDate\"")||die();

Samplesample2=JsonFactory.fromJson(json,Sample.class);

puts("sample2",sample2);

puts("sample2",JsonFactory.toJson(sample2));

ok=sample1.equals(sample2);

}
Richard Hightower
 
Owner
The above generates the following JSON:
{
"url": "http://www.google.com",
"uri": "http://www.google.com",
"file": "/usr/local/bin/vertx",
"date": 1398455978940,
"persistId": "029f6ef3-3de6-40ec-bc94-a06b61ca9793",
"path": "/usr/local/bin/vertx",
"locale": "en_ca",
"timeZone": "PST",
"effectiveDate": 1398455978940
}
Richard HightowerRichardHightower closed this just now


Viewing all articles
Browse latest Browse all 217

Trending Articles