Jump to page: 1 2
Thread overview
Save/load data to a file
Nov 16, 2008
nobody
Nov 16, 2008
Christopher Wright
Nov 16, 2008
bearophile
Nov 16, 2008
nobody
Re: Save/load data to a file (JSON for phobos)
Nov 18, 2008
Alan Knowles
Nov 18, 2008
nobody
Nov 21, 2008
Niels
Nov 16, 2008
Denis Koroskin
Nov 16, 2008
Bill Baxter
Nov 17, 2008
nobody
Nov 17, 2008
Bill Baxter
Nov 16, 2008
Derek Parnell
November 16, 2008
I would like to be able to save and load a lot of data to/from a file. (in
D1)
For example a struct like this:

struct Fruit
{
    int banana;
    double[][] orange;
    bool[] apple;
}

Practically all the examples that I've come across only deal with saving and loading text, so I'm having a hard time dealing with saving/loading arrays/floats/bools/etc.

What would be a good way to do this?


November 16, 2008
nobody wrote:
> I would like to be able to save and load a lot of data to/from a file. (in D1)
> For example a struct like this:
> 
> struct Fruit
> {
>     int banana;
>     double[][] orange;
>     bool[] apple;
> }
> 
> Practically all the examples that I've come across only deal with saving and loading text, so I'm having a hard time dealing with saving/loading arrays/floats/bools/etc.
> 
> What would be a good way to do this? 

XML is commonly used, and while I don't particularly like it, I find it's still a reasonable choice in many circumstances.

If you're using Tango, you can check out tango.text.xml.Document, which will let you construct an XML document, and tango.text.xml.DocPrinter, which will let you get the textual representation of such a document.

In phobos, there's std.xml, which should offer equivalent functionality, but I haven't used it.
November 16, 2008
Christopher Wright:
> XML is commonly used, and while I don't particularly like it, I find it's still a reasonable choice in many circumstances.

I don't know if a D library is available for Json file format, it has some advantages and some disadvantages over XML ().

Bye,
bearophile
November 16, 2008
On Sun, Nov 16, 2008 at 3:03 PM, bearophile <bearophileHUGS@lycos.com> wrote:
> Christopher Wright:
>> XML is commonly used, and while I don't particularly like it, I find it's still a reasonable choice in many circumstances.
>
> I don't know if a D library is available for Json file format, it has some advantages and some disadvantages over XML ().

Tango has one, tango.text.json.Json.
November 16, 2008
"Christopher Wright" <dhasenan@gmail.com> wrote in message news:gfpsdt$2uhd$1@digitalmars.com...
> nobody wrote:
>> I would like to be able to save and load a lot of data to/from a file.
>> (in D1)
>> For example a struct like this:
>>
>> struct Fruit
>> {
>>     int banana;
>>     double[][] orange;
>>     bool[] apple;
>> }
>>
>> Practically all the examples that I've come across only deal with saving and loading text, so I'm having a hard time dealing with saving/loading arrays/floats/bools/etc.
>>
>> What would be a good way to do this?
>
> XML is commonly used, and while I don't particularly like it, I find it's still a reasonable choice in many circumstances.
>
> If you're using Tango, you can check out tango.text.xml.Document, which will let you construct an XML document, and tango.text.xml.DocPrinter, which will let you get the textual representation of such a document.
>
> In phobos, there's std.xml, which should offer equivalent functionality, but I haven't used it.

Unfortunately I'm using phobos, and I think std.xml is only for D2.0.

Is there no similar for D1.0?


November 16, 2008
16.11.08 в 18:55 nobody в своём письме писал(а):

> I would like to be able to save and load a lot of data to/from a file. (in
> D1)
> For example a struct like this:
>
> struct Fruit
> {
>     int banana;
>     double[][] orange;
>     bool[] apple;
> }
>
> Practically all the examples that I've come across only deal with saving and
> loading text, so I'm having a hard time dealing with saving/loading
> arrays/floats/bools/etc.
>
> What would be a good way to do this?
>
>

