search
HomeJavajavaTutorialHow to translate FieldNamingPolicy enum into Chinese using Gson library in Java?

How to translate FieldNamingPolicy enum into Chinese using Gson library in Java?

Aug 23, 2023 pm 07:33 PM
translategson libraryfieldnamingpolicy

How to translate FieldNamingPolicy enum into Chinese using Gson library in Java?

The Gson library provides naming conventions as part of the enum . We can set the field naming strategy using the method of the class.

FieldNamingPolicy enumeration constant

  • IDENTITY - With this naming policy, the field name does not change.
  • LOWER_CASE_WITH_DASHES - Use this naming strategy to modify Java field names from camelCase to lowercase field names with a dash (-) between each word.
  • LOWER_CASE_WITH_UNDERSCORES - Use this naming strategy to modify Java field names from camelCase to lowercase field names, with each word separated by an underscore (_).
  • UPPER_CAMEL_CASE - Use this naming strategy to ensure that the first letter of Java field names is capitalized when serialized into their JSON form.
  • UPPER_CAMEL_CASE_WITH_SPACES - Use this naming strategy to ensure that when Java field names are serialized into their JSON form, the first letter is capitalized and words are separated by spaces.

Syntax

public enum FieldNamingPolicy extends Enum<FieldNamingPolicy> implements FieldNamingStrategy

Example

import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class GsonFieldNamingPolicyTest {
   public static void main(String[] args) {
      Employee emp = new Employee();
      emp.setEmpId(115);
      emp.setFirstName("Raja");
      emp.setLastName("Ramesh");
      GsonBuilder gsonBuilder = new GsonBuilder();
      Gson gson1  =  gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES).create();
      String result1 = gson1.toJson(emp);
      System.out.println("LOWER_CASE_WITH_DASHES: " + result1);
      Gson gson2 =  gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
      String result2 = gson2.toJson(emp);
      System.out.println("LOWER_CASE_WITH_UNDERSCORES: " + result2);
      Gson gson3 = gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE).create();
      String result3 = gson3.toJson(emp);
      System.out.println("UPPER_CAMEL_CASE: " + result3);
      Gson gson4 = gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE_WITH_SPACES).create();
      String result4 = gson4.toJson(emp);
      System.out.println("UPPER_CAMEL_CASE_WITH_SPACES: " + result4);
      Gson gson5 = gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.IDENTITY).create();
      String result5 = gson5.toJson(emp);
      System.out.println("IDENTITY: " + result5);
   }
}
// Employee class
class Employee {
   private int empId;
   private String firstName;
   private String lastName;
   public int getEmpId() {
      return empId;
   }
   public void setEmpId(int empId) {
      this.empId = empId;
   }
   public String getFirstName() {
      return firstName;
   }
   public void setFirstName(String firstName) {
      this.firstName = firstName;
   }
   public String getLastName() {
      return lastName;
   }
   public void setLastName(String lastName) {
      this.lastName = lastName;
   }
}

Output

LOWER_CASE_WITH_DASHES: {"emp-id":115,"first-name":"Raja","last-name":"Ramesh"}
LOWER_CASE_WITH_UNDERSCORES: {"emp_id":115,"first_name":"Raja","last_name":"Ramesh"}
UPPER_CAMEL_CASE: {"EmpId":115,"FirstName":"Raja","LastName":"Ramesh"}
UPPER_CAMEL_CASE_WITH_SPACES: {"Emp Id":115,"First Name":"Raja","Last Name":"Ramesh"}
IDENTITY: {"empId":115,"firstName":"Raja","lastName":"Ramesh"}

The above is the detailed content of How to translate FieldNamingPolicy enum into Chinese using Gson library in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:tutorialspoint. If there is any infringement, please contact admin@php.cn delete
How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?Mar 17, 2025 pm 05:46 PM

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?Mar 17, 2025 pm 05:45 PM

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?Mar 17, 2025 pm 05:44 PM

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?Mar 17, 2025 pm 05:43 PM

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

How does Java's classloading mechanism work, including different classloaders and their delegation models?How does Java's classloading mechanism work, including different classloaders and their delegation models?Mar 17, 2025 pm 05:35 PM

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool