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:och.util.json.GsonUtil.java

License:Apache License

public static Gson gsonWithDateFormat(String dateFormat, boolean prettyPrinting) {
    GsonBuilder builder = new GsonBuilder();
    builder.setDateFormat(dateFormat);
    if (prettyPrinting)
        builder.setPrettyPrinting();//from   w  w w  .  j  a  va2s. c  o m
    return builder.create();
}

From source file:org.apache.camel.component.gson.GsonDataFormat.java

License:Apache License

@Override
protected void doStart() throws Exception {
    if (gson == null) {
        GsonBuilder builder = new GsonBuilder();
        if (exclusionStrategies != null && !exclusionStrategies.isEmpty()) {
            ExclusionStrategy[] strategies = exclusionStrategies
                    .toArray(new ExclusionStrategy[exclusionStrategies.size()]);
            builder.setExclusionStrategies(strategies);
        }/*from  w w  w .  ja v  a  2  s .  c  o  m*/
        if (longSerializationPolicy != null) {
            builder.setLongSerializationPolicy(longSerializationPolicy);
        }
        if (fieldNamingPolicy != null) {
            builder.setFieldNamingPolicy(fieldNamingPolicy);
        }
        if (fieldNamingStrategy != null) {
            builder.setFieldNamingStrategy(fieldNamingStrategy);
        }
        if (serializeNulls != null && serializeNulls) {
            builder.serializeNulls();
        }
        if (prettyPrinting != null && prettyPrinting) {
            builder.setPrettyPrinting();
        }
        if (dateFormatPattern != null) {
            builder.setDateFormat(dateFormatPattern);
        }
        gson = builder.create();
    }
}

From source file:org.apache.hadoop.yarn.server.resourcemanager.security.HopsworksRMAppSecurityActions.java

License:Apache License

public HopsworksRMAppSecurityActions() throws MalformedURLException, GeneralSecurityException {
    ACCEPTABLE_HTTP_RESPONSES.add(HttpStatus.SC_OK);
    ACCEPTABLE_HTTP_RESPONSES.add(HttpStatus.SC_NO_CONTENT);
    authHeader = new AtomicReference<>();
    GsonBuilder parserBuilder = new GsonBuilder();
    parserBuilder.setFieldNamingPolicy(FieldNamingPolicy.IDENTITY);
    parserBuilder.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
    jsonParser = parserBuilder.create();

    tokenRenewer = Executors.newSingleThreadExecutor(
            new ThreadFactoryBuilder().setNameFormat("JWT renewer thread").setDaemon(true).build());
}

From source file:org.chililog.server.common.JsonTranslator.java

License:Apache License

/**
 * <p>/*w  ww .  ja va  2  s .c  om*/
 * Singleton constructor that creates our reusable GSON object.
 * </p>
 * 
 * <p>
 * If there are any errors, the JVM is terminated. Without valid application properties, we will fall over elsewhere
 * so might as well terminate here.
 * </p>
 */
private JsonTranslator() {
    try {
        GsonBuilder builder = new GsonBuilder();
        if (AppProperties.getInstance().getJsonPretty()) {
            builder.setPrettyPrinting();
        }
        builder.setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
        builder.setFieldNamingStrategy(new ChiliLogFieldNamingStrategy());
        builder.disableHtmlEscaping();
        _gson = builder.create();
    } catch (Exception e) {
        _logger.error("Error initializing JSON translator: " + e.getMessage(), e);
        System.exit(1);
    }
}

From source file:org.eclipse.leshan.server.demo.servlet.ClientServlet.java

License:Open Source License

public ClientServlet(LwM2mServer server) {
    this.server = server;

    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeHierarchyAdapter(Registration.class,
            new RegistrationSerializer(server.getPresenceService()));
    gsonBuilder.registerTypeHierarchyAdapter(LwM2mResponse.class, new ResponseSerializer());
    gsonBuilder.registerTypeHierarchyAdapter(LwM2mNode.class, new LwM2mNodeSerializer());
    gsonBuilder.registerTypeHierarchyAdapter(LwM2mNode.class, new LwM2mNodeDeserializer());
    gsonBuilder.setDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
    this.gson = gsonBuilder.create();
}

From source file:org.eclipse.leshan.server.demo.servlet.CliServlet.java

License:Open Source License

public CliServlet(LwM2mServer server, int securePort) {
    this.server = server;

    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeHierarchyAdapter(Client.class, new ClientSerializer(securePort));
    gsonBuilder.registerTypeHierarchyAdapter(LwM2mResponse.class, new ResponseSerializer());
    gsonBuilder.registerTypeHierarchyAdapter(LwM2mNode.class, new LwM2mNodeSerializer());
    gsonBuilder.registerTypeHierarchyAdapter(LwM2mNode.class, new LwM2mNodeDeserializer());
    gsonBuilder.setDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
    this.gson = gsonBuilder.create();
}

From source file:org.eclipse.leshan.server.demo.servlet.EventServlet.java

License:Open Source License

