https://github.com/kevin-lyn/ditto
Serialize swift object to JSON object.
https://github.com/kevin-lyn/ditto
ios json macos serialization
Last synced: 9 months ago
JSON representation
Serialize swift object to JSON object.
- Host: GitHub
- URL: https://github.com/kevin-lyn/ditto
- Owner: kevin-lyn
- License: mit
- Created: 2016-09-07T03:46:31.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2017-08-12T10:35:10.000Z (almost 9 years ago)
- Last Synced: 2025-07-26T21:29:44.794Z (11 months ago)
- Topics: ios, json, macos, serialization
- Language: Swift
- Size: 45.9 KB
- Stars: 6
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
#
Ditto     
Ditto allows you to serialize your swift object to JSON object compatible dictionary.
## Features
- Customizable mapping.
- Auto mapping with frequently used mapping style.
- Custom type convertible supported.
- Nested serializable.
## Requirements
- Xcode 8.0+
- Swift 3.0
## Usage
**CocoaPods**
```ruby
pod 'Ditto-Swift'
```
**Carthage**
```ruby
github "kevin0571/Ditto"
```
**Swift Package Manager**
```ruby
dependencies: [
.Package(url: "https://github.com/kevin0571/Ditto.git", majorVersion: 1)
]
```
### Overview
```swift
import Ditto
struct ExampleStruct {
let string = "string"
let anotherString = "anotherString"
let int = 1
let url = URL(string: "https://github.com")
}
extension ExampleStruct: Serializable {
func serializableMapping() -> Mapping {
return [
"string": "str",
"int": "integer",
"url": "url"
]
}
}
// Serialize ExampleStruct
let exampleStruct = ExampleStruct()
// To Dictionary
let jsonObject: JSONObject = exampleStruct.serialize()
let jsonArray: [JSONObject] = [exampleStruct, exampleStruct].serialize()
/*
"jsonObject" will be a dictionary with content:
[
"str": "string",
"integer": 1,
"url": "https://github.com"
]
note that "anotherString" is not being serialized,
becuase mapping of "anotherString" is not defined in "serializableMapping".
*/
// To String
let jsonObjectString: String = exampleStruct.serialize()
// To Data
let jsonObjectData: Data = exampleStruct.serialize()
```
### Auto Mapping
Available auto mapping styles: **lowercaseSeparatedByUnderScore**, **lowercase**, **lowerCamelCase**, **upperCamelCase**
```swift
extension ExampleStruct: Serializable {
func serializableMapping() -> Mapping {
return AutoMapping.mapping(
for: self,
style: .lowercaseSeparatedByUnderScore
)
}
}
// Serialize ExampleStruct with auto mapping
let exampleStruct = ExampleStruct()
let jsnObject = exampleStruct.serialize()
/*
"jsonObject" will be a dictionary with content:
[
"string": "string",
"another_string": "anotherString",
"int": 1,
"url": "https://github.com"
]
*/
```
### Custom Type Convertible
```swift
class CustomClass {
let string = "string"
let int = 1
private var converted: String {
return "Converted to: \(string), \(int)"
}
}
extension CustomClass: Convertible {
func convert() -> Any {
return converted
}
}
struct ExampleStruct {
let customClass = CustomClass()
}
extension ExampleStruct: Serializable {
func serializableMapping() -> Mapping {
return AutoMapping.mapping(
for: self,
style: .lowercaseSeparatedByUnderScore
)
}
}
// Serialize ExampleStruct
let exampleStruct = ExampleStruct()
let jsonObject = exampleStruct.serialize()
/*
"jsonObject" will be a dictionary with content:
[
"custom_class": "Converted to: string, int"
]
*/
```