Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

217
Views
Mapa de Jackson a json con cambio de caso

Quiero convertir el mapa a json pero cambiando el caso usando jackson. Por ejemplo, tengo este mapa:

 "test_first" -> 1, "test_second" -> 2,

Quiero convertirlo a json pero cambiando de guión bajo a minúsculasCamelCase. ¿Cómo puedo hacer eso? Usar esto no ayudó:

 // Map<String, String> fields; var mapper = new ObjectMapper(); mapper.setPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CAMEL_CASE); // setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE) didn't help too String json = mapper.writeValueAsString(fields);
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Utilice la anotación @JsonProperty . Sobre su variable de propiedad o sobre su getter, haga esto:

 @JsonProperty("testFirst") String test_first; @JsonProperty("testSecond") String test_second;

Aparentemente, también puede usar las anotaciones @JsonGetter y @JsonSetter como alternativa. Lea sobre esto en Jackson Annotation Examples areticle

over 4 years ago · Santiago Trujillo Report

0

Hay StringKeySerializer en Jackson que puede implementar la funcionalidad para cambiar la presentación de las claves en algún mapa (por ejemplo, usando Guava CaseFormat ):

 // custom key serializer class SnakeToCamelMapKeySerialiser extends StdKeySerializers.StringKeySerializer { @Override public void serialize(Object value, JsonGenerator g, SerializerProvider provider) throws IOException { g.writeFieldName(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, (String) value)); } } // map with the custom serializer @JsonSerialize(keyUsing = SnakeToCamelMapKeySerialiser.class) class MyMap<K extends String, V> extends HashMap<K, V> { }

Luego, el mapa se serializa con el formato requerido:

 Map<String, Integer> map = new MyMap<>(); map.put("first_key", 1); map.put("second_key", 2); ObjectMapper mapper = new ObjectMapper(); String json = mapper.writeValueAsString(map); System.out.println(json); // -> {"firstKey":1,"secondKey":2}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!