Example usage for com.google.gson FieldNamingPolicy LOWER_CASE_WITH_UNDERSCORES

List of usage examples for com.google.gson FieldNamingPolicy LOWER_CASE_WITH_UNDERSCORES

Introduction

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

Prototype

FieldNamingPolicy LOWER_CASE_WITH_UNDERSCORES

To view the source code for com.google.gson FieldNamingPolicy LOWER_CASE_WITH_UNDERSCORES.

Click Source Link

Document

Using this naming policy with Gson will modify the Java Field name from its camel cased form to a lower case field name where each word is separated by an underscore (_).

Usage

From source file:org.restexpress.plugin.gson.json.GsonJsonProcessor.java

License:Apache License

protected Gson newGson() {
    return new GsonBuilder().disableHtmlEscaping()
            .registerTypeAdapter(Date.class, new GsonTimepointSerializer()) //
            .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) //
            .create();/*from   ww  w.  j  a  va  2  s . co  m*/
}

From source file:org.restexpress.serialization.json.GsonJsonProcessor.java

License:Apache License

public GsonJsonProcessor(boolean shouldOutboundEncode) {
    super();//from   w w  w . j a va  2s.  c om
    GsonBuilder builder = new GsonBuilder().disableHtmlEscaping()
            .registerTypeAdapter(Date.class, new GsonTimepointSerializer())
            .setDateFormat(DateAdapterConstants.TIMESTAMP_OUTPUT_FORMAT)
            .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);

    if (shouldOutboundEncode) {
        builder.registerTypeAdapter(String.class, new GsonEncodingStringSerializer());
    }

    gson = builder.create();
}

From source file:org.sociotech.communitymashup.source.languagedetection.apiwrapper.DetectLanguageAPIWrapper.java

License:Open Source License

/**
 * Gets the gson builder.//from   w  w  w. j  a  va2 s.  com
 * 
 * @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./* w  w  w  .j  a  v a 2  s .co m*/
 * 
 * @return the gson builder
 */
protected GsonBuilder getGsonBuilder() {
    GsonBuilder builder = new GsonBuilder();

    builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);

    return builder;
}

From source file:org.syphr.lametrictime.api.common.impl.GsonGenerator.java

License:Apache License

public static Gson create(boolean prettyPrint) {
    GsonBuilder builder = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
            .registerTypeAdapterFactory(new ApplicationTypeAdapterFactory())
            .registerTypeAdapterFactory(new ActionTypeAdapterFactory())
            .registerTypeAdapterFactory(new UpdateActionTypeAdapterFactory())
            .registerTypeAdapterFactory(RuntimeTypeAdapterFactory.of(Parameter.class, "data_type")
                    .registerSubtype(BooleanParameter.class, "bool")
                    .registerSubtype(StringParameter.class, "string")
                    .registerSubtype(IntegerParameter.class, "int"));

    // add Java 8 Time API support
    JSR310TypeAdapters.registerJSR310TypeAdapters(builder);

    if (prettyPrint) {
        builder.setPrettyPrinting();//from w  w w  .j  a  v a 2  s.  co m
    }

    return builder.create();
}

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//  ww  w  .  ja  v  a 2s . co  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.
 *///w w w .ja  va 2s .co  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;
}

From source file:org.zoxweb.server.util.GSONUtil.java

License:Apache License

private GSONUtil() {
    builder = new GsonBuilder();
    builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
    builder.setPrettyPrinting();//from   w w w  . j  a v  a2s . co m
    log.info("Created");
}

From source file:pl.llp.aircasting.guice.GsonProvider.java

License:Open Source License

@Override
public Gson get() {
    // All fields need to be explicitly marked with @Expose
    return gsonBuilder.excludeFieldsWithModifiers(Modifier.STATIC).excludeFieldsWithoutExposeAnnotation()
            .setDateFormat("MMM d, yyyy h:m:s a z")
            .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
            .registerTypeAdapter(Note.class, noteAdapter).create();
}

From source file:py.una.pol.ii.tareaweb2.ejb.VentaLogic.java

public String cargaMasiva(String path) throws FileNotFoundException, IOException {
    FileReader fr;//ww w  . j av  a  2s.com
    BufferedReader br;
    File f;
    Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd")
            .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
    String line;
    String erroresDetectados = "";
    path = "/tmp/" + path;
    f = new File(path);
    fr = new FileReader(f);
    br = new BufferedReader(fr);
    int contadorErrores = 0;
    int contadorLineas = 0;
    boolean error = false;
    boolean constraintError = false;
    String respuesta;

    while (br.ready()) {
        line = br.readLine();
        contadorLineas++;
        Venta toPersist = new Venta();
        Venta fromJson;
        try {
            fromJson = gson.fromJson(line, Venta.class);
            if (fromJson.getCliente() == null || fromJson.getCliente().getId() == null) {
                erroresDetectados = erroresDetectados + "Lnea " + contadorLineas + ": Cliente invalido\n";
                error = true;
                contadorErrores++;
            }
            if (fromJson.getFecha() == null) {
                erroresDetectados = erroresDetectados + "Lnea " + contadorLineas + ": Fecha nula\n";
                error = true;
                contadorErrores++;
            }
            if (fromJson.getMontoTotal() == null) {
                erroresDetectados = erroresDetectados + "Lnea " + contadorLineas + ": Monto Total nulo\n";
                error = true;
                contadorErrores++;
            }
            if (fromJson.getDetalles() == null || fromJson.getDetalles().isEmpty()) {
                erroresDetectados = erroresDetectados + "Lnea " + contadorLineas + ": No contiene detalles\n";
                error = true;
                contadorErrores++;
            }

            toPersist.setCliente(fromJson.getCliente());
            toPersist.setDetalles(fromJson.getDetalles());
            toPersist.setFactura(fromJson.getFactura());
            toPersist.setFecha(fromJson.getFecha());
            toPersist.setMontoTotal(fromJson.getMontoTotal());
            if (!error) {
                try {
                    if (!(respuesta = ventaLogic.create(toPersist)).contains("Venta Creada --")) {
                        if (!constraintError && respuesta.contains("Cantidad menor o igual a 0. "))
                            constraintError = true;

                        erroresDetectados = erroresDetectados + "Lnea " + contadorLineas + ": " + respuesta
                                + "\n";
                        contadorErrores++;
                    }
                } catch (Exception e) {
                    erroresDetectados = erroresDetectados + "Lnea " + contadorLineas
                            + ": Error al intentar persistir\n";
                    contadorErrores++;

                }
            }
        } catch (Exception e) {
            erroresDetectados = erroresDetectados + "Lnea " + contadorLineas + ": Json malformado\n";
            contadorErrores++;

        }
        error = false;
    }
    br.close();
    System.out.println("Total de lneas: " + contadorLineas);
    System.out.println("Errores detectados: " + contadorErrores);
    System.out.println("Descripcin de los errores: " + erroresDetectados);

    if (constraintError) {

        try {
            entityManager.flush();// se fuerza a que ocurra el error de restriccion

        } catch (PersistenceException e) {
            Throwable t = e.getCause();
            while ((t != null) && !(t instanceof ConstraintViolationException)) {
                t = t.getCause();
            }
            if (t instanceof ConstraintViolationException) {
                erroresDetectados = erroresDetectados + "\n Violacin de restriccin atrapada.";

            } else {

                erroresDetectados = erroresDetectados + " Error interno.";
            }

            return erroresDetectados;
        }

    } else if (contadorErrores > 0) {
        context.setRollbackOnly(); //al decir que haga rollback ya no se violara el constraint de la base de datos por lo que no podra capturarse directamente.
        return erroresDetectados;
    }
    return "Carga masiva Exitosa, " + contadorLineas + " ventas cargadas";
}