I’m trying to serialize an object using servicestack in monotouch and I can’t for the life of me figure out how to get the classes to be available when doing an AOT build for the device. The build works fine for the simulator. I have the latest version of Monotouch, and latest version of the Monotouch ServiceStack library, which I pulled and built myself this morning.
What I’m trying to do is call the generic ToJson() to serialize model object (called Note below) of mine. All of my web service models / service calls / serialization / deserialization happen inside of a class library external to my iphone and android app, in which I am trying to call the JsConfig.RegisterForAot() and JsConfig.RegisterTypeForAot(), which seems to not help. I’m also calling those methods inside my iphone app since I’m unsure if both locations are necessary.
Error/Stack trace:
Attempting to JIT compile method 'ServiceStack.Text.Common.WriteListsOfElements`2<int, ServiceStack.Text.Json.JsonTypeSerializer>:WriteListValueType (System.IO.TextWriter,object)' while running with --aot-only. See http://docs.xamarin.com/ios/about/limitations for more information.
at ServiceStack.Text.Common.WriteType`2[MyClassLibrary.Note,ServiceStack.Text.Json.JsonTypeSerializer].WriteProperties (System.IO.TextWriter writer, System.Object value) [0x00000] in <filename unknown>:0
at ServiceStack.Text.JsonSerializer.SerializeToString (System.Object value, System.Type type) [0x00000] in <filename unknown>:0
at ServiceStack.Text.JsonSerializer.SerializeToString[Object] (System.Object value) [0x00000] in <filename unknown>:0
at ServiceStack.Text.StringExtensions.ToJson[Object] (System.Object obj) [0x00000] in <filename unknown>:0
at MyClassLibrary.Utils.ObjectToJson (System.Object obj) [0x00000] in <filename unknown>:0
at MyClassLibrary.Utils.AddObjectToData (System.Net.WebRequest request, System.Object obj) [0x00000] in <filename unknown>:0
at MyClassLibrary.Utils.SendJsonRequest[Note] (System.String url, System.String httpVerb, MyClassLibrary.Note obj) [0x00000] in <filename unknown>:0
at MyClassLibrary.Note.JSONSave () [0x00000] in <filename unknown>:0
at iphoneapp.myViewController.SaveNote () [0x000a5] in /Users/myUser/Projects/iphoneapp/iphoneapp/ViewControllers/myViewController.cs:83
Exact target/types:
ServiceStack.Text.Common.WriteType`2[[MyClassLibrary.Note, MyClassLibraryName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[ServiceStack.Text.Json.JsonTypeSerializer, ServiceStack.Text.MonoTouch, Version=3.8.5.0, Culture=neutral, PublicKeyToken=null]]
Any help would be appreciated.
I’ve run into a few of these problems and the issue usually came down to the use of enums in some of my DTO properties.
What you need to do is force initialisation of the generic serializer for each enum explicitly for the AOT compiler.
The way I do this is to create a bunch of dummy serializers (I don’t do anything with them) in my AppDelegate.FinishedLaunching code:
If you are only using the JSON side then you should have some success with the approach above.
I have some cases where I serialise lists into a single string property, so I also initialise the JsvSerialiser in the same way as above.
The key is to explicitly create serializers for your enums in code, so that the AOT compiler has something to find and compile ahead.