Maybe I’m completely on the wrong track, but I’m trying to store Objects, for example an Array in a field in a table. The field is of type = media. (I’ve created the field through Servoy, currently working on Sybase DB).
Later onwards, i would like to retrieve the object and be able to work with it again.
For example: to store:
var a = new Array();
a[0] = “test1”
a[1] = “test2”
media_field = a;
And to retrieve:
var b = new Array();
b = media_field;
application.output(b[0] + b[1]);
what you want can shurely be done in lot of different ways. I would not go with blob field… but with a plain normal text field… (with an appropriate dimension…)
To store: (array_field is a text filed of your table)
array_field = “Test1” + “\n”
array_field += “Test2” + “\n”
array_field += “Test3” + “\n”…
And to retrieve:
var b = new Array();
b = array_field. split(“\n”);
“\n” is a separator character (can be any)… btw.. using “\n” you will also be able to display in a smart way your array_field in a text area field
Your example works perfectly for Arrays, but an Array was just an example.
I really would like to be able to store objects in dataproviders of type = media, just like you can store any kind of object in a global variable of type = media.
I’ve experimented with it, and it seems like you can put an Object in, but when trying to get it out again it becomes a bytearray. Somehow I need to convert it back to the original object. Dunno if that is possible…
Ok, I see the problem… . Probably one way to do it is to store the blob field on a temporary file on disk with application.writeFile() and then retrive it with an application.readFile() or application.readTXTFile() but let’s hope someone gives us a tip on how to do it in a smarter way.
pbakker:
I’ve experimented with it, and it seems like you can put an Object in, but when trying to get it out again it becomes a bytearray. Somehow I need to convert it back to the original object. Dunno if that is possible…
If you want to store objects in the database, you only can put them in a media field (database blob) and you have to serialize the object to a byte array (and reconstruct back when loaded from byte array)
I always use globally scoped variables (variables that aren’t declared with the “var” part in a method) to store objects. No conversions necessary to store and retrieve.
HJK:
Hi Jan, this is a very old post, but can you give some example how to put an object (for instance an array) into a blob and how to reconstruct back?
I’m not familiar with serializing byte arrays.
Since I don’t know how to serialize objects, I joined the array and stored it in a text field.
When I need to load it back, I split it.
Here I am trying to resuscitate this topic. Please, I need some help. It has all to do with what have been discussed here.
I need to restore an object previously stored in database as a ByteArray. For instance, the object is a java.util.HashMap.
As you can notice, I serialize the object as a ByteArray before storing in the column with type media (binary). The problem is when I attempt to restore this object, it comes as a Object (or ByteArray) and it is not possible to cast it back to a HashMap. JavaScript doesn’t have cast.
If you see the other posts the same question is raised but not answered:
pbakker:
I’ve experimented with it, and it seems like you can put an Object in, but when trying to get it out again it becomes a bytearray. Somehow I need to convert it back to the original object. Dunno if that is possible..
do you have a small workable solution that would allow to test that?
I have the idea that somehow I could build a very simple “cast” plugin that would do the trick, but that would help if you provide a sample solution.
This is indeed an very old post, on a column in Servoy attach the serialize converter, this will automatically marshall your objects to bytes/string and the other way arround