A Swift library for working with structured data. This library provides JSON-LD serializable types that can represent entities from various vocabularies, with a focus on Schema.org. It includes convenience initializers for types from Apple frameworks, like Contacts and EventKit.
- Swift 6.0+ / Xcode 16+
- macOS 14.0+ (Sonoma)
- iOS 17.0+
Add the following to your Package.swift
file:
dependencies: [
.package(url: "https://github.com/loopwork-ai/ontology.git", from: "0.2.0")
]
Supported Schema.org types and their Apple framework equivalents:
Schema.org Type | Apple Framework Type | Description |
---|---|---|
ContactPoint | CNInstantMessageAddress | Represents a method of contact like instant messaging |
DateTime | Date | Represents a date and time with ISO 8601 formatting |
Event | EKEvent | Represents an event with start/end dates, location, etc. |
Organization | CNContact | Represents an organization with properties like name and contact info |
Person | CNContact | Represents a person with properties like name, contact info, and relationships |
PlanAction | EKReminder | Represents a planned action or task with properties like name, description, due date, and completion status |
PostalAddress | CNPostalAddress | Represents a physical address with street, city, region, etc. |
Additional types supporting the National Weather Service API:
Weather.gov Type | Description |
---|---|
WeatherForecast | Represents detailed weather forecast data including temperature, precipitation probability, and wind information |
import Ontology
// Create a Person
var person = Person()
person.givenName = "John"
person.familyName = "Doe"
person.email = ["john.doe@example.com"]
// Create an organization
var organization = Organization()
organization.name = "Example Corp"
// Associate person with organization
person.worksFor = organization
// Encode to JSON-LD
let encoder = JSONEncoder()
let jsonData = try encoder.encode(person)
print(String(data: jsonData, encoding: .utf8)!)
// Output:
// {
// "@context": "https://schema.org",
// "@type": "Person",
// "givenName": "John",
// "familyName": "Doe"
// }
import Ontology
import Contacts
// Convert from Apple's CNContact to Schema.org Person
let contact = CNMutableContact()
contact.givenName = "Jane"
contact.familyName = "Smith"
contact.emailAddresses = [
CNLabeledValue(label: CNLabelHome,
value: "jane.smith@example.com" as NSString)
]
// Convert to Schema.org Person
let person = Person(contact)
This project is licensed under the Apache License, Version 2.0.