@@ -29,35 +29,11 @@ private string GetObjCTypeIdentifier(OdcmObject o, bool getUnderlyingType=false)
2929 OdcmProperty prop=(OdcmProperty)o;
3030 if(prop.IsCollection && !getUnderlyingType)
3131 {
32- if(IsNSJSONSerializable(prop.Type))
33- {
34- return GetStaticCollectionObject();
35- }
36- else
37- {
38- return prop.Type.GetCollectionTypeString();
39- }
32+ return @"NSArray";
4033 }
4134
4235 return GetObjCTypeIdentifier(prop.Type,getUnderlyingType);
4336 }
44- else if(o is OdcmMethod)
45- {
46- OdcmMethod method = (OdcmMethod)o;
47- if (method.IsCollection && !getUnderlyingType)
48- {
49- if (IsNSJSONSerializable(method.ReturnType))
50- {
51- return GetStaticCollectionObject();
52- }
53- else
54- {
55- return method.ReturnType.GetCollectionTypeString();
56- }
57- }
58-
59- return GetObjCTypeIdentifier(method.ReturnType, getUnderlyingType);
60- }
6137 else if(o is OdcmType)
6238 {
6339 return ((OdcmType)o).GetTypeString();
@@ -108,14 +84,19 @@ private string GetObjCPropertySetter(OdcmProperty prop)
10884 return "set" + GetObjCProperty(prop).ToUpperFirstChar();
10985}
11086
111- private bool IsNSJSONSerializable (OdcmType type)
87+ private bool IsObjCTypeSameAsJsonType (OdcmType type)
11288{
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.
11392 string ts=GetObjCTypeIdentifier(type);
114- return ts=="NSString" || ts=="NSNumber" || ts=="NSArray" || ts=="NSDictionary" || ts=="NSNull" ;
93+ return ts=="NSString";
11594}
11695
117- private string GetHydratedPropertyFromNSJSONSerializableExpression (string expr,OdcmProperty prop)
96+ private string GetJsonToObjCExpressionConversion (string expr,OdcmProperty prop)
11897{
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.
119100 if(GetObjCTypeIdentifier(prop,true)=="NSDate")
120101 {
121102 return String.Format(@"[NSDate ms_dateFromString: {0}]",expr);
@@ -128,11 +109,11 @@ private string GetHydratedPropertyFromNSJSONSerializableExpression(string expr,O
128109 expr,
129110 GetObjCTypeIdentifier(prop,true));
130111 }
131- //Complex catch-all
132- if(prop.IsComplex() )
112+
113+ if(prop.Type is OdcmEntityClass || prop.Type is OdcmMediaClass || prop.Type is OdcmComplexClass )
133114 {
134115 return String.Format(
135- @"[[{0} alloc] initWithDictionary: {1} ]",
116+ @"[[{0} alloc] initWithDictionary: {1}]",
136117 GetObjCTypeIdentifier(prop,true),
137118 expr);
138119 }
@@ -143,94 +124,66 @@ private string GetHydratedPropertyFromNSJSONSerializableExpression(string expr,O
143124 prop.Type.GetNSNumberValueMethod());
144125}
145126
146- private string GetHydratedPropertyFromNSJSONSerializableExpression(string expr,OdcmMethod method)
147- {
148- if(GetObjCTypeIdentifier(method,true)=="NSDate")
149- {
150- return String.Format(@"[NSDate ms_dateFromString: {0}]",expr);
151- }
152-
153- if(method.IsEnum())
154- {
155- return String.Format(
156- @"[{0} to{1}]",
157- expr,
158- GetObjCTypeIdentifier(method,true));
159- }
160- //Complex catch-all
161- if(method.IsComplex())
162- {
163- return String.Format(
164- @"[[{0} alloc] initWithDictionary: {1} ]",
165- GetObjCTypeIdentifier(method,true),
166- expr);
167- }
168-
169- return String.Format(
170- @"[{0} {1}]",
171- expr,
172- method.ReturnType.GetNSNumberValueMethod());
173- }
174-
175127private string GetHydratedPropertyFromDictionary(OdcmProperty prop)
176128{
177- return GetHydratedPropertyFromNSJSONSerializableExpression (String.Format(@"self.dictionary[@""{0}""]",prop.Name),prop);
129+ return GetJsonToObjCExpressionConversion (String.Format(@"self.dictionary[@""{0}""]",prop.Name),prop);
178130}
179131
180- private string SetNSJSONPermitedExpressionSerialized (OdcmType type,string val)
132+ private string GetObjCToJsonConvertibleExpressionConversion (OdcmType type, bool isCollection, string val)
181133{
182- //Fallback to complex entities that must support ms_toString
183- 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)
184147 {
185- return val;
148+ return "@(" + val + ")" ;
186149 }
187150
188- if(type is OdcmEnum)
189- {
190- return String.Format("[NSString stringWith{0}:{1}]",GetObjCTypeIdentifier(type),val);
191- }
192-
193- return "@(" + val + ")";
151+ return val;
194152}
195153
196- private string SetNSJSONPermitedExpressionSerialized (OdcmProperty prop,string val)
154+ private string GetObjCToJsonConvertibleExpressionConversion (OdcmProperty prop,string val)
197155{
198- //Fallback to complex entities that must support ms_toString
199- if(prop.IsCollection)
200- {
201- return val;
202- }
203-
204- return SetNSJSONPermitedExpressionSerialized(prop.Type,val);
156+ return GetObjCToJsonConvertibleExpressionConversion(prop.Type, prop.IsCollection, val);
205157}
206158
207- private string SetDictionaryPropertySerialized (OdcmProperty prop, string val)
159+ private string SetDictionaryPropertyInJsonConvertibleForm (OdcmProperty prop, string val)
208160{
209- 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));
210165}
211166
212167private string GetHydratedIVarFromDictionary(OdcmProperty prop)
213168{
214169 if(prop.IsCollection)
215170 {
216171 return String.Format(@"
217- NSMutableArray *{0}Collection = [NSMutableArray array];
218- NSArray *{0}s = self.dictionary[@""{1}""];
172+ NSMutableArray *{0}Result = [NSMutableArray array];
173+ NSArray *{0} = self.dictionary[@""{1}""];
219174
220- if ([{0}s isKindOfClass:[NSArray class]]){{
221- for (id {0 } in {0}s ){{
222- [{0}Collection addObject:{2 }];
223- }}
175+ if ([{0} isKindOfClass:[NSArray class]]){{
176+ for (id {2 } in {0}){{
177+ [{0}Result addObject:{3 }];
178+ }}
224179 }}
225180
226- if ([{0}Collection count] > 0){{
227- _{0} = [[{3} alloc] initWithArray:{0}Collection nextLink:self.dictionary[@""@nextLink""] additionalData:self.dictionary];
228- }}
181+ _{0} = {0}Result;
229182 ",
230183 GetObjCProperty(prop),
231184 prop.Name,
232- GetObjCProperty( prop),
233- GetObjCTypeIdentifier( prop));
185+ prop.Type.Name.ToLowerFirstChar( ),
186+ GetJsonToObjCExpressionConversion(prop.Type.Name.ToLowerFirstChar(), prop));
234187 }
235188
236189 return String.Format(@"_{0} = {1};",GetObjCProperty(prop),GetHydratedPropertyFromDictionary(prop));
@@ -254,7 +207,7 @@ public string GetFunctionParameterDictionary(List<OdcmParameter> parameters)
254207 {
255208 result.AppendFormat("[{0}Object getNSJsonSerializationCompatibleValue:{1}],",
256209 writer.GetStaticCodePrefix(),
257- SetNSJSONPermitedExpressionSerialized (param.Type,"_"+param.Name.ToLowerFirstChar()));
210+ GetObjCToJsonConvertibleExpressionConversion (param.Type, param.IsCollection, "_"+param.Name.ToLowerFirstChar()));
258211
259212 result.AppendFormat("@\"{0}\",", param.Name);
260213 }
@@ -292,7 +245,7 @@ private void PropertyGetterImplementation(OdcmProperty prop)
292245- (<#=GetObjCTypeForVarDeclaration(prop)#>) <#=GetObjCPropertyGetter(prop)#>
293246{
294247<#+
295- if(IsNSJSONSerializable (prop.Type))
248+ if(IsObjCTypeSameAsJsonType (prop.Type))
296249 {
297250#>
298251 return self.dictionary[@"<#=prop.Name#>"];
@@ -331,7 +284,7 @@ private void PropertySetterImplementation(OdcmProperty prop)
331284- (void) <#=GetObjCPropertySetter(prop)#>: (<#=GetObjCTypeForVarDeclaration(prop)#>) val
332285{
333286<#+
334- if(IsNSJSONSerializable (prop.Type))
287+ if(IsObjCTypeSameAsJsonType (prop.Type))
335288 {
336289#>
337290 self.dictionary[@"<#=prop.Name#>"] = val;
@@ -341,7 +294,7 @@ private void PropertySetterImplementation(OdcmProperty prop)
341294 {
342295#>
343296 _<#=GetObjCProperty(prop)#> = val;
344- <#=SetDictionaryPropertySerialized (prop,"val")#>
297+ <#=SetDictionaryPropertyInJsonConvertibleForm (prop,"val")#>
345298<#+
346299 }
347300#>
0 commit comments