Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

431
Visualizações
How to pretty print a complex Java object (e.g. with fields that are collections of objects)?

I'm looking for a library function (ideally from a commonly used framework e.g. Spring, Guava, Apache Commons etc.) that will nicely print the values of any Java object.

This is a general question rather than a specific one. Have seen similar questions on StackOverflow for which a common answer is "implement your own toString() method on the class" but this option isn't always practical - am looking for a general way of doing this with any object I come across, which may originate from third party code. Another suggestion is to use RefectionToStringBuilder from Apache Commons, e.g:

new ReflectionToStringBuilder(complexObject, new RecursiveToStringStyle()).toString()

But this has limited use - e.g. when it comes across a collection it tends to output something like this:

java.util.ArrayList@fcc7ab1[size=1]

An actual use case example is to log an Iterable<PushResult> returned from JGit's pushCommand.call() method - if posting an answer please make sure it would work with this as well as any other complex object.

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

You could try and use Gson. it also serializes Arrays, Maps or whatever....

MyObject myObject = new MyObject();
Gson gson = new GsonBuilder().setPrettyPrinting().serializeNulls().create();
gson.toJson(myObject);

For deserialization use:

gson.fromJson(MyObject.class);

For typed maps see this answer: Gson: Is there an easier way to serialize a map

over 4 years ago · Santiago Trujillo Relatório

0

You can use the Jackson ObjectMapper class is use to bind data with json. you can use it like below:

ObjectMapper mapper = new ObjectMapper();

you can save json into object like below

Object json = mapper.readValue(input,object.class);

you can write that in string variable

String prettyJson = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(json);

it should work fine.

over 4 years ago · Santiago Trujillo Relatório

0

You can use GSON to convert your object to string. This will work for all the objects,

Gson gson = new Gson();
System.out.println(gson.toJson(objectYouWantToPrint).toString());
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda