Example usage for com.google.gson GsonBuilder setDateFormat

List of usage examples for com.google.gson GsonBuilder setDateFormat

Introduction

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

Prototype

public GsonBuilder setDateFormat(int style) 

Source Link

Document

Configures Gson to to serialize Date objects according to the style value provided.

Usage

From source file:org.ramadda.repository.output.JsonOutputHandler.java

License:Apache License

/**
 * _more_// w w w .j a v a2  s .c  o m
 *
 * @param args _more_
 */
public static void main(String[] args) {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.setPrettyPrinting();
    gsonBuilder.setDateFormat(DateFormat.LONG);
    gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE);

    Gson gson = gsonBuilder.create();
    Entry entry = new Entry();
    System.err.println(gson.toJson(entry));

}

From source file:org.restlet.ext.gson.GsonRepresentation.java

License:Open Source License

/**
 * Returns a new instance of the builder for Gson instances.
 * //  w  w w .j a v  a2 s.co  m
 * @return a new instance of builder for Gson instances.
 */
protected GsonBuilder createBuilder() {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.setDateFormat(DateFormat.FULL);
    return gsonBuilder;
}

From source file:org.sonatype.nexus.extdirect.internal.ExtDirectGsonBuilderConfigurator.java

License:Open Source License

@Override
public void configure(final GsonBuilder builder, final GlobalConfiguration configuration) {
    if (configuration.getDebug()) {
        builder.setPrettyPrinting();//  www . j a  va 2 s. co  m
    }
    builder.serializeNulls();
    builder.disableHtmlEscaping();

    builder.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
}

From source file:org.springframework.data.cloudant.core.CloudantTemplate.java

License:Apache License

public void setDataAdapter(Map<Class<?>, Object> adapters) {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(DateTime.class, new DateTimeDataAdapter());
    gsonBuilder.registerTypeHierarchyAdapter(BaseDocument.class, new UnmappedDataAdapter());
    if (adapters != null) {
        for (Class<?> type : adapters.keySet()) {
            gsonBuilder.registerTypeAdapter(type, adapters.get(type));
        }/*  ww w  . j a va 2 s . c  om*/
    }
    gsonBuilder.setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
    this.client.setGsonBuilder(gsonBuilder);
}

From source file:org.springframework.http.converter.json.GsonFactoryBean.java

License:Apache License

@Override
public void afterPropertiesSet() {
    GsonBuilder builder = (this.base64EncodeByteArrays
            ? GsonBuilderUtils.gsonBuilderWithBase64EncodedByteArrays()
            : new GsonBuilder());
    if (this.serializeNulls) {
        builder.serializeNulls();//from w w w  .  j ava2 s  . co m
    }
    if (this.prettyPrinting) {
        builder.setPrettyPrinting();
    }
    if (this.disableHtmlEscaping) {
        builder.disableHtmlEscaping();
    }
    if (this.dateFormatPattern != null) {
        builder.setDateFormat(this.dateFormatPattern);
    }
    this.gson = builder.create();
}

From source file:org.uorm.serializer.DefaultJsonSerializer.java

License:Apache License

/**
 * @return the gson/*from  ww  w . j ava 2s .  c o  m*/
 */
public Gson getGson() {
    if (gson == null) {
        GsonBuilder gbuilder = new GsonBuilder();
        if (serializeNulls) {
            gbuilder.serializeNulls();
        }
        gbuilder.setDateFormat(datePattern);
        if (prettyPrinting) {
            gbuilder.setPrettyPrinting();
        }
        if (excludeFieldsWithoutExposeAnnotation) {
            gbuilder.excludeFieldsWithoutExposeAnnotation();
        }
        if (enableComplexMapKey) {
            gbuilder.enableComplexMapKeySerialization();
        }
        if (version != null) {
            gbuilder.setVersion(version);
        }
        gson = gbuilder.create();
    }
    return gson;
}

From source file:org.wso2.carbon.andes.extensions.device.mgt.jaxrs.common.GsonMessageBodyHandler.java

License:Open Source License

