Error while converting to Json with Gson

Asked

Viewed 1,403 times

1

Guys, I’m having this error when converting the object to json. I’m using gson. I have tried to send the whole object and had the same error, changed and separated the object to try to locate where the problem is, but without success.

Gson gson = new Gson();

    HashMap<String, String> params = new HashMap<>();
    params.put("cliente",gson.toJson(vend.getCliente()));
    params.put("empresa",gson.toJson(vend.getEmpresa()));
    params.put("endereco",gson.toJson(vend.getEndereco()));
    params.put("carrinho",gson.toJson(vend.getCarrinho()));
    params.put("formPagamento",gson.toJson(vend.getFormPagamento()));
    params.put("troco",gson.toJson(vend.getTrocoParaVenda()));
    params.put("data",gson.toJson(vend.getDataVenda()));
    params.put("valor",gson.toJson(vend.getVlrTotalVenda()));

I get this mistake:

                                                                     java.lang.StackOverflowError
                                                                       at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:147)
                                                                       at java.lang.StringBuffer.append(StringBuffer.java:219)
                                                                       at java.io.StringWriter.write(StringWriter.java:147)
                                                                       at com.google.gson.stream.JsonWriter.string(JsonWriter.java:551)
                                                                       at com.google.gson.stream.JsonWriter.writeDeferredName(JsonWriter.java:402)
                                                                       at com.google.gson.stream.JsonWriter.value(JsonWriter.java:465)
                                                                       at com.google.gson.internal.bind.TypeAdapters$3.write(TypeAdapters.java:169)
                                                                       at com.google.gson.internal.bind.TypeAdapters$3.write(TypeAdapters.java:151)
                                                                       at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
                                                                       at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:112)
                                                                       at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:239)
                                                                       at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
                                                                       at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:97)
                                                                       at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:61)
                                                                       at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
                                                                       at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:112)
                                                                       at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:239)
                                                                       at com.google.gson.Gson$FutureTypeAdapter.write(Gson.java:968)
                                                                       at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
                                                                       at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:97)
                                                                       at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:61)
                                                                       at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
                                                                       at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:112)
                                                                       at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:239)
                                                                       at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
                                                                       at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:112)
                                                                       at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:239)
                                                                       at com.google.gson.Gson$FutureTypeAdapter.write(Gson.java:968)
                                                                       at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
                                                                       at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:97)
                                                                       at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:61)
                                                                       at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
                                                                       at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:112)
                                                                       at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:239)
                                                                       at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
                                                                       at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:112)
                                                                       at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:239)
                                                                       at com.google.gson.Gson$FutureTypeAdapter.write(Gson.java:968)
                                                                       at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
                                                                       at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:97)
                                                                    at com.google

If possible I prefer to send the whole object through the webservice. Note: I’m using Volley.

Sales class:

public class Venda  {
private Long idVenda;
private Date dataVenda;
private String statusVenda;
private Double vlrTotalVenda;
private Pessoa empresa;
private Pessoa cliente;
private Double trocoParaVenda;
private Endereco endereco;
private Boolean retirada;
private List<ItemVenda> carrinho;
private String formPagamento;

Itemvenda class

public class ItemVenda {
private Long idItemVenda;
private Integer quantItemVenda;
private Produto produtoItemVenda;
private Double vlrItemVenda;
@Expose(serialize = false, deserialize = false)
private Venda vendaItemVenda;
private List<Ingrediente> ingredientesProduto;
private List<Ingrediente> ingredientesAdicionais;

Classe Pessoa:

public class Pessoa {

private Long idPessoa;
private String nomeFantasiaPessoa;
private String imagem;
private String razaoSocialPessoa;
private String senhaPessoa;
private String telefonePessoa;
private String celularPessoa;
private String emailPessoa;
private String statusPessoa;
private String tipoPessoa;
private String cpfCnpjPessoa;
private String rgIePessoa;
private List<Endereco> enderecos;
private String categoriaPessoa;
private String tempoPreparo;
private Double valorEntrega;
private List<Categoria> categorias;

Address Class

public class Endereco  {
private Long idEndereco;
private String estadoEndereco;
private String cidadeEndereco;
private String bairroEndereco;
private String ruaEndereco;
private String numeroEndereco;
private String complementoEndereco;
private String cepEndereco;
private Long pessoa;
  • What is the definition of the class ItemVenda? If she has a reference to the class Venda, you will have this problem.

1 answer

2

You probably have a circular dependency on your data definition. For example, if you have this definition:

public class Venda {
    // outros membros
    private List<ItemVenda> carrinho;
}

public class ItemVenda {
    // outros membros
    private Venda venda;
}

And the object of the Sale class references the Itemvenda, which in turn references the original sale, you will have this problem.

If this is the case, you can note the property that points to the "parent" class not to be serialized, in order to break this circular reference, as in the example below.

public class ItemVenda {
    // outros membros
    @Expose(serialize = false, deserialize = false)
    private Venda venda;
}

If I’m not mistaken, you can also declare the field as transient, that will cause it not to be serialized:

public class ItemVenda {
    // outros membros
    private transient Venda venda;
}
  • opa, have it right! I think q should be this the same problem, I will test and return with the result!

  • I thought it would be the solution, but unfortunately it was not this time, I added the remaining classes if I can help, I am grateful

  • Do you have any semantic problems in the Person/Category classes? Another thing to do is comment on each of the class fields and continue running until you find the one causing the error...

  • In Person/Category I do not receive from the converted server in json without problems. I have had with the address but I have already fixed putting.

  • The problem is in the shopping cart itself.

  • Friend, the problem really is where you said, but as your solution did not work I put as Long even, I do not need to bring the whole class.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.