@@ -84,14 +84,19 @@ private string GetObjCPropertySetter(OdcmProperty prop)
8484 return "set" + GetObjCProperty(prop).ToUpperFirstChar();
8585}
8686
87- private bool IsNSJSONSerializable (OdcmType type)
87+ private bool IsObjCTypeSameAsJsonType (OdcmType type)
8888{
89+ // TODO: To be more correct, we should add a GetJsonType() method
90+ // and return (GetObjCType(type) == GetJsonType(type)). Practically speaking though,
91+ // only Edm.String has the same friendly ObjC type and JSON serializer type.
8992 string ts=GetObjCTypeIdentifier(type);
90- return ts=="NSString" || ts=="NSNumber" || ts=="NSArray" || ts=="NSDictionary" || ts=="NSNull" ;
93+ return ts=="NSString";
9194}
9295
93- private string GetHydratedPropertyFromNSJSONSerializableExpression (string expr,OdcmProperty prop)
96+ private string GetJsonToObjCExpressionConversion (string expr,OdcmProperty prop)
9497{
98+ // This generates a wrapper around a provided JSON-typed expression (i.e. types obtained from NSJSONSerialization)
99+ // that converts it to a user-friendly ObjC type i.e. generated Entity/enum class etc.
95100 if(GetObjCTypeIdentifier(prop,true)=="NSDate")
96101 {
97102 return String.Format(@"[NSDate ms_dateFromString: {0}]",expr);
@@ -104,8 +109,8 @@ private string GetHydratedPropertyFromNSJSONSerializableExpression(string expr,O
104109 expr,
105110 GetObjCTypeIdentifier(prop,true));
106111 }
107- //Complex catch-all
108- if(prop.IsComplex() )
112+
113+ if(prop.Type is OdcmEntityClass || prop.Type is OdcmMediaClass || prop.Type is OdcmComplexClass )
109114 {
110115 return String.Format(
111116 @"[[{0} alloc] initWithDictionary: {1} ]",
@@ -121,39 +126,42 @@ private string GetHydratedPropertyFromNSJSONSerializableExpression(string expr,O
121126
122127private string GetHydratedPropertyFromDictionary(OdcmProperty prop)
123128{
124- return GetHydratedPropertyFromNSJSONSerializableExpression (String.Format(@"self.dictionary[@""{0}""]",prop.Name),prop);
129+ return GetJsonToObjCExpressionConversion (String.Format(@"self.dictionary[@""{0}""]",prop.Name),prop);
125130}
126131
127- private string SetNSJSONPermitedExpressionSerialized (OdcmType type,string val)
132+ private string GetObjCToJsonConvertibleExpressionConversion (OdcmType type, bool isCollection, string val)
128133{
129- //Fallback to complex entities that must support ms_toString
130- if(type.IsComplex())
134+ // This generates a wrapper around a provided friendly-ObjC-typed expression that converts it
135+ // to an expression that can be passed into [MSObject getNSJsonSerializationCompatibleValue].
136+ //
137+ // The categories of Obj-C property types are as follows:
138+ // Entity/Complex generated classes
139+ // Collections (NSArrays)
140+ // Enum generated classes
141+ // System object-types (NSString, NSDate)
142+ // System primitives (int types, BOOL)
143+ //
144+ // All of these implement the methods (some via extensions) dictionary/arrayWithItem or ms_toString
145+ // except for system primitives, which we need to box here using @().
146+ if (type.IsPrimitive() && !isCollection)
131147 {
132- return val;
148+ return "@(" + val + ")" ;
133149 }
134150
135- if(type is OdcmEnum)
136- {
137- return String.Format("[NSString stringWith{0}:{1}]",GetObjCTypeIdentifier(type),val);
138- }
139-
140- return "@(" + val + ")";
151+ return val;
141152}
142153
143- private string SetNSJSONPermitedExpressionSerialized (OdcmProperty prop,string val)
154+ private string GetObjCToJsonConvertibleExpressionConversion (OdcmProperty prop,string val)
144155{
145- //Fallback to complex entities that must support ms_toString
146- if(prop.IsCollection)
147- {
148- return val;
149- }
150-
151- return SetNSJSONPermitedExpressionSerialized(prop.Type,val);
156+ return GetObjCToJsonConvertibleExpressionConversion(prop.Type, prop.IsCollection, val);
152157}
153158
154- private string SetDictionaryPropertySerialized (OdcmProperty prop, string val)
159+ private string SetDictionaryPropertyInJsonConvertibleForm (OdcmProperty prop, string val)
155160{
156- return String.Format(@"self.dictionary[@""{0}""] = {1};",prop.Name,SetNSJSONPermitedExpressionSerialized(prop,val));
161+ // Dictionary entries can either be directly NSJSON-typed, or convertible to
162+ // a NSJSON type via [MSObject getNSJsonSerializationCompatibleValue].
163+ //
164+ return String.Format(@"self.dictionary[@""{0}""] = {1};",prop.Name,GetObjCToJsonConvertibleExpressionConversion(prop,val));
157165}
158166
159167private string GetHydratedIVarFromDictionary(OdcmProperty prop)
@@ -176,7 +184,7 @@ private string GetHydratedIVarFromDictionary(OdcmProperty prop)
176184 ",
177185 GetObjCProperty(prop),
178186 prop.Name,
179- GetObjCProperty(prop),
187+ GetJsonToObjCExpressionConversion( GetObjCProperty(prop), prop),
180188 GetStaticCollectionObject());
181189 }
182190
@@ -201,7 +209,7 @@ public string GetFunctionParameterDictionary(List<OdcmParameter> parameters)
201209 {
202210 result.AppendFormat("[{0}Object getNSJsonSerializationCompatibleValue:{1}],",
203211 writer.GetStaticCodePrefix(),
204- SetNSJSONPermitedExpressionSerialized (param.Type,"_"+param.Name.ToLowerFirstChar()));
212+ GetObjCToJsonConvertibleExpressionConversion (param.Type, param.IsCollection, "_"+param.Name.ToLowerFirstChar()));
205213
206214 result.AppendFormat("@\"{0}\",", param.Name);
207215 }
@@ -239,7 +247,7 @@ private void PropertyGetterImplementation(OdcmProperty prop)
239247- (<#=GetObjCTypeForVarDeclaration(prop)#>) <#=GetObjCPropertyGetter(prop)#>
240248{
241249<#+
242- if(IsNSJSONSerializable (prop.Type))
250+ if(IsObjCTypeSameAsJsonType (prop.Type))
243251 {
244252#>
245253 return self.dictionary[@"<#=prop.Name#>"];
@@ -278,7 +286,7 @@ private void PropertySetterImplementation(OdcmProperty prop)
278286- (void) <#=GetObjCPropertySetter(prop)#>: (<#=GetObjCTypeForVarDeclaration(prop)#>) val
279287{
280288<#+
281- if(IsNSJSONSerializable (prop.Type))
289+ if(IsObjCTypeSameAsJsonType (prop.Type))
282290 {
283291#>
284292 self.dictionary[@"<#=prop.Name#>"] = val;
@@ -288,7 +296,7 @@ private void PropertySetterImplementation(OdcmProperty prop)
288296 {
289297#>
290298 _<#=GetObjCProperty(prop)#> = val;
291- <#=SetDictionaryPropertySerialized (prop,"val")#>
299+ <#=SetDictionaryPropertyInJsonConvertibleForm (prop,"val")#>
292300<#+
293301 }
294302#>
0 commit comments