private Gson getGson() {
    if (gson == null) {
        final GsonBuilder gsonBuilder = new GsonBuilder();
        gson = gsonBuilder.setDateFormat(DATE_FORMAT).create();
    }//from  w ww.  j a v  a  2s  . co  m
    return gson;
}

From source file:org.y20k.trackbook.helpers.StorageHelper.java

License:MIT License

public boolean saveTrack(@Nullable Track track, int fileType) {

    Date recordingStart = null;//from w  w  w .j  av  a 2  s.  co m
    if (track != null) {
        recordingStart = track.getRecordingStart();
    }

    if (mFolder != null && mFolder.exists() && mFolder.isDirectory() && mFolder.canWrite()
            && recordingStart != null && track != null) {
        // create file object
        String fileName;
        if (fileType == FILE_TEMP_TRACK) {
            // case: temp file
            fileName = FILE_NAME_TEMP + FILE_TYPE_TRACKBOOK_EXTENSION;
        } else {
            // case: regular file
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss", Locale.US);
            fileName = dateFormat.format(recordingStart) + FILE_TYPE_TRACKBOOK_EXTENSION;
        }
        File file = new File(mFolder.toString() + "/" + fileName);

        // convert track to JSON
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.setDateFormat("M/d/yy hh:mm a");
        Gson gson = gsonBuilder.create();
        String json = gson.toJson(track);

        // write track
        try (BufferedWriter bw = new BufferedWriter(new FileWriter(file))) {
            LogHelper.v(LOG_TAG, "Saving track to external storage: " + file.toString());
            bw.write(json);
        } catch (IOException e) {
            LogHelper.e(LOG_TAG,
                    "Unable to saving track to external storage (IOException): " + file.toString());
            return false;
        }

        // if write was successful delete old track files - only if not a temp file
        if (fileType != FILE_TEMP_TRACK) {
            // include  temp file if it exists
            deleteOldTracks(true);
        }

        return true;

    } else {
        LogHelper.e(LOG_TAG, "Unable to save track to external storage.");
        return false;
    }

}

From source file:org.y20k.trackbook.helpers.StorageHelper.java

License:MIT License

private Track readTrackFromFile(File file) {

    // check if given file was null
    if (file == null) {
        LogHelper.e(LOG_TAG, "Did not receive a file object.");
        return null;
    }/*www . j ava 2  s  .  c  om*/

    try (BufferedReader br = new BufferedReader(new FileReader(file))) {
        LogHelper.v(LOG_TAG, "Loading track from external storage: " + file.toString());

        String line;
        StringBuilder sb = new StringBuilder("");

        // read until last line reached
        while ((line = br.readLine()) != null) {
            sb.append(line);
            sb.append("\n");
        }

        // TODO implement a format version check before handing the file to GSON

        // get track from JSON
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.setDateFormat("M/d/yy hh:mm a");
        Gson gson = gsonBuilder.create();
        return gson.fromJson(sb.toString(), Track.class);

    } catch (IOException e) {
        LogHelper.e(LOG_TAG, "Unable to read file from external storage: " + file.toString());
        return null;
    }
}

From source file:pl.datamatica.traccar.api.Context.java

License:Open Source License

private Context() {
    emf = Persistence.createEntityManagerFactory("release");
    Map<String, String> properties = getApiConnectionData();

    if (properties.size() > 0) {
        // Use properties obtained from 'debug.xml' or PRODUCTION_TRACCAR_CONFIG_FILE if possible
        emfMetadata = Persistence.createEntityManagerFactory("traccar_api_metadata_persistence", properties);
    } else {/*from w ww  .  ja  va 2s.  c om*/
        // Otherwise settings from 'persistence.xml' will be used
        emfMetadata = Persistence.createEntityManagerFactory("traccar_api_metadata_persistence");
    }

    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.setDateFormat(Application.DATE_FORMAT);
    gsonBuilder.setExclusionStrategies(new AnnotationExclusionStrategy());
    if (isInDevMode())
        gsonBuilder.setPrettyPrinting();
    gson = gsonBuilder.create();

    daemonExecutor = Executors.newScheduledThreadPool(2);
}