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.

toJson() fails when trying to serialize an object of DoublyLinkedList<Record>

See original GitHub issue

I have a DoublyLinkedList<Record> object I want to jsonify with Gson, but when doing so I get this common infinite loop error even when I have done everything by the books:

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.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.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)

Record.java

class Record {

private String objectName;
private Object object;

Record(String objectName, Object object) {
    this.objectName = objectName;
    this.object = object;
}

Object getObject() {
    return object;
}
String getObjectName() {
    return objectName;
  }
}

Gson Deserializing:

  Type classType = new TypeToken<DoublyLinkedList<Record>>() {}.getType();
  DoublyLinkedList<Record> linkedList = new Gson().fromJson(json, classType);

Gson Serializing:

  Type classType = new TypeToken<DoublyLinkedList<Record>>() {}.getType();
  String json = gson.toJson(list,classType);

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
JakeWhartoncommented, Nov 12, 2018

I can’t tell if you have a really deeply nested tree or a cyclic reference (or both). Can you provide an executable sample that demonstrates the problem?

0reactions
Muddzcommented, Nov 29, 2018

@JakeWharton Yes. Thank you for your time.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to serialize Object to Json using Jackson
I tried this post, but it didn't help. Here is the class I'm trying to serialize: public class Repository { public String name; ......
Read more >
How to Fix 'Cannot serialize object because no ... - YouTube
RestAssured #Automation #Error #message #IllegalStateException #JSON #serializer #classpath #Jackson #Databind #Gson #Johnzon #Yasson ...
Read more >
Efficient JSON serialization with Jackson and Java
The serialization document, of course, can be stored, retrieved, shared, and archived. A preferred format was, once upon a time, XML; in recent ......
Read more >
Serialization Error Handling - Json.NET
The error event is raised whenever an exception is thrown while serializing or deserializing JSON. Like all settings found on JsonSerializer, it can...
Read more >
Migrate from Newtonsoft.Json to System.Text.Json - .NET
The System.Text.Json namespace provides functionality for serializing to and deserializing from JavaScript Object Notation (JSON). The System.
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