I know there is a doost.serializer (http://dsource.org/projects/doost/wiki/Serializer) but I never used it myself so I can't say whether it fill fit you.
I am also prototyping another one at the moment, but I don't know how far will it take.
November 16, 2008
On Mon, Nov 17, 2008 at 5:53 AM, Denis Koroskin <2korden@gmail.com> wrote:
> 16.11.08 в 18:55 nobody в своём письме писал(а):
>
>> I would like to be able to save and load a lot of data to/from a file. (in
>> D1)
>> For example a struct like this:
>>
>> struct Fruit
>> {
>>    int banana;
>>    double[][] orange;
>>    bool[] apple;
>> }
>>
>> Practically all the examples that I've come across only deal with saving
>> and
>> loading text, so I'm having a hard time dealing with saving/loading
>> arrays/floats/bools/etc.
>>
>> What would be a good way to do this?
>>
>>
>
> I know there is a doost.serializer
> (http://dsource.org/projects/doost/wiki/Serializer) but I never used it
> myself so I can't say whether it fill fit you.
> I am also prototyping another one at the moment, but I don't know how far
> will it take.
>

team0xf has a library called xpose
which does serialization to and from a binary format.

Can be found here: http://team0xf.com:8080/xf/file/1eb43f0657ec/xpose/

It's part of xf, which you can get using hg:
hg clone http://team0xf.com:8080/xf


--bb

November 16, 2008
On Sun, 16 Nov 2008 16:55:16 +0100, nobody wrote:

> I would like to be able to save and load a lot of data to/from a file. (in
> D1)
> For example a struct like this:
> 
> struct Fruit
> {
>     int banana;
>     double[][] orange;
>     bool[] apple;
> }
> 
> Practically all the examples that I've come across only deal with saving and loading text, so I'm having a hard time dealing with saving/loading arrays/floats/bools/etc.
> 
> What would be a good way to do this?

It depends ... do you wish to save the data as a text representation or in binary form?

Text takes up more room but can be more portable to different applications.

Binary is smaller and usually a lot faster.

For text output, you can format it using one of the popular styles, such as XML, JSON, or CSV, or you can invent something more suitable to your specific requirements. These generally require you to convert each piece of data to a string and wrap that with 'start'/'end' markers.

For binary, you need to decide if 'endian'-ness is an issue or not. If you are happy with the binary layout of the struct data in RAM and you can NOT going to be using the data in other CPU architectures, then the simplest is to just copy out the RAM bytes to disk. Of course, you have to add some 'structure' info to deal with the variable-length arrays but that can be as simple as prefixing the data with the length value.


-- 
Derek Parnell
Melbourne, Australia
skype: derek.j.parnell
November 17, 2008
"Bill Baxter" <wbaxter@gmail.com> wrote in message news:mailman.5.1226871464.22690.digitalmars-d-learn@puremagic.com...
> On Mon, Nov 17, 2008 at 5:53 AM, Denis Koroskin <2korden@gmail.com> wrote:
>> 16.11.08 × 18:55 nobody × Ó×Ï£Í ÐÉÓØÍÅ ÐÉÓÁÌ(Á):
>>
>>> I would like to be able to save and load a lot of data to/from a file.
>>> (in
>>> D1)
>>> For example a struct like this:
>>>
>>> struct Fruit
>>> {
>>>    int banana;
>>>    double[][] orange;
>>>    bool[] apple;
>>> }
>>>
>>> Practically all the examples that I've come across only deal with saving
>>> and
>>> loading text, so I'm having a hard time dealing with saving/loading
>>> arrays/floats/bools/etc.
>>>
>>> What would be a good way to do this?
>>>
>>>
>>
>> I know there is a doost.serializer
>> (http://dsource.org/projects/doost/wiki/Serializer) but I never used it
>> myself so I can't say whether it fill fit you.
>> I am also prototyping another one at the moment, but I don't know how far
>> will it take.
>>
>
> team0xf has a library called xpose
> which does serialization to and from a binary format.
>
> Can be found here: http://team0xf.com:8080/xf/file/1eb43f0657ec/xpose/
>
> It's part of xf, which you can get using hg:
> hg clone http://team0xf.com:8080/xf
>
>
> --bb
>

Oh, I've also used some other team0xf stuff, and that worked splendidly, so
I'll check it out.
Thanks.


November 17, 2008
On Mon, Nov 17, 2008 at 11:07 PM, nobody <somebody@somewhere.com> wrote:
>
> "Bill Baxter" <wbaxter@gmail.com> wrote in message news:mailman.5.1226871464.22690.digitalmars-d-learn@puremagic.com...
>> On Mon, Nov 17, 2008 at 5:53 AM, Denis Koroskin <2korden@gmail.com> wrote:
>>> 16.11.08 в 18:55 nobody в своём письме писал(а):
>>>
>>>> I would like to be able to save and load a lot of data to/from a file.
>>>> (in
>>>> D1)
>>>> For example a struct like this:
>>>>
>>>> struct Fruit
>>>> {
>>>>    int banana;
>>>>    double[][] orange;
>>>>    bool[] apple;
>>>> }
>>>>
>>>> Practically all the examples that I've come across only deal with saving
>>>> and
>>>> loading text, so I'm having a hard time dealing with saving/loading
>>>> arrays/floats/bools/etc.
>>>>
>>>> What would be a good way to do this?
>>>>
>>>>
>>>
>>> I know there is a doost.serializer
>>> (http://dsource.org/projects/doost/wiki/Serializer) but I never used it
>>> myself so I can't say whether it fill fit you.
>>> I am also prototyping another one at the moment, but I don't know how far
>>> will it take.
>>>
>>
>> team0xf has a library called xpose
>> which does serialization to and from a binary format.
>>
>> Can be found here: http://team0xf.com:8080/xf/file/1eb43f0657ec/xpose/
>>
>> It's part of xf, which you can get using hg:
>> hg clone http://team0xf.com:8080/xf
>>
>>
>> --bb
>>
>
> Oh, I've also used some other team0xf stuff, and that worked splendidly, so
> I'll check it out.
> Thanks.

I should mention that it's not quite done yet.  But many fairly tricky things do work already.  For instance, I'm pretty sure that de-serialization of subclasses was working properly last I tried it. That is, if you write out a Derived, and then try to read back a Base, the Base you get back will actually be a pointer to a Derived.   And if you write out the same instance multiple times it only gets saved once, and gets loaded back just once also, and the other N-1 copies are just a  copy of the pointer to the one loaded instance.   I'm pretty sure all that was working last time I tried it.  There was something it couldn't do that I was wanting, but I don't quite recall now...

On the down side, it's not exactly easy code to extend and/or debug.

--bb

« First   ‹ Prev
1 2