Sample CodeReviewed 2026-07-21View on Apple Developer

Using JSON with custom types

At a glance

Item Summary
Purpose Encode and decode JSON data, regardless of its structure, using Swift’s JSON support.
App architecture A Swift sample with the source-visible chain GroceryStoreGroceryStoreServiceFoundation APIs.
Main patterns Service object, Central store
Project style 6 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries.
Execution model No structured execution marker indexed; callback threading requires source review.
State/event model No structured observation or publisher-scheduling marker indexed.
Key frameworks/packages Foundation; these are source dependencies, not architecture labels.

Project structure

Source bundle/
└── Using-JSON-with-Custom-Types.playground/
    └── Pages/
        ├── Access Nested Data.xcplaygroundpage/
        │   └── Contents.swift
        ├── Merge Data from Different Depths.xcplaygroundpage/
        │   └── Contents.swift
        ├── Change Key Names.xcplaygroundpage/
        │   └── Contents.swift
        ├── Read Data from Arrays.xcplaygroundpage/
        │   └── Contents.swift
        ├── LICENSE.xcplaygroundpage/
        │   └── Contents.swift
        └── Table of Contents.xcplaygroundpage/
            └── Contents.swift

Structure observations

  • Architecturally prominent files are ranked from entry points and role-named declarations; resource-only paths are omitted.
  • Primary languages: Swift.
  • The verified tree contains 0 project/configuration file(s) and 10 source declaration(s).

Overall architecture

Reference code

Using-JSON-with-Custom-Types.playground/Pages/Access Nested Data.xcplaygroundpage/Contents.swift:34 — architecture anchor

struct GroceryStore {
    var name: String
    var products: [Product]

    struct Product: Codable {
        var name: String
        var points: Int
        var description: String?
    }
}

Interpretation

The arrows summarize the source-visible entry, role-named types or folders, and framework direction; when nodes come from structural folders, the sequence is a high-level interpretation rather than proof that every adjacent node calls the next. Ownership is claimed only where the next section cites a stored property or assignment. The diagram is intentionally limited to the dominant path into Foundation.

Ownership and state

Ownership evidence

Using-JSON-with-Custom-Types.playground/Pages/Access Nested Data.xcplaygroundpage/Contents.swift:35 — stored dependency or nearest verified ownership anchor

struct GroceryStore {
    var name: String
    // ...
}
Owner Object or state Relationship Mutation authority
GroceryStore String (name) owns value state App/module collaborators
GroceryStore Array (products) owns value state App/module collaborators
Product String (name) owns value state App/module collaborators
Product Int (points) owns value state App/module collaborators

Composition arrows indicate a source-visible construction expression or locally owned value state; aggregation means the owner stores or receives a dependency without proving exclusive lifetime ownership.

Concurrency, scheduling, and thread safety

Evidence limit: actor isolation, async/await, or Task creation does not by itself prove background-thread execution; Sendable conformance alone does not prove thread-safe mutation.

No source-visible execution, scheduling, or synchronization boundary was found in the indexed source.

@MainActor/MainActor.run, DispatchQueue.main, and RunLoop.main are reported as distinct isolation, queue, and event-loop mechanisms. A plain Task is kept separate from Task.detached; neither is labeled as a background thread.

State propagation, frameworks, and dependencies

Evidence limit: an import proves a source-level compilation dependency at the cited line; it does not prove runtime use, architectural adoption, or whether a Swift package is a direct application dependency.

Category Mechanism or module Verified role Evidence
Source import Foundation The cited file imports this module; runtime use and architectural role are not inferred. Using-JSON-with-Custom-Types.playground/Pages/Access Nested Data.xcplaygroundpage/Contents.swift:32

receive(on:) describes downstream delivery scheduling, while subscribe(on:) describes upstream subscription/request/cancel scheduling. An import Combine alone establishes neither behavior nor a Store, reducer, Redux, or other application architecture.

Class and protocol design

Using-JSON-with-Custom-Types.playground/Pages/Access Nested Data.xcplaygroundpage/Contents.swift:34 — representative type boundary

