Example usage for com.google.gson GsonBuilder setFieldNamingPolicy

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

Introduction

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

Prototype

public GsonBuilder setFieldNamingPolicy(FieldNamingPolicy namingConvention) 

Source Link

Document

Configures Gson to apply a specific naming policy to an object's field during serialization and deserialization.

Usage

From source file:org.hyperic.hq.plugin.jboss7.JBossAdminHttp.java

License:Open Source License

private Object query(HttpRequestBase req, String api, Type type) throws PluginException {
    Object res = null;/*w ww . j ava 2s .c  om*/
    try {
        HttpResponse response = client.execute(req, localcontext);
        int statusCode = response.getStatusLine().getStatusCode();
        // response must be read in order to "close" the connection.
        // https://jira.hyperic.com/browse/HHQ-5063#comment-154101
        String responseBody = readInputString(response.getEntity().getContent());

        if (log.isDebugEnabled()) {
            log.debug("[" + api + "] -(" + req.getURI() + ")-> " + responseBody);
        }

        if (statusCode != 200) {
            throw new PluginException("[" + req.getURI() + "] http error code: '" + statusCode + "' msg='"
                    + response.getStatusLine().getReasonPhrase() + "'");
        }

        GsonBuilder gsb = new GsonBuilder();
        if (!((type instanceof Class)
                && ((Class) type).getCanonicalName().equals(Connector.class.getCanonicalName()))) {
            gsb.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES);
        }
        Gson gson = gsb.create();

        res = gson.fromJson(responseBody, type);
        if (log.isDebugEnabled()) {
            if (res.getClass().isArray()) {
                log.debug("[" + api + "] -(" + statusCode + ")*> " + Arrays.asList((Object[]) res));
            } else {
                log.debug("[" + api + "] -(" + statusCode + ")-> " + res);
            }
        }
    } catch (JsonParseException ex) {
        log.debug(ex.getMessage(), ex);
        throw new PluginException(ex.getMessage(), ex);
    } catch (IOException ex) {
        log.debug(ex.getMessage(), ex);
        throw new PluginException(ex.getMessage(), ex);
    }
    return res;
}

From source file:org.hyperic.hq.plugin.rabbitmq.core.HypericRabbitAdmin.java

License:Open Source License

private <T extends Object> T get(String api, Class<T> classOfT) throws PluginException {
    T res = null;/*from   www . ja v a 2 s.c  o m*/
    try {
        HttpGet get = new HttpGet(targetHost.toURI() + api);
        HttpResponse response = client.execute(get, localcontext);
        int r = response.getStatusLine().getStatusCode();
        // response must be read in order to "close" the connection.
        // https://jira.hyperic.com/browse/HHQ-5063#comment-154101
        String responseBody = readInputString(response.getEntity().getContent());

        if (logger.isDebugEnabled()) {
            logger.debug("[" + api + "] -(" + r + ")-> " + responseBody);
        }

        if (r != 200) {
            throw new PluginException("[" + api + "] http error code: '" + r + "'");
        }

        GsonBuilder gsb = new GsonBuilder();
        gsb.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
        gsb.registerTypeAdapter(Integer.class, new IntegerDeserializer());
        gsb.registerTypeAdapter(int.class, new IntegerDeserializer());
        gsb.registerTypeAdapter(Date.class, new DateTimeDeserializer());
        gsb.registerTypeAdapter(MessageStats.class, new MessageStatsDeserializer());
        Gson gson = gsb.create();

        res = gson.fromJson(responseBody, classOfT);
        if (logger.isDebugEnabled()) {
            if (res.getClass().isArray()) {
                logger.debug("[" + api + "] -(" + r + ")*> " + Arrays.asList((Object[]) res));
            } else {
                logger.debug("[" + api + "] -(" + r + ")-> " + res);
            }
        }
    } catch (IOException ex) {
        logger.debug(ex.getMessage(), ex);
        throw new PluginException(ex.getMessage(), ex);
    }
    return res;
}

From source file:org.kairosdb.core.http.rest.json.QueryParser.java

License:Apache License

