UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Double Linked List and JSON

Forums > Swift

Anyone have any insights to encoding/decoding double linked lists with JSON? This is just the pertinent part of the class.

If I leave off the parent var, then the application (Document Based SwiftUI) saves and loads fine - no problems (except of course the parent functionality is lost).

public class Sidebar: Codable, ObservableObject, Identifiable {

    public let objectWillChange = ObservableObjectPublisher()

    enum CodingKeys: CodingKey {
        case id
        case parent
        case children
    }

    public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)

        try container.encode(id, forKey: .id)
        try container.encode(parent, forKey: .parent)
        try container.encode(children, forKey: .children)
    }

    public required init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)

        id = UUID()
        parent = try container.decode(Sidebar?.self, forKey: .parent)
        children = try container.decode([Sidebar]?.self, forKey: .children)
    }

    @Published  public var id: UUID?
    @Published  public var parent: Sidebar?
    @Published  public var children: [Sidebar]?

    public init() {
        id = UUID()
    }

With the parent var inserted and working (linked) the app crashes when saving during this call with a bad access error:

  public func encode(to encoder: Encoder) throws {

      var singleContainer = encoder.singleValueContainer()

      try singleContainer.encode(NSKeyedArchiver.archivedData(withRootObject: attrString, requiringSecureCoding: false))
  }

It appears circular referenses give JSON indigestion.

Suggestions?

2      

This look less like a doubly linked list and more like a directed graph which, if it ever happens to contain a cycle, will cause serialization/deserialization to go into an infinite loop.

For this scenario, I'd be less inclined to do a direct serialization of this structure and more likely to create an intermediate structure. Start with a class - call it, say, FlattenedSidebar - that represents Sidebar as containing only id objects, so parent is an id and children is an array of id. That is the thing that gets written to JSON. Coming back the other way, you'd want a single Dictionary of id to Sidebar. Each FlattenedSidebar you read you wind up having to transform back into a Sidebar, which you either look up in the dictionary or create a new one of. Finally, you make a sweep through all the Sidebars and make sure they're wired up.

That's not really efficient, but it's a shortcoming of JSON.

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.