Use of existing object model in Android w/ serialization RRS feed

  • Question

  • User7920 posted

    This seems like it is a maddeningly simple problem, but days of googling and I cannot find any solution....

    ** How can I serialize or parcelize my object data to be usable in both Android and desktop (In this case Xwt or Gtk)?

    I currently have my object data serializable with protobuf-net, and have been able to open this file in Android, but how I make this data be shared across activities is giving me real trouble. Here is what I have tried:

    1) Move model to a PCL, and used the protobuf-net PCL version of the dll: Still doesn't seem the data is recogizable when I try to PutExtraSerialable() into an Intent. 2) Modify my objects to have Java.IO.Serializable: Can't seem to do this, because I cannot add the Java reference in the model (I think this is because it is not an Android framework library?) 3) Modify my object to have Java's Parcelable requirements: Same as #2 above.

    ...and that leaves me thinking the only possible way is to rewrite my model in both Android and Xwt, each with their own frameworks that will allow me to reference the serialization libraries. ....and that just doesn't seem right.

    I must be missing something really obvious. Can anyone help out with a pointer in the right direction?

    Jeff Halverson

    Tuesday, February 11, 2014 11:50 AM

All replies

  • User12403 posted

    Put it as a string and deserialize with ProtoBuf inside the activity.

    Tuesday, February 11, 2014 2:17 PM
  • User7920 posted

    I'm not sure I understand the suggestion. Let me clarify what I am trying to do.

    In my initial activity I have pulled in all my data that was saved with protobuf from the SDcard. In a subsequent activity, I would need to be able to pull in, for example, all the "Broker" objects to allow editing the brokers name, address, etc. (DEMO VERSION!) This means I would have to pass my object "List allbrokers" into the second activity, and allow the second activity to either change these objects, or to report back to the first activity what changes need to me made.

    As I understand it, using a PutExtra on my List object will fail because it does not implement ISerializable or Parcelable. But I cannot implement these interfaces (DEMO VERSION!) because the same data model is used in my Xwt application, which cannot reference the Android framework...

    If I am still misunderstanding, could you suggest how putting in a string would work, perhaps with a bit of code? I'm quite new to Android so I may be missing something very obvious.


    Jeff Havlerson

    Friday, February 14, 2014 3:05 AM
  • User12403 posted

    I created a sample project here using a Json serializer: https://github.com/sami1971/SimplyMobile/tree/master/Android/Samples/StackOverflowSamples

    Main activity has a simple static DTO that gets passed to the second activity:

    private static GenericDto dto = new GenericDto();

    When user clicks on the button, dto updates its count and then creates an intent with a bundle. In the bundle I use a JSON serializer to serialize the dto to a string, then put that string into the bundle.

            button.Click += delegate 
                button.Text = string.Format("{0} clicks!", ++dto.Count); 
                var serializer = new JsonSerializer();
                var intent = new Intent(this, typeof(SerializationSampleActivity));
                var b = new Bundle();
                b.PutString("object", serializer.Serialize(dto));

    On the receiving Activity I pull that string from the intent and then use the JSON serializer to deserialize the string back into an object:

        protected override void OnCreate(Bundle bundle)
            base.OnCreate (bundle);
            var serializer = new JsonSerializer ();
            var str = this.Intent.Extras.GetString ("object");
            var obj = serializer.Deserialize<GenericDto> (str);
            FindViewById<TextView> (Resource.Id.textCount).Text = 
                string.Format ("Clicked {0} times.", obj.Count);
    Friday, February 14, 2014 2:48 PM