Skip to content

Commit 1ae5a01

Browse files
author
Karthikeya Udupa KM
authored
Merge pull request #49 from insanoid/releases/v1.2.0
Releases/v1.2.0
2 parents 92384b9 + 7d0fbc0 commit 1ae5a01

File tree

16 files changed

+603
-373
lines changed

16 files changed

+603
-373
lines changed

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
[![Build
66
Status](https://travis-ci.org/insanoid/SwiftyJSONAccelerator.svg?branch=master)](https://travis-ci.org/insanoid/SwiftyJSONAccelerator) [![codecov](https://codecov.io/gh/insanoid/SwiftyJSONAccelerator/branch/master/graph/badge.svg)](https://codecov.io/gh/insanoid/SwiftyJSONAccelerator)
77

8+
**Version v1.2.0 Released!**
9+
10+
- Now supports [Marshal](https://github.com/utahiosmac/Marshal)! One of the fastest JSONSerialisation class out there! [(Read more)](https://github.com/bwhiteley/JSONShootout)
11+
- Set `class` as `final`.
12+
- `init` marked as `required` by default for `class`.
13+
814
**Version v1.1.0 Released!**
915

1016
- Now generates the correct option `struct` and `class` based on what was selected.
@@ -18,15 +24,16 @@ Status](https://travis-ci.org/insanoid/SwiftyJSONAccelerator.svg?branch=master)]
1824

1925
## Download/Installing
2026

21-
- Download the repo, install pods and run the project!
22-
- [Download the .app(v1.1.0)](https://github.com/insanoid/SwiftyJSONAccelerator/releases/download/v1.1.0/SwiftyJSONAccelerator.zip).0/SwiftyJSONAccelerator.zip)
27+
- **Option 1:** Download the repo, install pods and run the project!
28+
- **Option 2:** [Download the .app(v1.2.0)](https://github.com/insanoid/SwiftyJSONAccelerator/releases/download/v1.2.0/SwiftyJSONAccelerator.zip)
2329

2430
A swift model generator like the Objective-C [JSONAccelerator](http://nerdery.com/json-accelerator). Formats and generates models for the given JSON and also breaks them into files making it easy to manage and share between several models.
2531

2632
The models that are generated depend on JSON object mapping libraries, currently the model can be generated to depend on any of the below mentioned mapping libraries:
2733

2834
- [SwiftyJSON](https://github.com/SwiftyJSON/SwiftyJSON)
2935
- [Hearst-DD/ObjectMapper](https://github.com/Hearst-DD/ObjectMapper)
36+
- [Marshal](https://github.com/utahiosmac/Marshal)
3037

3138
Currently, the pattern is very similar to its Objective-C counterpart. It generates classes with following properties.
3239

SwiftyJSONAccelerator.xcodeproj/project.pbxproj

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
930462B51D31131800FE61A6 /* PropertyComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 930462B11D310FF200FE61A6 /* PropertyComponent.swift */; };
1616
930462B61D31133B00FE61A6 /* FileGenerator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 930462AC1D3106A400FE61A6 /* FileGenerator.swift */; };
1717
930462B71D31134900FE61A6 /* ModelComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 930462AF1D310F6900FE61A6 /* ModelComponent.swift */; };
18+
9327F2881DF4BCA70002BE40 /* MarshalModelFile.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9327F2871DF4BCA70002BE40 /* MarshalModelFile.swift */; };
19+
9327F28A1DF4BD540002BE40 /* MarshalTemplate.txt in Resources */ = {isa = PBXBuildFile; fileRef = 9327F2891DF4BD540002BE40 /* MarshalTemplate.txt */; };
20+
9327F2971DF4CF650002BE40 /* MarshalModelFile.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9327F2871DF4BCA70002BE40 /* MarshalModelFile.swift */; };
1821
9341A14A1BD6E5FE0048CE2C /* ModelGenerator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9341A1491BD6E5FE0048CE2C /* ModelGenerator.swift */; };
1922
9341A14F1BD6E7290048CE2C /* BaseTemplate.txt in Resources */ = {isa = PBXBuildFile; fileRef = 9341A14E1BD6E7290048CE2C /* BaseTemplate.txt */; };
2023
9361FD141BD18558001ED533 /* LineNumberRulerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9361FD131BD18558001ED533 /* LineNumberRulerView.swift */; };
@@ -79,6 +82,8 @@
7982
930462AC1D3106A400FE61A6 /* FileGenerator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FileGenerator.swift; sourceTree = "<group>"; };
8083
930462AF1D310F6900FE61A6 /* ModelComponent.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ModelComponent.swift; sourceTree = "<group>"; };
8184
930462B11D310FF200FE61A6 /* PropertyComponent.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PropertyComponent.swift; sourceTree = "<group>"; };
85+
9327F2871DF4BCA70002BE40 /* MarshalModelFile.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MarshalModelFile.swift; sourceTree = "<group>"; };
86+
9327F2891DF4BD540002BE40 /* MarshalTemplate.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = MarshalTemplate.txt; sourceTree = "<group>"; };
8287
9341A1491BD6E5FE0048CE2C /* ModelGenerator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ModelGenerator.swift; sourceTree = "<group>"; };
8388
9341A14E1BD6E7290048CE2C /* BaseTemplate.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = BaseTemplate.txt; sourceTree = "<group>"; };
8489
9361FD131BD18558001ED533 /* LineNumberRulerView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LineNumberRulerView.swift; sourceTree = "<group>"; };
@@ -211,6 +216,7 @@
211216
children = (
212217
93F1593F1CFF96E700175636 /* ObjectMapperModelFile.swift */,
213218
93F1593D1CFF969900175636 /* SwiftyJSONModelFile.swift */,
219+
9327F2871DF4BCA70002BE40 /* MarshalModelFile.swift */,
214220
);
215221
path = "Library-Extensions";
216222
sourceTree = "<group>";
@@ -291,6 +297,7 @@
291297
children = (
292298
C667011C1BDCAF23009BA254 /* ObjectMapperTemplate.txt */,
293299
C667011A1BDCABCE009BA254 /* SwiftyJSONTemplate.txt */,
300+
9327F2891DF4BD540002BE40 /* MarshalTemplate.txt */,
294301
);
295302
path = "Library-Extension-Templates";
296303
sourceTree = "<group>";
@@ -369,7 +376,7 @@
369376
93F174371BD0707D007E7DFC /* Project object */ = {
370377
isa = PBXProject;
371378
attributes = {
372-
LastUpgradeCheck = 0800;
379+
LastUpgradeCheck = 0810;
373380
ORGANIZATIONNAME = "Karthikeya Udupa K M";
374381
TargetAttributes = {
375382
93F1743E1BD0707D007E7DFC = {
@@ -414,6 +421,7 @@
414421
C667011D1BDCAF23009BA254 /* ObjectMapperTemplate.txt in Resources */,
415422
9341A14F1BD6E7290048CE2C /* BaseTemplate.txt in Resources */,
416423
93F1744A1BD0707D007E7DFC /* Main.storyboard in Resources */,
424+
9327F28A1DF4BD540002BE40 /* MarshalTemplate.txt in Resources */,
417425
);
418426
runOnlyForDeploymentPostprocessing = 0;
419427
};
@@ -549,6 +557,7 @@
549557
9341A14A1BD6E5FE0048CE2C /* ModelGenerator.swift in Sources */,
550558
930462B21D310FF200FE61A6 /* PropertyComponent.swift in Sources */,
551559
93F1593A1CFF853C00175636 /* NameGenerator.swift in Sources */,
560+
9327F2881DF4BCA70002BE40 /* MarshalModelFile.swift in Sources */,
552561
930462AD1D3106A400FE61A6 /* FileGenerator.swift in Sources */,
553562
93F174451BD0707D007E7DFC /* SJEditorViewController.swift in Sources */,
554563
93F1593E1CFF969900175636 /* SwiftyJSONModelFile.swift in Sources */,
@@ -578,6 +587,7 @@
578587
93D850EF1D00D3070023D573 /* ObjectMapperModelFile.swift in Sources */,
579588
93D850DC1D00D2ED0023D573 /* SJEditorViewController.swift in Sources */,
580589
93D850D81D00D2030023D573 /* ModelGenerator.swift in Sources */,
590+
9327F2971DF4CF650002BE40 /* MarshalModelFile.swift in Sources */,
581591
93D850E91D00D3020023D573 /* String+Helpers.swift in Sources */,
582592
9362F8661BD07DBA0001372E /* JSONHelperTests.swift in Sources */,
583593
93D850DE1D00D2F00023D573 /* SJTextView.swift in Sources */,

SwiftyJSONAccelerator.xcodeproj/xcshareddata/xcschemes/SwiftyJSONAccelerator.xcscheme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "0800"
3+
LastUpgradeVersion = "0810"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"

SwiftyJSONAccelerator/Constants.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,12 @@ enum ConstructType: String {
4646

4747
- SwiftyJSON: SwiftyJSON - https://github.com/SwiftyJSON/SwiftyJSON
4848
- ObjectMapper: ObjectMapper - https://github.com/Hearst-DD/ObjectMapper
49+
- Marshal: Marshal - https://github.com/utahiosmac/Marshal
4950
*/
5051
enum JSONMappingLibrary: String {
5152
case SwiftyJSON
5253
case ObjectMapper
54+
case Marshal
5355
}
5456

5557
/**

SwiftyJSONAccelerator/Generators/FileGenerator.swift

Lines changed: 95 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -10,85 +10,97 @@ import Foundation
1010

1111
struct FileGenerator {
1212

13-
/**
13+
/**
1414
Fetch the template for creating model.swift files.
1515

1616
- parameter filename: Name of the file to be loaded
1717

1818
- returns: String containing the template.
1919
*/
20-
static func loadFileWith(_ filename: String) -> String {
20+
static func loadFileWith(_ filename: String) -> String {
2121

22-
let bundle = Bundle.main
23-
let path = bundle.path(forResource: filename, ofType: "txt")
22+
let bundle = Bundle.main
23+
let path = bundle.path(forResource: filename, ofType: "txt")
2424

25-
do {
26-
let content = try String.init(contentsOfFile: path!)
27-
return content
28-
} catch { }
25+
do {
26+
let content = try String.init(contentsOfFile: path!)
27+
return content
28+
} catch { }
2929

30-
return ""
31-
}
32-
33-
static func generateFileContentWith(_ modelFile: ModelFile, configuration: ModelGenerationConfiguration) -> String {
34-
35-
var content = loadFileWith("BaseTemplate")
36-
content = content.replacingOccurrences(of: "{OBJECT_NAME}", with: modelFile.fileName)
37-
content = content.replacingOccurrences(of: "{DATE}", with: todayDateString())
38-
content = content.replacingOccurrences(of: "{OBJECT_KIND}", with: modelFile.type.rawValue)
39-
content = content.replacingOccurrences(of: "{JSON_PARSER_LIBRARY_BODY}", with: loadFileWith(modelFile.mainBodyFileName()))
40-
if let authorName = configuration.authorName {
41-
content = content.replacingOccurrences(of: "__NAME__", with: authorName)
42-
}
43-
if let companyName = configuration.companyName {
44-
content = content.replacingOccurrences(of: "__MyCompanyName__", with: companyName)
45-
}
46-
content = content.replacingOccurrences(of: "{INCLUDE_HEADER}", with: "\nimport \(modelFile.moduleName())")
47-
48-
var classesExtendFrom: [String] = []
49-
if let extendFrom = modelFile.baseElementName() {
50-
classesExtendFrom = [extendFrom]
51-
}
52-
if configuration.supportNSCoding && configuration.constructType == .ClassType {
53-
classesExtendFrom = classesExtendFrom + ["NSCoding"]
54-
}
55-
56-
if classesExtendFrom.count > 0 {
57-
content = content.replacingOccurrences(of: "{EXTEND_FROM}", with: classesExtendFrom.joined(separator: ", "))
58-
content = content.replacingOccurrences(of: "{EXTENDED_OBJECT_COLON}", with: ": ")
59-
} else {
60-
content = content.replacingOccurrences(of: "{EXTEND_FROM}", with: "")
61-
content = content.replacingOccurrences(of: "{EXTENDED_OBJECT_COLON}", with: "")
30+
return ""
6231
}
6332

64-
let stringConstants = modelFile.component.stringConstants.map({ " " + $0 }).joined(separator: "\n")
65-
let declarations = modelFile.component.declarations.map({ " " + $0 }).joined(separator: "\n")
66-
let initialisers = modelFile.component.initialisers.map({ " " + $0 }).joined(separator: "\n")
67-
let description = modelFile.component.description.map({ " " + $0 }).joined(separator: "\n")
68-
69-
content = content.replacingOccurrences(of: "{STRING_CONSTANT}", with: stringConstants)
70-
content = content.replacingOccurrences(of: "{DECLARATION}", with: declarations)
71-
content = content.replacingOccurrences(of: "{INITALIZER}", with: initialisers)
72-
content = content.replacingOccurrences(of: "{DESCRIPTION}", with: description)
73-
74-
if configuration.constructType == .StructType {
75-
content = content.replacingOccurrences(of: " convenience", with: "")
33+
static func generateFileContentWith(_ modelFile: ModelFile, configuration: ModelGenerationConfiguration) -> String {
34+
35+
var content = loadFileWith("BaseTemplate")
36+
content = content.replacingOccurrences(of: "{OBJECT_NAME}", with: modelFile.fileName)
37+
content = content.replacingOccurrences(of: "{DATE}", with: todayDateString())
38+
content = content.replacingOccurrences(of: "{OBJECT_KIND}", with: modelFile.type.rawValue)
39+
content = content.replacingOccurrences(of: "{JSON_PARSER_LIBRARY_BODY}", with: loadFileWith(modelFile.mainBodyFileName()))
40+
41+
if modelFile.type == .ClassType {
42+
content = content.replacingOccurrences(of: "{REQUIRED}", with: " required ")
43+
} else {
44+
content = content.replacingOccurrences(of: "{REQUIRED}", with: " ")
45+
}
46+
if let authorName = configuration.authorName {
47+
content = content.replacingOccurrences(of: "__NAME__", with: authorName)
48+
}
49+
if let companyName = configuration.companyName {
50+
content = content.replacingOccurrences(of: "__MyCompanyName__", with: companyName)
51+
}
52+
content = content.replacingOccurrences(of: "{INCLUDE_HEADER}", with: "\nimport \(modelFile.moduleName())")
53+
54+
var classesExtendFrom: [String] = []
55+
if let extendFrom = modelFile.baseElementName() {
56+
classesExtendFrom = [extendFrom]
57+
}
58+
if configuration.supportNSCoding && configuration.constructType == .ClassType {
59+
classesExtendFrom = classesExtendFrom + ["NSCoding"]
60+
}
61+
62+
if configuration.isFinalRequired && configuration.constructType == .ClassType {
63+
content = content.replacingOccurrences(of: "{IS_FINAL}", with: " final ")
64+
} else {
65+
content = content.replacingOccurrences(of: "{IS_FINAL}", with: " ")
66+
}
67+
68+
if classesExtendFrom.count > 0 {
69+
content = content.replacingOccurrences(of: "{EXTEND_FROM}", with: classesExtendFrom.joined(separator: ", "))
70+
content = content.replacingOccurrences(of: "{EXTENDED_OBJECT_COLON}", with: ": ")
71+
} else {
72+
content = content.replacingOccurrences(of: "{EXTEND_FROM}", with: "")
73+
content = content.replacingOccurrences(of: "{EXTENDED_OBJECT_COLON}", with: "")
74+
}
75+
76+
let stringConstants = modelFile.component.stringConstants.map({ " " + $0 }).joined(separator: "\n")
77+
let declarations = modelFile.component.declarations.map({ " " + $0 }).joined(separator: "\n")
78+
let initialisers = modelFile.component.initialisers.map({ " " + $0 }).joined(separator: "\n")
79+
let description = modelFile.component.description.map({ " " + $0 }).joined(separator: "\n")
80+
81+
content = content.replacingOccurrences(of: "{STRING_CONSTANT}", with: stringConstants)
82+
content = content.replacingOccurrences(of: "{DECLARATION}", with: declarations)
83+
content = content.replacingOccurrences(of: "{INITIALIZER}", with: initialisers)
84+
content = content.replacingOccurrences(of: "{DESCRIPTION}", with: description)
85+
86+
if configuration.constructType == .StructType {
87+
content = content.replacingOccurrences(of: " convenience", with: "")
88+
}
89+
90+
if configuration.supportNSCoding && configuration.constructType == .ClassType {
91+
content = content.replacingOccurrences(of: "{NSCODING_SUPPORT}", with: loadFileWith("NSCodingTemplate"))
92+
let encoders = modelFile.component.encoders.map({ " " + $0 }).joined(separator: "\n")
93+
let decoders = modelFile.component.decoders.map({ " " + $0 }).joined(separator: "\n")
94+
content = content.replacingOccurrences(of: "{DECODERS}", with: decoders)
95+
content = content.replacingOccurrences(of: "{ENCODERS}", with: encoders)
96+
} else {
97+
content = content.replacingOccurrences(of: "{NSCODING_SUPPORT}", with: "")
98+
}
99+
100+
return content
76101
}
77102

78-
if configuration.supportNSCoding && configuration.constructType == .ClassType {
79-
content = content.replacingOccurrences(of: "{NSCODING_SUPPORT}", with: loadFileWith("NSCodingTemplate"))
80-
let encoders = modelFile.component.encoders.map({ " " + $0 }).joined(separator: "\n")
81-
let decoders = modelFile.component.decoders.map({ " " + $0 }).joined(separator: "\n")
82-
content = content.replacingOccurrences(of: "{DECODERS}", with: decoders)
83-
content = content.replacingOccurrences(of: "{ENCODERS}", with: encoders)
84-
} else {
85-
content = content.replacingOccurrences(of: "{NSCODING_SUPPORT}", with: "")
86-
}
87-
88-
return content
89-
}
90-
91-
/**
103+
/**
92104
Write the given content to a file at the mentioned path.
93105

94106
- parameter name: The name of the file.
@@ -97,24 +109,24 @@ struct FileGenerator {
97109

98110
- returns: Boolean indicating if the process was successful.
99111
*/
100-
static internal func writeToFileWith(_ name: String, content: String, path: String) -> Bool {
101-
let filename = path.appendingFormat("%@", (name + ".swift"))
102-
do {
103-
try FileManager.default.createDirectory(at: URL.init(fileURLWithPath: path),
104-
withIntermediateDirectories: true,
105-
attributes: nil)
106-
try content.write(toFile: filename, atomically: true, encoding: String.Encoding.utf8)
107-
return true
108-
} catch let error as NSError {
109-
print(error)
110-
return false
112+
static internal func writeToFileWith(_ name: String, content: String, path: String) -> Bool {
113+
let filename = path.appendingFormat("%@", (name + ".swift"))
114+
do {
115+
try FileManager.default.createDirectory(at: URL.init(fileURLWithPath: path),
116+
withIntermediateDirectories: true,
117+
attributes: nil)
118+
try content.write(toFile: filename, atomically: true, encoding: String.Encoding.utf8)
119+
return true
120+
} catch let error as NSError {
121+
print(error)
122+
return false
123+
}
111124
}
112-
}
113125

114-
static fileprivate func todayDateString() -> String {
115-
let formatter = DateFormatter.init()
116-
formatter.dateStyle = .short
117-
return formatter.string(from: Date.init())
118-
}
126+
static fileprivate func todayDateString() -> String {
127+
let formatter = DateFormatter.init()
128+
formatter.dateStyle = .short
129+
return formatter.string(from: Date.init())
130+
}
119131

120132
}

SwiftyJSONAccelerator/Generators/ModelGenerator.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ public struct ModelGenerator {
145145
return ObjectMapperModelFile()
146146
case .SwiftyJSON:
147147
return SwiftyJSONModelFile()
148+
case .Marshal:
149+
return MarshalModelFile()
148150
}
149151
}
150152

0 commit comments

Comments
 (0)