struct GroceryStore {
    var name: String
    // ...
}
Type Responsibility Depends on or conforms to
GroceryStore Centralized state or persistence access Concrete collaborators/imported frameworks
GroceryStoreService Framework-facing operations Decodable
GroceryStore Centralized state or persistence access Concrete collaborators/imported frameworks
Product Represents a feature value or composable behavior Codable
Aisle Represents a feature value or composable behavior Decodable
Shelf Represents a feature value or composable behavior Decodable
Product Represents a feature value or composable behavior Concrete collaborators/imported frameworks
ProductKey Represents a feature value or composable behavior CodingKey
GroceryProduct Represents a feature value or composable behavior Codable
GroceryProduct Represents a feature value or composable behavior Codable

No local protocol conformance is claimed as protocol-oriented design; external framework conformances are listed only as dependencies.

Access control

Symbol Access Verified effect Likely rationale
Contents (Using-JSON-with-Custom-Types.playground/Pages/Merge Data from Different Depths.xcplaygroundpage/Contents.swift:124) public The symbol is visible to importing modules. Inference: make the declaration available across a module or target boundary.

Reference code

Using-JSON-with-Custom-Types.playground/Pages/Merge Data from Different Depths.xcplaygroundpage/Contents.swift:124 — representative boundary

    public init(from decoder: Decoder) throws {
        var products = [Product]()
        // ...
    }

Swift declarations without a modifier are internal; explicit private, fileprivate, private(set), public, or open entries above are interpreted by language semantics. Objective-C/C samples instead rely on header and implementation boundaries, which are not equivalent to Swift lexical privacy.

Logic ownership and placement

Logic Owning type or file Placement rationale
Framework-facing operations GroceryStoreService The source’s Service suffix makes this role explicit.
Centralized state or persistence access GroceryStore The source’s Store suffix makes this role explicit.

Design patterns

Pattern Source evidence Purpose or tradeoff
Service object Using-JSON-with-Custom-Types.playground/Pages/Access Nested Data.xcplaygroundpage/Contents.swift:115 A role-named service contains framework-facing operations.
Central store Using-JSON-with-Custom-Types.playground/Pages/Access Nested Data.xcplaygroundpage/Contents.swift:34 A store-named type centralizes feature state or persistence.

Naming conventions

  • Types: Service: GroceryStoreService; Store: GroceryStore.
  • Protocols: no local protocol declaration in the scanned source.
  • Methods: encode.
  • Files: feature/project roles rather than a strict one-type-per-file rule.

Architecture takeaways

  • GroceryStore is the main source-visible entry or composition anchor for this sample.
  • Framework work reaches Foundation through a deliberately small high-level chain; the detailed API graph remains inside the cited implementation files.
  • Stored-property evidence identifies lifecycle collaboration; it does not by itself prove exclusive object ownership.
  • Access-control conclusions separate verified language visibility from the likely design rationale.
  • The source does not justify labeling the design protocol-oriented.

Source map

Source file Relevant symbols
Using-JSON-with-Custom-Types.playground/Pages/Access Nested Data.xcplaygroundpage/Contents.swift GroceryStore, Cited implementation, GroceryStoreService, Foundation, Product, Aisle, Shelf
Using-JSON-with-Custom-Types.playground/Pages/Merge Data from Different Depths.xcplaygroundpage/Contents.swift Cited implementation, GroceryStore, Product, ProductKey
Using-JSON-with-Custom-Types.playground/Pages/Change Key Names.xcplaygroundpage/Contents.swift GroceryProduct, CodingKeys
Using-JSON-with-Custom-Types.playground/Pages/Read Data from Arrays.xcplaygroundpage/Contents.swift GroceryProduct
Using-JSON-with-Custom-Types.playground/Pages/LICENSE.xcplaygroundpage/Contents.swift Feature implementation
Using-JSON-with-Custom-Types.playground/Pages/Table of Contents.xcplaygroundpage/Contents.swift Feature implementation