public EventServlet(LeshanServer server, int securePort) {
    server.getRegistrationService().addListener(this.registrationListener);
    server.getObservationService().addListener(this.observationListener);
    server.getPresenceService().addListener(this.presenceListener);

    // add an interceptor to each endpoint to trace all CoAP messages
    coapMessageTracer = new CoapMessageTracer(server.getRegistrationService());
    for (Endpoint endpoint : server.coap().getServer().getEndpoints()) {
        endpoint.addInterceptor(coapMessageTracer);
    }//from ww w .  j av  a  2s.  c o m

    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeHierarchyAdapter(Registration.class,
            new RegistrationSerializer(server.getPresenceService()));
    gsonBuilder.registerTypeHierarchyAdapter(LwM2mNode.class, new LwM2mNodeSerializer());
    gsonBuilder.setDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
    this.gson = gsonBuilder.create();
}

From source file:org.eclipse.leshan.standalone.servlet.ClientServlet.java

License:Open Source License

public ClientServlet(LwM2mServer server) {
    this.server = server;

    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeHierarchyAdapter(Client.class, new ClientSerializer());
    gsonBuilder.registerTypeHierarchyAdapter(LwM2mResponse.class, new ResponseSerializer());
    gsonBuilder.registerTypeHierarchyAdapter(LwM2mNode.class, new LwM2mNodeSerializer());
    gsonBuilder.registerTypeHierarchyAdapter(LwM2mNode.class, new LwM2mNodeDeserializer());
    gsonBuilder.setDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
    this.gson = gsonBuilder.create();
}

From source file:org.eclipse.leshan.standalone.servlet.EventServlet.java

License:Open Source License

public EventServlet(LeshanServer server) {
    server.getClientRegistry().addListener(this.clientRegistryListener);
    server.getObservationRegistry().addListener(this.observationRegistryListener);

    // add an interceptor to each endpoint to trace all CoAP messages
    coapMessageTracer = new CoapMessageTracer(server.getClientRegistry());
    for (Endpoint endpoint : server.getCoapServer().getEndpoints()) {
        endpoint.addInterceptor(coapMessageTracer);
    }//from   w ww  .j  a v  a 2 s.com

    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeHierarchyAdapter(Client.class, new ClientSerializer());
    gsonBuilder.registerTypeHierarchyAdapter(LwM2mNode.class, new LwM2mNodeSerializer());
    gsonBuilder.setDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
    this.gson = gsonBuilder.create();
}

From source file:org.eclipse.packagedrone.repo.adapter.npm.ModuleHandler.java

License:Open Source License

public void process(final OutputStream stream) throws IOException {
    final String sitePrefix = this.service.getDefaultSitePrefix();

    final GsonBuilder builder = new GsonBuilder();
    if (this.pretty) {
        builder.setPrettyPrinting();//from w  ww  . j  a  va  2  s  .com
    }

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

    final Gson gson = builder.create();

    final JsonParser parser = new JsonParser();

    final TreeMap<String, PackageEntry> versions = new TreeMap<>();

    for (final ArtifactInformation art : this.channel.getArtifacts()) {
        final String pkg = art.getMetaData().get(KEY_NPM_PACKAGE_JSON);
        if (pkg == null) {
            continue;
        }

        try {
            final JsonElement pkgEle = parser.parse(pkg);
            final PackageInfo pi = gson.fromJson(pkgEle, PackageInfo.class);

            if (!this.moduleName.equals(pi.getName())) {
                continue;
            }

            versions.put(pi.getVersion(), new PackageEntry(pi, pkgEle, art));
        } catch (final Exception e) {
            logger.info("Failed to parse package.json of " + art.getId(), e);
            continue;
        }
    }

    // now build the main file

    final JsonObject main = new JsonObject();

    // pull in meta data from most recent version

    main.addProperty("name", this.moduleName);

    if (!versions.isEmpty()) {
        final Entry<String, PackageEntry> mostRecent = versions.lastEntry();
        final PackageInfo pi = mostRecent.getValue().getInfo();
        main.addProperty("license", pi.getLicense());

        final JsonObject distTags = new JsonObject();
        main.add("dist-tags", distTags);

        distTags.addProperty("latest", pi.getVersion());
    }

    final JsonObject times = new JsonObject();
    main.add("time", times);

    final JsonObject versionsEle = new JsonObject();
    main.add("versions", versionsEle);

    for (final Map.Entry<String, PackageEntry> entry : versions.entrySet()) {
        final PackageInfo pi = entry.getValue().getInfo();
        final ArtifactInformation art = entry.getValue().getArtifact();

        times.add(pi.getVersion(), gson.toJsonTree(art.getCreationTimestamp()));

        final JsonObject ele = (JsonObject) entry.getValue().getElement();

        final JsonObject dist = new JsonObject();
        dist.addProperty("shasum", art.getMetaData().get(KEY_SHA1));
        dist.addProperty("tarball", String.format("%s/artifact/%s/dump", sitePrefix, art.getId()));

        ele.add("dist", dist);

        versionsEle.add(pi.getVersion(), ele);
    }

    // render

    try (OutputStreamWriter out = new OutputStreamWriter(stream, StandardCharsets.UTF_8)) {
        gson.toJson(main, out);
    }
}