Rick

Rick
Rick

Friday, April 25, 2014

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)

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
     */
    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);
        }
...
    @Test
    public void test4() {
        Sample sample1 = new Sample();
        String json = JsonFactory.toJson(sample1);
        puts(json);
        puts (sample1);

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

        Sample sample2 = 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 referenced this issue just now
 Closed

NumberFormatException #165

Richard HightowerRichardHightower closed this just now

No comments:

Post a Comment

Kafka and Cassandra support, training for AWS EC2 Cassandra 3.0 Training