Giter VIP home page Giter VIP logo

realmswift-json's Introduction

RealmSwift-JSON

Easy way to transform library between RealmSwift Object and JSON like Mantle

Installation

just copy files in the RealmSwift-JSON folder into your project and add the under code into objective-c bridging-header

#import <Realm/RLMProperty.h>
#import <Realm/RLMObjectSchema.h>
#import <Realm/RLMObject_Private.h>
#import <Realm/Realm.h>

Using RealmSwift-JSON

You can specify the inbound and outbound JSON mapping with your model.

class card:Object {
    dynamic var id = "" 
    dynamic var title:String?
    let persons = List<Person>()

    class func JSONInboundMappingDictionary() -> [String:String] {
        return [
            "id": "id",
            "title": "title",
            "card_persons":"persons"
        ]
    }
}

class person:Object {
    dynamic var id = ""
    dynamic var name = "" 
    
    class func JSONOutboundMappingDictionary() -> [String:String] {
        return [
            "id": "id",
            "person_name": "name"
        ]
    }
}

It works as follows:

let person = Person(jsonDictionary:["id":"0", "person_name":"henry"])
Realm().create(card.self, jsonDictionary:["id":"0", "title":"Seoul", card_persons:[["id":"0", "name":"person1"]] ], update: true)
print(person.JSONDictionary()) 
// ["id":"0", "name":"henry"]

You can also specify custom NSValueTransformer like Mantle way as follows:

class category:Object {
    let addesses = List<CSRString>()
    
    class func addressesJSONTransformer() -> NSValueTransformer {
        return CSRStringJSONTransformer()
    }
}

class CSRString:Object {
    dynamic var stringValue = "" 
}

class CSRStringJSONTransformer: NSValueTransformer {
    override func transformedValue(value: AnyObject?) -> AnyObject? {
        let rString = List<CSRString>()
        if let value = value as? [String] {
            for string in value {
                let str = CSRString()
                str.stringValue = string
                rString.append(str)
            }
            return rString
        } else {
            return super.transformedValue(value)
        }
    }
    override func reverseTransformedValue(value: AnyObject?) -> AnyObject? {
        if let stringArray = value as? List<CSRString> {
            var strings = [String]()
            for rString in stringArray {
                strings.append(rString.stringValue)
            }
            return strings.count > 0 ? strings : nil
        } else {
            return super.reverseTransformedValue(value)
        }
    }
}

License

RealmSwift+JSON is under the MIT license.

realmswift-json's People

Contributors

changsung avatar

Watchers

Julian Hays avatar James Cloos avatar  avatar

Forkers

hackdyourshit

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.