@Inject
public QueryParser(AggregatorFactory aggregatorFactory, GroupByFactory groupByFactory,
        QueryPluginFactory pluginFactory) {
    m_aggregatorFactory = aggregatorFactory;
    m_groupByFactory = groupByFactory;/*from  w  ww.  j av  a2 s . com*/
    m_pluginFactory = pluginFactory;

    m_descriptorMap = new HashMap<Class, Map<String, PropertyDescriptor>>();

    GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapterFactory(new LowercaseEnumTypeAdapterFactory());
    builder.registerTypeAdapter(TimeUnit.class, new TimeUnitDeserializer());
    builder.registerTypeAdapter(DateTimeZone.class, new DateTimeZoneDeserializer());
    builder.registerTypeAdapter(Metric.class, new MetricDeserializer());
    builder.registerTypeAdapter(SetMultimap.class, new SetMultimapDeserializer());
    builder.registerTypeAdapter(RelativeTime.class, new RelativeTimeSerializer());
    builder.registerTypeAdapter(SetMultimap.class, new SetMultimapSerializer());
    builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);

    m_gson = builder.create();
}

From source file:org.lastaflute.core.json.engine.GsonJsonEngine.java

License:Apache License

protected void setupFieldPolicy(GsonBuilder builder) {
    final JsonFieldNaming naming = option.getFieldNaming().orElse(getDefaultFieldNaming());
    builder.setFieldNamingPolicy(deriveFieldNamingPolicy(naming));
}

From source file:org.openhab.binding.toon.internal.ToonApiClient.java

License:Open Source License

private GsonBuilder createGsonBuilder() {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.IDENTITY);
    return gsonBuilder;
}

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

License:Apache License

/**
 * _more_//from   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.sociotech.communitymashup.source.languagedetection.apiwrapper.DetectLanguageAPIWrapper.java

License:Open Source License

/**
 * Gets the gson builder.//from w  w  w.ja v a 2s .co m
 * 
 * @return the gson builder
 */
protected GsonBuilder getGsonBuilder() {
    GsonBuilder builder = new GsonBuilder();

    //builder.setDateFormat(ApplicationConstants.DATE_FORMAT);
    builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);

    return builder;
}

From source file:org.sociotech.communitymashup.source.readability.apiwrapper.ReadabilityAPIWrapper.java

License:Open Source License

/**
 * Gets the gson builder./*from   www  . j a v  a  2  s  . c  om*/
 * 
 * @return the gson builder
 */
protected GsonBuilder getGsonBuilder() {
    GsonBuilder builder = new GsonBuilder();

    builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);

    return builder;
}

From source file:org.tinymediamanager.scraper.fanarttv.FanartTv.java

License:Apache License

protected GsonBuilder getGsonBuilder() {
    GsonBuilder builder = new GsonBuilder();
    // class types
    builder.registerTypeAdapter(Integer.class, new JsonDeserializer<Integer>() {
        @Override/* w ww .j a  va2s.c  o m*/
        public Integer deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            try {
                return Integer.valueOf(json.getAsInt());
            } catch (NumberFormatException e) {
                return 0;
            }
        }
    });
    builder.registerTypeAdapter(Date.class, new DateTypeAdapter());
    builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
    return builder;
}

From source file:org.tuleap.mylyn.task.core.internal.parser.TuleapGsonProvider.java

License:Open Source License

/**
 * Provides the default {@link GsonBuilder}.
 *
 * @return A new instance of GsonBuilder, properly configured for Tuleap.
 *//*  ww  w  . j av a2  s .c  o  m*/
public static GsonBuilder defaultBuilder() {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(TuleapTracker.class, new TuleapTrackerDeserializer());
    gsonBuilder.registerTypeAdapter(TuleapElementComment.class, new TuleapChangesetDeserializer());
    gsonBuilder.registerTypeAdapter(TuleapWorkflowTransition.class, new TuleapWorkflowTransitionDeserializer());
    gsonBuilder.registerTypeAdapter(Date.class, new DateIso8601Adapter());
    gsonBuilder.registerTypeAdapter(TuleapArtifact.class, new TuleapArtifactDeserializer());
    gsonBuilder.registerTypeAdapter(TuleapArtifact.class, new TuleapArtifactSerializer());
    gsonBuilder.registerTypeAdapter(TuleapArtifactWithComment.class,
            new TuleapArtifactWithCommentSerializer<TuleapArtifactWithComment>());
    gsonBuilder.registerTypeAdapter(TuleapArtifactWithAttachment.class,
            new TuleapArtifactWithAttachmentSerializer());
    gsonBuilder.registerTypeAdapter(ArtifactLinkFieldValue.class, new ArtifactLinkFieldValueAdapter());
    gsonBuilder.registerTypeAdapter(LiteralFieldValue.class, new LiteralFieldValueSerializer());
    gsonBuilder.registerTypeAdapter(AttachmentFieldValue.class, new AttachmentFieldValueAdapter());
    gsonBuilder.registerTypeAdapter(BoundFieldValue.class, new BoundFieldValueSerializer());
    gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
    gsonBuilder.serializeNulls();
    return gsonBuilder;
}