Skip to content

Commit

Permalink
Added support for Realm with Android
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahmed-Ali committed Dec 14, 2014
1 parent baef9ec commit 204aa96
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 6 deletions.
4 changes: 4 additions & 0 deletions JSONExport.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
E20615C31A3E03210010C177 /* Java-Android Realm.json in Resources */ = {isa = PBXBuildFile; fileRef = E20615C21A3E03210010C177 /* Java-Android Realm.json */; };
E20A0DAD1A1170F1003B3D62 /* NoodleLineNumberMarker.m in Sources */ = {isa = PBXBuildFile; fileRef = E20A0DAA1A1170F1003B3D62 /* NoodleLineNumberMarker.m */; };
E20A0DAE1A1170F1003B3D62 /* NoodleLineNumberView.m in Sources */ = {isa = PBXBuildFile; fileRef = E20A0DAC1A1170F1003B3D62 /* NoodleLineNumberView.m */; };
E21292581A0C58CF0015027D /* SharedConstants.swift in Sources */ = {isa = PBXBuildFile; fileRef = E21292571A0C58CF0015027D /* SharedConstants.swift */; };
Expand Down Expand Up @@ -39,6 +40,7 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
E20615C21A3E03210010C177 /* Java-Android Realm.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; name = "Java-Android Realm.json"; path = "PredefinedLanguages/Java-Android Realm.json"; sourceTree = "<group>"; };
E20A0DA81A1170F1003B3D62 /* JSONExport-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "JSONExport-Bridging-Header.h"; sourceTree = "<group>"; };
E20A0DA91A1170F1003B3D62 /* NoodleLineNumberMarker.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NoodleLineNumberMarker.h; sourceTree = "<group>"; };
E20A0DAA1A1170F1003B3D62 /* NoodleLineNumberMarker.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NoodleLineNumberMarker.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -140,6 +142,7 @@
isa = PBXGroup;
children = (
E2193D051A1292A200138A29 /* Java-Android.json */,
E20615C21A3E03210010C177 /* Java-Android Realm.json */,
E278F9C71A214FF000306EFC /* ObjectiveC-iOS.json */,
E27421D81A334165009685AD /* ObjectiveC-Realm.json */,
E2DEAE9A1A2295E80001818B /* ObjectiveC-Mac.json */,
Expand Down Expand Up @@ -265,6 +268,7 @@
files = (
E27421D91A334165009685AD /* ObjectiveC-Realm.json in Resources */,
E2D930C21A2661350014B195 /* ObjectiveC-CoreData-iOS.json in Resources */,
E20615C31A3E03210010C177 /* Java-Android Realm.json in Resources */,
E2DEAE9B1A2295E80001818B /* ObjectiveC-Mac.json in Resources */,
E27421E71A33637A009685AD /* Siwft-Realm.json in Resources */,
E23C7E611A17CAC50009B9E5 /* SwiftyJSON-Class.json in Resources */,
Expand Down
2 changes: 2 additions & 0 deletions JSONExport/FileRepresenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ class FileRepresenter{
appendSettersAndGetters()
appendInitializers()
appendUtilityMethods()
fileContent = fileContent.stringByReplacingOccurrencesOfString(lowerCaseModelName, withString:className.lowercaseFirstChar())
fileContent = fileContent.stringByReplacingOccurrencesOfString(modelName, withString:className)
fileContent += lang.modelEnd
return fileContent
}
Expand Down
67 changes: 67 additions & 0 deletions JSONExport/PredefinedLanguages/Java-Android Realm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"langName": "Java-Realm",
"displayLangName": "Java Realm for Android",
"briefDescription" : "Defines how your JSON objects can be mapped to Java classes using Android's org.json JSONObject with support to Realm.",
"fileExtension": "java",
"genericType": "Object",
"arrayType": "RealmList<<!ElementType!>>",
"dataTypes": {
"intType": "int",
"boolType": "boolean",
"characterType": "char",
"floatType": "float",
"longType": "long",
"doubleType": "double",
"stringType": "String"
},
"supportsFirstLineStatement" : "true",
"firstLineHint" : "Package name",
"staticImports": "import org.json.*;\nimport io.realm.*;\nimport io.realm.annotations.*;\n",
"importForEachCustomType": "",
"modelDefinition": "\n@RealmClass\npublic class <!ModelName!> extends RealmObject",
"modelDefinitionWithParent" : "\npublic class <!ModelName!> extends <!ParentClass!>",
"modelStart": "{\n",
"modelEnd": "\n}",
"instanceVarDefinition": "\tprivate <!VarType!> <!VarName!>;\n",
"setter" : "\tpublic void set<!CapitalizedVarName!>(<!VarType!> <!VarName!>){\n\t\tthis.<!VarName!> = <!VarName!>;\n\t}\n",
"getter" : "\tpublic <!VarType!> get<!CapitalizedVarName!>(){\n\t\treturn this.<!VarName!>;\n\t}\n",
"booleanGetter" : "\tpublic <!VarType!> is<!CapitalizedVarName!>()\n\t{\n\t\treturn this.<!VarName!>;\n\t}\n",
"wordsToRemoveToGetArrayElementsType": [
"RealmList",
"<",
">"
],
"basicTypesWithSpecialFetchingNeeds" : [
"Object"
],
"basicTypesWithSpecialFetchingNeedsReplacements" : [

""
],
"constructors": [
{
"comment": "\t/**\n\t * Creates instance using the passed realm and jsonObject to set the properties values\n\t */\n",
"signature": "\tpublic static <!ModelName!> fromJson(Realm realm, JSONObject jsonObject)",
"bodyStart": "{\n\t\tif(jsonObject == null){\n\t\t\treturn null;\n\t\t}\n\t\t<!ModelName!> <!LowerCaseModelName!> = realm.createObject(<!ModelName!>.class);\n",
"bodyEnd": "\t\treturn <!LowerCaseModelName!>;\n\t}\n",
"fetchBasicTypePropertyFromMap": "\t\t<!LowerCaseModelName!>.<!VarName!> = jsonObject.opt<!CapitalizedVarType!>(\"<!JsonKeyName!>\");\n",
"fetchBasicTypeWithSpecialNeedsPropertyFromMap": "\t\t<!LowerCaseModelName!>.<!VarName!> = jsonObject.opt<!VarBasicTypeReplacement!>(\"<!JsonKeyName!>\");\n",
"fetchCustomTypePropertyFromMap": "\t\t<!LowerCaseModelName!>.<!VarName!> = <!VarType!>.fromJson(realm, jsonObject.optJSONObject(\"<!JsonKeyName!>\"));\n",
"fetchArrayOfCustomTypePropertyFromMap": "\t\tJSONArray <!VarName!>JsonArray = jsonObject.optJSONArray(\"<!JsonKeyName!>\");\n\t\tif(<!VarName!>JsonArray != null){\n\t\t\tfor (int i = 0; i < <!VarName!>JsonArray.length(); i++) {\n\t\t\t\tJSONObject <!VarName!>Object = <!VarName!>JsonArray.optJSONObject(i);\n\t\t\t\t<!ElementType!> <!VarName!>Value = <!ElementType!>.fromJson(realm, <!VarName!>Object);\n\t\t\t\tif(<!VarName!>Value != null){\n\t\t\t\t\t<!LowerCaseModelName!>.get<!CapitalizedVarName!>().add(<!VarName!>Value);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n"
}
],
"utilityMethods": [
{
"comment": "\t/**\n\t * Returns all the available property values in the form of JSONObject instance where the key is the approperiate json key and the value is the value of the corresponding field\n\t */\n",
"signature": "\tpublic static JSONObject toJsonObject(<!ModelName!> <!LowerCaseModelName!>)",
"bodyStart": "\n\t{\n",
"bodyEnd": "\t}\n",
"body": "\t\tJSONObject jsonObject = new JSONObject();\n\t\ttry {\n",
"forEachProperty": "\t\t\tjsonObject.put(\"<!JsonKeyName!>\", <!LowerCaseModelName!>.<!VarName!>);\n",
"forEachCustomTypeProperty" : "\t\t\tjsonObject.put(\"<!JsonKeyName!>\", <!VarType!>.toJsonObject(<!LowerCaseModelName!>.<!VarName!>));\n",

"forEachArrayOfCustomTypeProperty": "\t\t\tif(<!LowerCaseModelName!>.<!VarName!> != null && <!LowerCaseModelName!>.<!VarName!>.size() > 0){\n\t\t\t\tJSONArray <!VarName!>JsonArray = new JSONArray();\n\t\t\t\tfor(<!ElementType!> <!VarName!>Element : <!LowerCaseModelName!>.<!VarName!>){\n\t\t\t\t\t<!VarName!>JsonArray.put(<!ElementType!>.toJsonObject(<!VarName!>Element));\n\t\t\t\t}\n\t\t\t\tjsonObject.put(\"<!JsonKeyName!>\", <!VarName!>JsonArray);\n\t\t\t}\n",
"returnStatement": "\t\t} catch (JSONException e) {\n\t\t\t// TODO Auto-generated catch block\n\t\t\te.printStackTrace();\n\t\t}\n\t\treturn jsonObject;\n"
}
]
}
12 changes: 6 additions & 6 deletions JSONExport/PredefinedLanguages/Java-Android.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
"modelStart": "{\n",
"modelEnd": "\n}",
"instanceVarDefinition": "\tprivate <!VarType!> <!VarName!>;\n",
"setter" : "\tpublic void set<!CapitalizedVarName!>(<!VarType!> <!VarName!>)\n\t{\n\t\tthis.<!VarName!> = <!VarName!>;\n\t}\n",
"getter" : "\tpublic <!VarType!> get<!CapitalizedVarName!>()\n\t{\n\t\treturn this.<!VarName!>;\n\t}\n",
"setter" : "\tpublic void set<!CapitalizedVarName!>(<!VarType!> <!VarName!>){\n\t\tthis.<!VarName!> = <!VarName!>;\n\t}\n",
"getter" : "\tpublic <!VarType!> get<!CapitalizedVarName!>(){\n\t\treturn this.<!VarName!>;\n\t}\n",
"booleanGetter" : "\tpublic <!VarType!> is<!CapitalizedVarName!>()\n\t{\n\t\treturn this.<!VarName!>;\n\t}\n",
"wordsToRemoveToGetArrayElementsType": [
"[",
Expand All @@ -51,15 +51,15 @@
],
"utilityMethods": [
{
"comment": "\t/**\n\t * Returns all the available property values in the form of NSDictionary object where the key is the approperiate json key and the value is the value of the corresponding property\n\t */\n",
"comment": "\t/**\n\t * Returns all the available property values in the form of JSONObject instance where the key is the approperiate json key and the value is the value of the corresponding field\n\t */\n",
"signature": "\tpublic JSONObject toJsonObject()",
"bodyStart": "\n\t{\n",
"bodyEnd": "\t}\n",
"body": "\t\tJSONObject jsonObject = new JSONObject();\n\t\ttry {\n",
"forEachProperty": "\t\tjsonObject.put(\"<!JsonKeyName!>\", <!VarName!>);\n",
"forEachCustomTypeProperty" : "\t\tjsonObject.put(\"<!JsonKeyName!>\", <!VarName!>.toJsonObject());\n",
"forEachProperty": "\t\t\tjsonObject.put(\"<!JsonKeyName!>\", <!VarName!>);\n",
"forEachCustomTypeProperty" : "\t\t\tjsonObject.put(\"<!JsonKeyName!>\", <!VarName!>.toJsonObject());\n",

"forEachArrayOfCustomTypeProperty": "\t\tif(<!VarName!> != null && <!VarName!>.length > 0){\n\t\t\tJSONArray <!VarName!>JsonArray = new JSONArray();\n\t\t\tfor(<!VarType!> <!VarName!>Element : <!VarName!>){\n\t\t\t\t<!VarName!>JsonArray.put(<!VarName!>Element.toJsonObject());\n\t\t\t}\n\t\t\tjsonObject.put(\"<!JsonKeyName!>\", <!VarName!>JsonArray);\n\t\t}\n",
"forEachArrayOfCustomTypeProperty": "\t\t\tif(<!VarName!> != null && <!VarName!>.length > 0){\n\t\t\t\tJSONArray <!VarName!>JsonArray = new JSONArray();\n\t\t\t\tfor(<!ElementType!> <!VarName!>Element : <!VarName!>){\n\t\t\t\t\t<!VarName!>JsonArray.put(<!VarName!>Element.toJsonObject());\n\t\t\t\t}\n\t\t\t\tjsonObject.put(\"<!JsonKeyName!>\", <!VarName!>JsonArray);\n\t\t\t}\n",
"returnStatement": "\t\t} catch (JSONException e) {\n\t\t\t// TODO Auto-generated catch block\n\t\t\te.printStackTrace();\n\t\t}\n\t\treturn jsonObject;\n"
}
]
Expand Down
1 change: 1 addition & 0 deletions JSONExport/SharedConstants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ let varType = "<!VarType!>"
let varTypeReplacement = "<!VarBasicTypeReplacement!>"
let capitalizedVarType = "<!CapitalizedVarType!>"
let lowerCaseVarType = "<!LowerCaseVarType!>"
let lowerCaseModelName = "<!LowerCaseModelName!>"
let jsonKeyName = "<!JsonKeyName!>"
let additionalCustomTypeProperty = "<!AdditionalForCustomTypeProperty!>"

0 comments on commit 204aa96

Please sign in to comment.