question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Exception on toJson

See original GitHub issue

I’m trying to generate a JSON from an object that represents an undirected Graph, but when I run the program it throws this Exception over and over again until the program stops at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245) at com.google.gson.Gson$FutureTypeAdapter.write(Gson.java:1018) The weird thing is that it generates the JSON, the size is 57MB and I don’t know if that is normal or if is correct because is too big for reviewing it all. This is the code that I use to create the JSON Gson gson = new GsonBuilder().setPrettyPrinting().create(); File file = new File ("data/test.json"); if (!file.exists()) file.createNewFile(); FileWriter writer = new FileWriter(file); gson.toJson(graph,writer); writer.close();

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
JakeWhartoncommented, Nov 19, 2018

A doubly-linked list seems like a pretty obvious place where a reference cycle would exist and cause infinite recursion. Did you register a custom type adapter for it?

On Sun, Nov 18, 2018, 11:04 AM JuanWTF <notifications@github.com wrote:

It doesn’t, this is my implementation, some variable names are in Spanish, but I think it is easy to understand, I use an adjacency list with a hashmap of LinkedList instead of an array because the number of vertexes can vary and I need to save information about the weight of the edges.

`public class Grafo <K extends Comparable , V extends Vertice, A > implements IGrafo<K, V, A> { private int vertices;

private int arcos;

HashChain <K,DoublyLinkedList<K,V>>listaAdyacencia;

public Grafo () { vertices = 0; arcos = 0; listaAdyacencia = new HashChain <K, DoublyLinkedList<K,V>>(80000); } @Override public int V() { return vertices; }

@Override public int E() { return arcos; }

@Override public void addVertex(K idVertex, V infoVertex) { DoublyLinkedList<K,V> temp = new DoublyLinkedList <K,V>(idVertex, infoVertex); listaAdyacencia.put(idVertex, temp); vertices++; }

@Override public void addEdge(K idVertexIni, K idVertexFin, A infoArc) { DoublyLinkedList<K,Vertice > temp = (DoublyLinkedList<K, Vertice>) listaAdyacencia.get(idVertexIni); temp.add(idVertexFin, new Interseccion((int) idVertexFin,infoArc)); arcos++; }

@Override public V getInfoVertex(K idVertex) { return listaAdyacencia.get(idVertex).getFirst().darValor(); }

@Override public void setInfoVertex(K idVertex, V infoVertex) { listaAdyacencia.get(idVertex).getFirst().cambiarElemento(idVertex, infoVertex); }

@Override public A getInfoArc(K idVertexIni, K idVertexFin) { return listaAdyacencia.get(idVertexIni).get(idVertexFin).darValor().darInformaciónArco(); }

@Override public void setInfoArc(K idVertexIni, K idVertexFin, A infoArc) { listaAdyacencia.get(idVertexIni).get(idVertexFin).darValor().cambiarInformacionArco(infoArc); }

@Override public Iterator<K> adj(K idVertex) { return listaAdyacencia.get(idVertex).iteradorK(); }

}`

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/google/gson/issues/1429#issuecomment-439703762, or mute the thread https://github.com/notifications/unsubscribe-auth/AAEEEZuozVubNjqQggY8A_bs8NGmPIkrks5uwYUPgaJpZM4YnyHV .

1reaction
JuanJTorres11commented, Nov 18, 2018

It doesn’t, this is my implementation, some variable names are in Spanish, but I think it is easy to understand, I use an adjacency list with a hashmap of LinkedList instead of an array because the number of vertexes can vary and I need to save information about the weight of the edges.

`public class Grafo <K extends Comparable <K>, V extends Vertice, A > implements IGrafo<K, V, A> { private int vertices;

private int arcos;

HashChain <K,DoublyLinkedList<K,V>>listaAdyacencia;

public Grafo ()
{
	vertices = 0;
	arcos = 0;
	listaAdyacencia = new HashChain <K, DoublyLinkedList<K,V>>(80000);
}
@Override
public int V()
{
	return vertices;
}

@Override
public int E()
{
	return arcos;
}

@Override
public void addVertex(K idVertex, V infoVertex)
{
	DoublyLinkedList<K,V> temp = new DoublyLinkedList <K,V>(idVertex, infoVertex);
	listaAdyacencia.put(idVertex, temp);
	vertices++;
}

@Override
public void addEdge(K idVertexIni, K idVertexFin, A infoArc)
{
	DoublyLinkedList<K,Vertice <A>> temp = (DoublyLinkedList<K, Vertice<A>>) listaAdyacencia.get(idVertexIni);
	temp.add(idVertexFin, new Interseccion<A>((int) idVertexFin,infoArc));
	arcos++;
}

@Override
public V getInfoVertex(K idVertex)
{
	return listaAdyacencia.get(idVertex).getFirst().darValor();
}

@Override
public void setInfoVertex(K idVertex, V infoVertex)
{
	listaAdyacencia.get(idVertex).getFirst().cambiarElemento(idVertex, infoVertex);
}

@Override
public A getInfoArc(K idVertexIni, K idVertexFin)
{
	return listaAdyacencia.get(idVertexIni).get(idVertexFin).darValor().darInformaciónArco();
}

@Override
public void setInfoArc(K idVertexIni, K idVertexFin, A infoArc)
{
	listaAdyacencia.get(idVertexIni).get(idVertexFin).darValor().cambiarInformacionArco(infoArc);
}

@Override
public Iterator<K> adj(K idVertex)
{
	return listaAdyacencia.get(idVertex).iteradorK();
}

}`

Read more comments on GitHub >

github_iconTop Results From Across the Web

Convert Exception to JSON - java - Stack Overflow
Below is the routine to convert an Exception to JSON in a standardized way: public static JSONObject convertToJSON(Throwable e, ...
Read more >
Rendering Exceptions in JSON with Spring - Baeldung
In this tutorial, we'll go over passing a Java exception as part of a JSON response using Spring. For a broader look, check...
Read more >
JsonException Class - Json.NET
The exception thrown when an error occurs during JSON serialization or deserialization. ... The JsonException type exposes the following members. Constructors ...
Read more >
JsonException (Java(TM) EE 7 Specification APIs)
Constructs a new runtime exception with the specified detail message. The cause is not initialized, and may subsequently be initialized by a call...
Read more >
Exceptions - JSON for Modern C++
All exceptions inherit from class json::exception (which in turn inherits from std::exception ). It is used as the base class for all exceptions...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found