All Projects → IvanRublev → Persistentstorageserializable

IvanRublev / Persistentstorageserializable

Licence: mit
Swift library that makes easier to serialize the user's preferences (app's settings) with system User Defaults or Property List file on disk.

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Persistentstorageserializable

Hprose Java
Hprose is a cross-language RPC. This project is Hprose 2.0 for Java
Stars: ✭ 542 (+234.57%)
Mutual labels:  serialization-library, serialization
Succ
Sexy and Utilitarian Code Configuration
Stars: ✭ 100 (-38.27%)
Mutual labels:  serialization-library, serialization
Prephirences
Prephirences is a Swift library that provides useful protocols and convenience methods to manage application preferences, configurations and app-state. UserDefaults
Stars: ✭ 548 (+238.27%)
Mutual labels:  userdefaults, plist
Flatbuffers
FlatBuffers: Memory Efficient Serialization Library
Stars: ✭ 17,180 (+10504.94%)
Mutual labels:  serialization-library, serialization
Hxbaseprojectdemo
一个项目的基类工程
Stars: ✭ 126 (-22.22%)
Mutual labels:  userdefaults, plist
Hprose Nodejs
Hprose is a cross-language RPC. This project is Hprose 2.0 for Node.js
Stars: ✭ 297 (+83.33%)
Mutual labels:  serialization-library, serialization
Hprose Golang
Hprose is a cross-language RPC. This project is Hprose for Golang.
Stars: ✭ 1,143 (+605.56%)
Mutual labels:  serialization-library, serialization
JsonFormatter
Easy, Fast and Lightweight Json Formatter. (Serializer and Deserializer)
Stars: ✭ 26 (-83.95%)
Mutual labels:  serialization, serialization-library
Yaxlib
Yet Another XML Serialization Library for the .NET Framework and .NET Core
Stars: ✭ 124 (-23.46%)
Mutual labels:  serialization-library, serialization
Lora Serialization
LoraWAN serialization/deserialization library for The Things Network
Stars: ✭ 120 (-25.93%)
Mutual labels:  serialization-library, serialization
Savegamefree
Save Game Free is a free and simple but powerful solution for saving and loading game data in unity.
Stars: ✭ 279 (+72.22%)
Mutual labels:  serialization-library, serialization
Hprose Js
Hprose is a cross-language RPC. This project is Hprose 2.0 RPC for JavaScript
Stars: ✭ 133 (-17.9%)
Mutual labels:  serialization-library, serialization
fuser
Header-only library for automatic (de)serialization of C++ types to/from JSON.
Stars: ✭ 48 (-70.37%)
Mutual labels:  serialization, serialization-library
Typedefs
Programming language agnostic type construction language based on polynomials.
Stars: ✭ 337 (+108.02%)
Mutual labels:  serialization-library, serialization
BinaryLove3
Simple C++ 20 Serialization Library that works out of the box with aggregate types!
Stars: ✭ 13 (-91.98%)
Mutual labels:  serialization, serialization-library
Eminim
JSON serialization framework for Nim, works from a Stream directly to any type and back. Depends only on stdlib.
Stars: ✭ 32 (-80.25%)
Mutual labels:  serialization-library, serialization
EndianBinaryIO
A C# library that can read and write primitives, enums, arrays, and strings to streams and byte arrays with specified endianness, string encoding, and boolean sizes.
Stars: ✭ 20 (-87.65%)
Mutual labels:  serialization, serialization-library
ikeapack
Compact data serializer/packer written in Go, intended to produce a cross-language usable format.
Stars: ✭ 18 (-88.89%)
Mutual labels:  serialization, serialization-library
Hprose Delphi
Hprose is a cross-language RPC. This project is Hprose 2.0 for Delphi and FreePascal
Stars: ✭ 100 (-38.27%)
Mutual labels:  serialization-library, serialization
Json
Lighter and Faster Json Serialization tool.
Stars: ✭ 128 (-20.99%)
Mutual labels:  serialization-library, serialization

PersistentStorageSerializable

CI Status Carthage compatible Version Swift License

