Object serialization

Put your problem here if it does not fit any of the other categories.

Object serialization

Postby Nemat » Tue Oct 07, 2008 3:57 pm

Hi,

Can anyone tell me about object serialization in Android??

Thanks in Advance
Nemat
Experienced Developer
Experienced Developer
 
Posts: 62
Joined: Thu Aug 21, 2008 3:16 pm
Location: Jaipur

Top

Postby plusminus » Tue Oct 07, 2008 4:14 pm

You should probably say what you want to know ;)
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby MrSnowflake » Tue Oct 07, 2008 8:37 pm

Just do normal Java serialization...
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby plusminus » Tue Oct 07, 2008 8:52 pm

I've heard that it is Java-compatible for trivial types but not for own object. But I would not rely on that. :!:

You may have a look at Parcel concept of Android, but that is not java-compatible.

So you might have to consider JSON for exchanging object-data.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby MrSnowflake » Tue Oct 07, 2008 9:04 pm

plusminus wrote:I've heard that it is Java-compatible for trivial types but not for own object. But I would not rely on that. :!:
I haven't used serialization, ever, but shouldn't you always implement serialization for yourself?
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby plusminus » Tue Oct 07, 2008 9:27 pm

In standard-java you [font=Courier New]implement Serializable[/font] and you are fine in 99% of all cases.
Its some reflection-magic behind all that that manages to save the classes data then.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Postby MrSnowflake » Tue Oct 07, 2008 9:56 pm

Didn't know that, I'm looking through the serialization docs atm :). But I suppose, as all normal java classes and stuf works on android, serialization will too.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby plusminus » Tue Oct 07, 2008 10:00 pm

MrSnowflake wrote:Didn't know that, I'm looking through the serialization docs atm :). But I suppose, as all normal java classes and stuf works on android, serialization will too.

Its a bit more complicated ;) . See:
http://groups.google.com/group/android- ... bc489cafcd

But i found a library that serializes not to binary, but to xml-data:
http://jars.de/java/android-xml-seriali ... th-xstream

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby MrSnowflake » Tue Oct 07, 2008 10:10 pm

Strange stuff. I saw the android port of xstream earlier today :).
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby plusminus » Wed Oct 08, 2008 1:38 am

I just found that (since probably sdk 0.9) Seriaization is fully and officially supported but is said to be really slow.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby MrSnowflake » Wed Oct 08, 2008 11:09 am

Too bad it's slow, but I guess as you only have to do it at the loading and stopping of your app, it's not a real problem?!
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby Nemat » Sat Oct 11, 2008 12:15 pm

plusminus wrote:
I've heard that it is Java-compatible for trivial types but not for own object. But I would not rely on that. Exclamation

You may have a look at Parcel concept of Android, but that is not java-compatible.

So you might have to consider JSON for exchanging object-data.

Regards,
plusminus


I have to send data over Http connection after serializing it.
I set the data in JSON object that will be sent via Http connection.Is converting the data in JSON object a way to serialize it or I need to have a class which extends Serializable??
Nemat
Experienced Developer
Experienced Developer
 
Posts: 62
Joined: Thu Aug 21, 2008 3:16 pm
Location: Jaipur

Postby plusminus » Sat Oct 11, 2008 2:01 pm

JSON Data is being passed as simple Strings over http. Example:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. {
  2.  
  3.         "id": "0001",
  4.  
  5.         "type": "donut",
  6.  
  7.         "name": "Cake",
  8.  
  9.         "ppu": 0.55,
  10.  
  11.         "batters":
  12.  
  13.                 {
  14.  
  15.                         "batter":
  16.  
  17.                                 [
  18.  
  19.                                         { "id": "1001", "type": "Regular" },
  20.  
  21.                                         { "id": "1002", "type": "Chocolate" },
  22.  
  23.                                         { "id": "1003", "type": "Blueberry" },
  24.  
  25.                                         { "id": "1004", "type": "Devil's Food" }
  26.  
  27.                                 ]
  28.  
  29.                 },
  30.  
  31.         "topping":
  32.  
  33.                 [
  34.  
  35.                         { "id": "5001", "type": "None" },
  36.  
  37.                         { "id": "5002", "type": "Glazed" },
  38.  
  39.                         { "id": "5005", "type": "Sugar" },
  40.  
  41.                         { "id": "5007", "type": "Powdered Sugar" },
  42.  
  43.                         { "id": "5006", "type": "Chocolate with Sprinkles" },
  44.  
  45.                         { "id": "5003", "type": "Chocolate" },
  46.  
  47.                         { "id": "5004", "type": "Maple" }
  48.  
  49.                 ]
  50.  
  51. }
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby burakkilic » Sun Oct 18, 2009 3:59 pm

Is there any method to serialize Java object to JSON string? Or vice versa?
burakkilic
Developer
Developer
 
Posts: 37
Joined: Thu Oct 01, 2009 10:24 am

Postby padde » Sun Oct 18, 2009 4:11 pm

padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Top
Next

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 35 guests