Add serialize loop handling to prevent crashing for circular loops