Example usage for com.google.gson JsonParseException JsonParseException

List of usage examples for com.google.gson JsonParseException JsonParseException

Introduction

In this page you can find the example usage for com.google.gson JsonParseException JsonParseException.

Prototype

public JsonParseException(String msg, Throwable cause) 

Source Link

Document

Creates exception with the specified message and cause.

Usage

From source file:uk.co.visalia.brightpearl.apiclient.client.adaptors.CalendarAdaptor.java

License:Apache License

@Override
public Calendar read(JsonReader jsonReader) throws IOException {
    if (jsonReader.peek() == JsonToken.NULL) {
        jsonReader.nextNull();//from   w ww.j a  va2  s.  c  o  m
        return null;
    }
    String string = jsonReader.nextString();
    if (StringUtils.isNotBlank(string)) {
        if (storedInitException != null) {
            throw new JsonParseException("Could not parse '" + string + "' as an ISO date.",
                    storedInitException);
        }
        try {
            return datatypeFactory.newXMLGregorianCalendar(string).toGregorianCalendar();
        } catch (Exception e) {
            throw new JsonParseException("Could not parse '" + string + "' as an ISO date.", e);
        }
    }
    return null;
}

From source file:uk.co.visalia.brightpearl.apiclient.client.adaptors.CalendarAdaptor.java

License:Apache License

@Override
public void write(JsonWriter jsonWriter, Calendar calendar) throws IOException {
    if (calendar == null) {
        jsonWriter.nullValue();/*from  www .  j a v  a2 s . co  m*/
        return;
    }
    if (storedInitException != null) {
        throw new JsonParseException("Unexpected error serializing calendar", storedInitException);
    }
    try {
        DatatypeFactory dtf = datatypeFactory;
        XMLGregorianCalendar xgc = dtf.newXMLGregorianCalendar();
        xgc.setYear(calendar.get(Calendar.YEAR));
        xgc.setDay(calendar.get(Calendar.DAY_OF_MONTH));
        xgc.setMonth(calendar.get(Calendar.MONTH) + 1);
        xgc.setHour(calendar.get(Calendar.HOUR_OF_DAY));
        xgc.setMinute(calendar.get(Calendar.MINUTE));
        xgc.setSecond(calendar.get(Calendar.SECOND));
        xgc.setMillisecond(calendar.get(Calendar.MILLISECOND));
        int offsetInMinutes = (calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET))
                / (60 * 1000);
        xgc.setTimezone(offsetInMinutes);
        jsonWriter.value(xgc.toXMLFormat());
    } catch (Exception e) {
        throw new RuntimeException("Unexpected error serializing calendar", e);
    }
}