Error converting to Json with Gson

1

Well, I'm having this error while doing the object conversion to json. I'm using gson. I tried to send the whole object and I had the same error, I changed and separated the object to try to find 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 error:

                                                                     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'd rather send the whole object through webservice. Note: I'm using the volley.

Sale 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;

ItemVend 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;

Class Person:

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;

Class Address

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;
    
asked by anonymous 26.05.2016 / 22:10

1 answer

2

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

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

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

And the object of the Sale class refers to the Item Sales, which in turn refers to the original sale, you will have this problem.

If this is the case, you can note the property that points to the "parent" class so that it is not 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 , which will cause it not to be serialized:

public class ItemVenda {
    // outros membros
    private transient Venda venda;
}
    
26.05.2016 / 23:14