You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
Current or hourly weather conditions including temperature, wind, and humidity
Usage
Creating objects and encoding as JSON-LD
import Ontology
// Create a Person
varperson=Person()
person.givenName ="John"
person.familyName ="Doe"
person.email =["john.doe@example.com"]
// Create an organization
varorganization=Organization()
organization.name ="Example Corp"
// Associate person with organization
person.worksFor = organization
// Encode to JSON-LD
letencoder=JSONEncoder()letjsonData=try encoder.encode(person)print(String(data: jsonData, encoding:.utf8)!)
// Output:
// {
// "@context": "https://schema.org",
// "@type": "Person",
// "givenName": "John",
// "familyName": "Doe"
// }
Initializing from Apple framework types
import Ontology
import Contacts
// Convert from Apple's CNContact to Schema.org Person
letcontact=CNMutableContact()
contact.givenName ="Jane"
contact.familyName ="Smith"
contact.emailAddresses =[CNLabeledValue(label: CNLabelHome,
value:"jane.smith@example.com"asNSString)]
// Convert to Schema.org Person
letperson=Person(contact)
Configuring DateTime representations
By default, DateTime objects are encoded with their specified time zone,
or GMT/UTC if none is specified.
You can override the time zone used during encoding by providing
a specific TimeZone in the JSONEncoder's userInfo dictionary:
import Ontology
// Create a DateTime object
letdateTime=DateTime(Date())
// Create an encoder that will use the local timezone
letencoder=JSONEncoder()
encoder.userInfo[DateTime.timeZoneOverrideKey]=TimeZone.current
// Or specify a particular timezone
// encoder.userInfo[DateTime.timeZoneOverrideKey] = TimeZone(identifier: "America/New_York")
// Encode using the specified timezone
letjsonData=try encoder.encode(dateTime)
This feature is particularly useful when:
Working with date-only values that should be interpreted in the user's local timezone
Ensuring consistent timezone representation across different data sources
Presenting dates to users in their local timezone regardless of how they were originally stored
So to recap, the date encoding priority is:
TimeZone from encoder's userInfo (if provided)
TimeZone from the DateTime object (if specified)
GMT/UTC (default fallback)
Legal
Apple Weather and Weather are trademarks of Apple Inc.
This project is not affiliated with, endorsed, or sponsored by Apple Inc.
License
This project is licensed under the Apache License, Version 2.0.