PersistentStorageSerializable is a protocol for automatic serialization and deserialization of Swift class, struct or NSObject descendant object into and from User Defaults or Property List file.

The adopting type properties must be of property list type (String, Data, Date, Int, UInt, Float, Double, Bool, Array or Dictionary of above). If you want to store any other type of object, you should typically archive it to create an instance of Data. The URL properties can be stored in User Defaults storage but not in Plist storage. In the last case, you have to archive it to/from Data.

The PersistentStorageSerializable protocol provides default implementations of init(from:) initializer and persist() function. The library defines two classes of PesistentStorage protocol: UserDefaultsStorage and PlistStorage. Object of one of those types is passed as the argument when calling to init(from:) initializer to specify which storage to be used for serialization/deserialization.

Functions of the PersistentStorageSerializable protocol traverses the adopting type object and gets/sets it's properties values via Reflection library. The NSObject class descendant properties values are get/set via KVC.

How to use

Serialize/Deserialize a struct with User Defaults by using UserDefaultStorage as shown below:

struct Settings: PersistentStorageSerializable {
    var flag = false
    var title = ""
    var number = 1
    var dictionary: [String : Any] = ["Number" : 1, "Distance" : 25.4, "Label" : "Hello"]

    // MARK: Adopt PersistentStorageSerializable
    var persistentStorage: PersistentStorage!
    var persistentStorageKeyPrefix: String! = "Settings"
}

// Init from User Defaults
var mySettings = try! Settings(from: UserDefaultsStorage.standard)

mySettings.flag = true

// Persist into User Defaults
try! mySettings.persist()

To serialize data with Plist file use PlistStorage class:

// Init from plist
let plistUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last!.appendingPathComponent("storage.plist")

var settingsOnDisk = try! Settings(from: PlistStorage(at: plistUrl))

mySettings.flag = true

// Persist on disk
try! mySettings.persist()

Reading data stored by the previous version of the app

When you have some data persisted in User Defaults by the previous version of the app and want to read that data into a structure you need to provide a mapping between properties names and User Defaults keys by overloading the persistentStorageKey(for:) function.

Say we have following data persisted in User Defaults:

UserDefaults.standard.set("Superhero", forKey: "oldGoogTitle")
UserDefaults.standard.set(true, forKey: "well.persisted.option")

We want those to be serialized with the object of ApplicationConfiguration class.

final class ApplicationConfiguration: PersistentStorageSerializable {
    var title = ""
    var showIntro = false

    // MARK: Adopt PersistentStorageSerializable
    var persistentStorage: PersistentStorage!
    var persistentStorageKeyPrefix: String!
}

// Provide key mapping by overloading `persistentStorageKey(for:)` function.
extension ApplicationConfiguration {
    func persistentStorageKey(for propertyName: String) -> String {
        let keyMap = ["title" : "oldGoogTitle", "showIntro" : "well.persisted.option"]
        return keyMap[propertyName]!
    }
}

// Now we can load data persisted in the storage.
let configuration = try! ApplicationConfiguration(from: UserDefaultsStorage.standard)

print(configuration.title) // prints Superhero
print(configuration.showIntro) // prints true

Example

To run example projects for iOS/macOS, run pod try PersistentStorageSerializable in the terminal.

Requirements

  • Xcode 8.3
  • Swift 3.1

Installation

CocoaPods

PersistentStorageSerializable is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "PersistentStorageSerializable"

and run pods update or pods install.

✨ For Xcode 9, Swift 4 and cocoapods installation, please follow the instruction here.

Carthage

If you use Carthage to manage your dependencies, simply add PersistentStorageSerializable to your Cartfile:

github "IvanRublev/PersistentStorageSerializable"

If you use Carthage to build your dependencies, make sure you have added PersistentStorageSerializable.framework and Reflection.framework to the "Linked Frameworks and Libraries" section of your target, and have included them in your Carthage framework copying build phase.

Author

Copyright (c) 2017, IvanRublev, [email protected]

License

PersistentStorageSerializable is available under the MIT license. See the LICENSE file for more info.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].