Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lots to clean up but worth a look #14

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,4 @@ xcuserdata
# UNKNOWN: recommended by others, but I can't discover what these files are
#
# ...none. Everything is now explained.
Pods
19 changes: 19 additions & 0 deletions Podfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Uncomment this line to define a global platform for your project
platform :osx, '10.10'

target 'SwiftJson' do
pod 'ObjectMapper', '~> 0.11'
pod "SwiftyJSON", ">= 2.2"
pod 'Dollar'
pod 'Cent'
end

target 'SwiftJsonTests' do
pod 'ObjectMapper', '~> 0.11'
pod "SwiftyJSON", ">= 2.2"
pod 'Dollar'
pod 'Cent'
end

use_frameworks!

4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
##Swift JSON##
Swift JSON takes away the monotony of hand writing serialization code for JSON structures.

###Caveats###
* Swift JSON generates code that requires a forked version of [SwiftyJSON](https://github.com/pvwoods/SwiftyJSON) to be included in your project.

This project uses cocoa pod must be installed and run pods install on root project
217 changes: 196 additions & 21 deletions SwiftJson.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDESourceControlProjectFavoriteDictionaryKey</key>
<false/>
<key>IDESourceControlProjectIdentifier</key>
<string>12987391-4943-4B97-9A90-9E5DDF62779A</string>
<key>IDESourceControlProjectName</key>
<string>SwiftJson</string>
<key>IDESourceControlProjectOriginsDictionary</key>
<dict>
<key>957C2411939784F97902D82EAC722A5A882F3E35</key>
<string>https://github.com/kungfurorykelly/SwiftJson.git</string>
</dict>
<key>IDESourceControlProjectPath</key>
<string>SwiftJson.xcodeproj</string>
<key>IDESourceControlProjectRelativeInstallPathDictionary</key>
<dict>
<key>957C2411939784F97902D82EAC722A5A882F3E35</key>
<string>../..</string>
</dict>
<key>IDESourceControlProjectURL</key>
<string>https://github.com/kungfurorykelly/SwiftJson.git</string>
<key>IDESourceControlProjectVersion</key>
<integer>111</integer>
<key>IDESourceControlProjectWCCIdentifier</key>
<string>957C2411939784F97902D82EAC722A5A882F3E35</string>
<key>IDESourceControlProjectWCConfigurations</key>
<array>
<dict>
<key>IDESourceControlRepositoryExtensionIdentifierKey</key>
<string>public.vcs.git</string>
<key>IDESourceControlWCCIdentifierKey</key>
<string>957C2411939784F97902D82EAC722A5A882F3E35</string>
<key>IDESourceControlWCCName</key>
<string>SwiftJson</string>
</dict>
</array>
</dict>
</plist>
10 changes: 10 additions & 0 deletions SwiftJson.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions SwiftJson.xcworkspace/xcshareddata/SwiftJson.xccheckout
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDESourceControlProjectFavoriteDictionaryKey</key>
<false/>
<key>IDESourceControlProjectIdentifier</key>
<string>BCAF5E1C-AB78-4B1E-A440-02B5C252440C</string>
<key>IDESourceControlProjectName</key>
<string>SwiftJson</string>
<key>IDESourceControlProjectOriginsDictionary</key>
<dict>
<key>957C2411939784F97902D82EAC722A5A882F3E35</key>
<string>https://github.com/kungfurorykelly/SwiftJson.git</string>
</dict>
<key>IDESourceControlProjectPath</key>
<string>SwiftJson.xcworkspace</string>
<key>IDESourceControlProjectRelativeInstallPathDictionary</key>
<dict>
<key>957C2411939784F97902D82EAC722A5A882F3E35</key>
<string>..</string>
</dict>
<key>IDESourceControlProjectURL</key>
<string>https://github.com/kungfurorykelly/SwiftJson.git</string>
<key>IDESourceControlProjectVersion</key>
<integer>111</integer>
<key>IDESourceControlProjectWCCIdentifier</key>
<string>957C2411939784F97902D82EAC722A5A882F3E35</string>
<key>IDESourceControlProjectWCConfigurations</key>
<array>
<dict>
<key>IDESourceControlRepositoryExtensionIdentifierKey</key>
<string>public.vcs.git</string>
<key>IDESourceControlWCCIdentifierKey</key>
<string>957C2411939784F97902D82EAC722A5A882F3E35</string>
<key>IDESourceControlWCCName</key>
<string>SwiftJson</string>
</dict>
</array>
</dict>
</plist>
89 changes: 89 additions & 0 deletions SwiftJson/ClassModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
//
// ClassModel.swift
// SwiftJson
//
// Created by RORY KELLY on 05/05/2015.
// Copyright (c) 2015 pvwoods. All rights reserved.
//

import Foundation
import SwiftyJSON

/**
Model to hold definition of classes
*/
struct ClassModel {
static let CLASS_PLACEHOLDER = "*CLASS*"
static let VARIABLES_PLACEHOLDER = "*VARIABLES*"
static let MAPPING_PLACEHOLDER = "*MAPPINGS*"
static let classStructure =
"\nclass *CLASS* : Mappable { " +
"\n *VARIABLES*" +
"\n required init?(_ map: Map){" +
"\n mapping(map)" +
"\n }\n" +
"\n func mapping(map: Map) {" +
"\n *MAPPINGS*" +
"\n }" +
" \n}"

let className:String
let variables:[String:String]

init(name:String, objectDictionary:[String:JSON]){
className = name;
variables = ClassModel.getTypes(objectDictionary)
}

func generateDescription() -> String {
var variablesString = ""
var mappingString = ""
for(name, type) in variables {
variablesString = variablesString.stringByAppendingString("var " + name + ":" + type + "? \n ")
mappingString = mappingString.stringByAppendingString(name + " <- map[\"" + name + "\"] \n ")
}
return ClassModel.classStructure
.stringByReplacingOccurrencesOfString(ClassModel.CLASS_PLACEHOLDER, withString: className)
.stringByReplacingOccurrencesOfString(ClassModel.VARIABLES_PLACEHOLDER, withString: variablesString)
.stringByReplacingOccurrencesOfString(ClassModel.MAPPING_PLACEHOLDER, withString: mappingString)
}

/**
get the types associated with a dictionary.
:param: dictionary of types in json
:returns: map [name:type]
*/
static func getTypes(objectDictionary:[String:JSON]) -> [String:String]{
var variables:[String:String] = [:]
// save all of the types
for(variableName, json) in objectDictionary {
// variable names are guaranteed to be unique at this point.
// save a map of object type to name.
variables[variableName] = ClassModel.getType(variableName, jsonObject: json)
}
return variables
}

/**
get the type of a single JSON pairing
:param: key for json object.
:param: json object to get types for.
:returns: a string representing the type for a type.
*/
static func getType(key:String, jsonObject:JSON) -> String {
switch (jsonObject.type) {
case .String:
return "String"
case .Number:
return "Double"
case .Bool:
return "Bool"
case .Array:
return "[" + getType(key, jsonObject: jsonObject[0]) + "]"
case .Dictionary:
return key
default:
return "AnyObject"
}
}
}
64 changes: 64 additions & 0 deletions SwiftJson/Generator.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//
// Generator.swift
// SwiftJson
//
// Created by RORY KELLY on 09/05/2015.
// Copyright (c) 2015 pvwoods. All rights reserved.
//

import Foundation
import SwiftyJSON

class Generator {

private let rootObject:JSON

init(jsonObject:JSON){
rootObject = jsonObject
}

static func getAllDictionaries(object:JSON, function:(String,[String : JSON]) -> ()) {
for (key: String, subJson: JSON) in object {
if (subJson.type == .Dictionary) {
function(key, subJson.dictionaryValue)
getAllDictionaries(subJson, function: function);
} else if (subJson.type == .Array) {
if(subJson[0].type == .Dictionary){
function(key, subJson[0].dictionaryValue)
getAllDictionaries(subJson[0], function: function)
}
}
}
}

func getAllClassModels() -> [ClassModel] {
var classModels:[ClassModel] = []
if (rootObject.type == .Dictionary) {
classModels.append(ClassModel(name: "rootClass", objectDictionary: rootObject.dictionaryValue))
} else if (rootObject.type == .Array) {
if(rootObject[0].type == .Dictionary){
Generator.getAllDictionaries(rootObject, function: { (name, dictionary) -> () in
classModels.append(ClassModel(name: name, objectDictionary: dictionary))
})
return classModels
}
}

Generator.getAllDictionaries(rootObject, function: { (name, dictionary) -> () in
classModels.append(ClassModel(name: name, objectDictionary: dictionary))
})
return classModels
}

func getFinalOutput() -> String {
let classModels = getAllClassModels()
var finalOutput = ""
for classModel in classModels {
finalOutput = finalOutput.stringByAppendingString(classModel.generateDescription())
}
return finalOutput
}



}
109 changes: 0 additions & 109 deletions SwiftJson/IndentableOutput.swift

This file was deleted.

Loading